Unable to run an Ubuntu command

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!

Does this another linux command also pipes the output of one command into another?

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?

	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))

or

output, _ := exec.Command("sh", "-c", "sudo grep ^protected-mode /etc/redis/*.conf | wc -l").Output()
fmt.Println(string(output))
1 Like

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!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.