Browse Source

Add printenv

dtluna 5 months ago
parent
commit
b7178c3a2c
2 changed files with 33 additions and 0 deletions
  1. 1
    0
      README.md
  2. 32
    0
      printenv/printenv.go

+ 1
- 0
README.md View File

@@ -14,3 +14,4 @@ Implemeted utilities:
14 14
  * `dirname`
15 15
  * `basename`
16 16
  * `echo`
17
+ * `printenv`

+ 32
- 0
printenv/printenv.go View File

@@ -0,0 +1,32 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"os"
6
+)
7
+
8
+func printStrings(strings []string) {
9
+	for _, str := range strings {
10
+		fmt.Println(str)
11
+	}
12
+}
13
+
14
+func main() {
15
+	if len(os.Args) == 1 {
16
+		printStrings(os.Environ())
17
+		os.Exit(0)
18
+	}
19
+
20
+	exitCode := 0
21
+	envVars := []string{}
22
+	for _, key := range os.Args[1:] {
23
+		val, present := os.LookupEnv(key)
24
+		if present {
25
+			envVars = append(envVars, val)
26
+		} else {
27
+			exitCode = 1
28
+		}
29
+	}
30
+	printStrings(envVars)
31
+	os.Exit(exitCode)
32
+}

Loading…
Cancel
Save