Os Exec Sudo Command in Go
Background
While exploring Go and goroutines, users may encounter difficulties executing commands with the format:
sudo find /folder -type f | while read i; do sudo -S chmod 644 "$i"; done
Question
Answer
cmd := exec.Command("/bin/sh", "-c", "sudo find ...")
Code Modifications
Here's the modified code:
package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("/bin/sh", "-c", "sudo find /folder -type f | while read i; do sudo -S chmod 644 \"\"; done") out, err := cmd.CombinedOutput() if err != nil { fmt.Printf("Error: %s\nOutput: %s", err, out) } }
The above is the detailed content of Why Does `exec.Command()` Return Exit Status 1 When Running `sudo` Commands in Go?. For more information, please follow other related articles on the PHP Chinese website!