-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprintenv.py
More file actions
48 lines (42 loc) · 1.25 KB
/
printenv.py
File metadata and controls
48 lines (42 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 20-11-2024
Last update: 20-11-2024
Version: 1.0
Description: The printenv command from GNU coreutils in Python3.
Example of use: python3 printenv.py
'''
import os
import sys
def printenv(specific_vars=None):
"""
Print environment variables
Args:
specific_vars (list): List of specific environment variables to print
"""
try:
# If no specific variables requested, print all
if not specific_vars:
for key, value in os.environ.items():
print(f"{key}={value}")
else:
# Print only requested variables
for var in specific_vars:
value = os.environ.get(var)
if value is not None:
print(value)
else:
# Variable not found
print(f"printenv: {var}: No such environment variable", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"printenv error: {e}", file=sys.stderr)
sys.exit(1)
def main():
# Parse command-line arguments
args = sys.argv[1:]
printenv(args)
if __name__ == "__main__":
main()