如何用未知参数在Golang中执行系统命令 [英] How to execute system command in Golang with unknown arguments
问题描述
我有一堆系统命令,这些命令与将新内容附加到文件中很相似。我写了一个简单的脚本来执行系统命令,如果有'ls','date'等单个单词,这很好用。但是如果命令大于此,程序就会死掉。
以下是代码
包主
导入(
fmt
os / exec
同步
)
func exe_cmd(cmd字符串,wg * sync.WaitGroup){
fmt.Println(cmd)
c = cmd.Str $ b $ out,err:= exec.Command(cmd).Output()
if err!= nil {
fmt.Println(error occured)
fmt.Printf(%s,err)
}
fmt.Printf(%s,out)
wg.Done()
}
func main(){
wg:= new(sync.WaitGroup)
wg.Add(3)
x:= [] string {echo newline>> foo.o,echo newline>> f1.o,echo newline>> f2.o}
转到exe_cmd(x [0],wg)
转到exe_cmd(x [1],wg)
转到exe_cmd(x [2],wg)
wg.Wait()
}
以下是我看到的错误
exec:echo newline>> foo.o:$ PATHexec中找不到可执行文件:
echo newline&g吨;> f2.o:在$ PATHexec中找不到可执行文件:
echo newline>> f1.o:在$ PATH中找不到可执行文件
我想这可能是由于,不单独发送命令和参数( http://golang.org/pkg/os/exec/#命令)。我想知道如何去颠覆这个,因为我不知道在我的命令中会有多少个参数需要执行。
我找到了一个相对体面的方法来达到同样的效果。
out,err:= exec。 Command(sh, - c,cmd).Output()
现在仍然可以找到更好的方法来实现同样的效果。
Edit1:
最后一个更简单高效(至少到目前为止)的方式是这样的
func exe_cmd(cmd string,wg * sync.WaitGroup){
fmt.Println(command is,cmd)
// splitting head => g ++部分=>命令的其余部分
部分:=字符串.fields(cmd)
head:= parts [0]
parts = parts [1:len(parts)]
$ b $ out,err:= exec.Command(head,部分...)。Output()
if err!= nil {
fmt.Printf(%s,err)
}
fmt.Printf(%s ,out)
wg.Done()//需要发信号给waitgroup这个goroutine完成
code $
感谢go和variadic的参数,指出了这一点:)
I have a bunch of systems commands which are somwhat similar to appending new content to a file. I wrote a simple script to execute system commands, which works well if there are single words like 'ls' , 'date' etc. But if the command is greater than that, program dies.
The following is the code
package main
import (
"fmt"
"os/exec"
"sync"
)
func exe_cmd(cmd string, wg *sync.WaitGroup) {
fmt.Println(cmd)
c = cmd.Str
out, err := exec.Command(cmd).Output()
if err != nil {
fmt.Println("error occured")
fmt.Printf("%s", err)
}
fmt.Printf("%s", out)
wg.Done()
}
func main() {
wg := new(sync.WaitGroup)
wg.Add(3)
x := []string{"echo newline >> foo.o", "echo newline >> f1.o", "echo newline >> f2.o"}
go exe_cmd(x[0], wg)
go exe_cmd(x[1], wg)
go exe_cmd(x[2], wg)
wg.Wait()
}
The following is the error i see
exec: "echo newline >> foo.o": executable file not found in $PATHexec:
"echo newline >> f2.o": executable file not found in $PATHexec:
"echo newline >> f1.o": executable file not found in $PATH
I guess, this may be due to, not sending cmds and arguments seperately ( http://golang.org/pkg/os/exec/#Command ) . I am wondering how to subvert this , since I dunno how many arguments will be there in my command which needs to be executed.
解决方案 I found a relatively decent way to achieve the same .
out, err := exec.Command("sh","-c",cmd).Output()
Works for me until now. Still finding better ways to achieve the same.
Edit1:
Finally a easier and efficient (atleast so far) way to do would be like this
func exe_cmd(cmd string, wg *sync.WaitGroup) {
fmt.Println("command is ",cmd)
// splitting head => g++ parts => rest of the command
parts := strings.Fields(cmd)
head := parts[0]
parts = parts[1:len(parts)]
out, err := exec.Command(head,parts...).Output()
if err != nil {
fmt.Printf("%s", err)
}
fmt.Printf("%s", out)
wg.Done() // Need to signal to waitgroup that this goroutine is done
}
Thanks to variadic arguments in go and people that pointed that out to me :)
这篇关于如何用未知参数在Golang中执行系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!