Os Exec Sudo Command in Go -
in process of familiarizing myself go , goroutines have hit road block executing commands. format of these commands are:
sudo find /folder -type f | while read i; sudo -s chmod 644 "$i"; done
with code taken how execute system command in golang unknown arguments i'm trying execute command believe it's not executing due first argument being sudo, wrong. have 2 questions.
when these commands fail run returned "exit status 1", there way more detailed error doing? question two, why getting "exit status 1" script? going on isn't supposed to?
package main import ( "bufio" "fmt" "os" "os/exec" "strings" "sync" ) func execmd(cmd string, wg *sync.waitgroup) { parts := strings.fields(cmd) head := parts[0] // head @ point "sudo" parts = parts[1:len(parts)] out, err := exec.command(head,parts...).output() if err != nil { fmt.printf("%s\n", err) } fmt.printf("%s\n", out) wg.done() // signal waitgroup goroutine finished } func inarray(a []string, e string) bool { _, x := range { if x == e { return true fmt.print("true") } } return false } func main() { exec.command("sudo ls > /dev/null") // sudo password once, doesn't seem work go wg := new(sync.waitgroup) reader := bufio.newreader(os.stdin) fdbslices := []string{"f", "d", "b", "files", "directories", "both"} commands := []string{} fdb := "" filep := "" dirp := "" // grab path fmt.print("path: ") findpath, _ := reader.readstring('\n') findpath = strings.tolower(strings.trim(findpath, "\n")) // grab files, directories, or both { fmt.print("files, directories, or both: ") fdb, _ = reader.readstring('\n') fdb = strings.tolower(strings.trim(fdb, "\n")) if inarray(fdbslices, fdb) == true { break } } // build commands string, these come out should { if fdb == "f" || fdb == "files" { fmt.print("permissions files: ") filep, _ = reader.readstring('\n') filep = strings.trim(filep, "\n") commands = append(commands, "sudo find " + findpath + " -type f | while read i; sudo -s chmod " + filep + " \"$i\"; done") } if fdb == "d" || fdb == "directories" { fmt.print("permissions directories: ") dirp, _ = reader.readstring('\n') dirp = strings.trim(dirp, "\n") commands = append(commands, "sudo find " + findpath + " -type d | while read i; sudo -s chmod " + dirp + " \"$i\"; done") } if fdb == "b" || fdb == "both" { fmt.print("permissions files: ") filep, _ = reader.readstring('\n') filep = strings.trim(filep, "\n") commands = append(commands, "sudo find " + findpath + " -type f | while read i; sudo -s chmod " + filep + " \"$i\"; done") fmt.print("permissions directories: ") dirp, _ = reader.readstring('\n') dirp = strings.trim(dirp, "\n") commands = append(commands, "sudo find " + findpath + " -type d | while read i; sudo -s chmod " + dirp + " \"$i\"; done") } break } // execute commands _, str := range commands { wg.add(1) fmt.print("doing: " + str + "\r\n") go execmd(str, wg) } wg.wait() }
example terminal output:
path: test files, directories, or both: b permissions files: 644 permissions directories: 755 doing: find test -type f | while read i; sudo -s chmod 644 "$i"; done doing: find test -type d | while read i; sudo -s chmod 755 "$i"; done exit status 1 exit status 1
thank time.
exec.command()
ask kernel execute given process directly. however, command passing string of multiple programs hooked shell script.
if want execute shell script, should use this:
cmd := exec.command("/bin/sh", "-c", "sudo find ...")
Comments
Post a Comment