jose
(jose)
December 13, 2019, 9:06pm
1
Hi all,
For some reason I’m unable to run this code:
fmt.Println("\nChecking if protected-mode is on each .conf file...")
protectedModeStatusByte, _ := exec.Command("sudo", "grep", "^protected-mode", "/etc/redis/*.conf", "|", "wc", "-l").Output()
protectedModeStatus := string(protectedModeStatusByte[:])
fmt.Println(protectedModeStatus)
fmt.Printf("\nprotected-mode Status: %s", protectedModeStatus)
the output is empty:
Checking if protected-mode is on each .conf files...
protected-mode Status:
The funny thing is I do have a similar code for another linux command on the same .go file and it’s running perfectly! It doesn’t make any sense!
Txs!
jabbson
(Karl Benedict)
December 13, 2019, 10:27pm
2
Does this another linux command also pipes the output of one command into another?
jose
(jose)
December 13, 2019, 10:33pm
3
Thanks jabbson for answering.
I’m just trying to run the simple linux grep command from a .go script and seems to be impossible…
if I run the command on linux I’ve got:
NS-000498:$ sudo grep ^protected-mode /etc/redis/*.conf | wc -l
5
I just want to get that “5” for later analysis inside the .go script.
Makes sense?
jabbson
(Karl Benedict)
December 13, 2019, 10:42pm
4
wc := exec.Command("wc", "-l")
grep := exec.Command("sudo", "grep", "^protected-mode", "/etc/redis/*.conf")
pipe, _ := grep.StdoutPipe()
defer pipe.Close()
wc.Stdin = pipe
grep.Start()
res, _ := wc.Output()
fmt.Println(string(res))
jabbson
(Karl Benedict)
December 13, 2019, 10:49pm
5
or
output, _ := exec.Command("sh", "-c", "sudo grep ^protected-mode /etc/redis/*.conf | wc -l").Output()
fmt.Println(string(output))
1 Like
jose
(jose)
December 13, 2019, 10:52pm
6
OMG!! I swear to god that I’ve tried that one before! ;-(
The first solution returned 0. But the second one worked! It’ s not a good day for coding…
Many thanks for your help!
system
(system)
Closed
March 12, 2020, 10:52pm
7
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.