3 回答

TA貢獻1851條經驗 獲得超5個贊
由于您已經使用了sh -c,因此下一個參數應該是完整的命令或命令:
SystemdockerCommand := exec.Command("sh", "-c", "docker exec 9aa1124 gluster peer detach 192.168.1.1 force")
更一般地說,如下所示:
cmd := exec.Command("/bin/sh", "-c", "command1 param1; command2 param2; command3; ...")
err := cmd.Run()? ? ? ?
看這個例子:
sh := os.Getenv("SHELL") //fetch default shell
//execute the needed command with `-c` flag
cmd := exec.Command(sh, "-c ", `docker exec 9aa1124 ...`)
或者這個,首先將命令放入字符串中:
cmd := "cat /proc/cpuinfo | egrep '^model name' | uniq | awk '{print substr($0, index($0,$4))}'"
out, err := exec.Command("bash","-c",cmd).Output()
if err != nil {
? ? return fmt.Sprintf("Failed to execute command: %s", cmd)
}
- 3 回答
- 0 關注
- 402 瀏覽
添加回答
舉報