如何执行带有未知参数的系统命令? [英] How to execute system command with unknown arguments?
问题描述
我有一堆系统命令,它们类似于将新内容附加到文件中.我编写了一个简单的脚本来执行系统命令,如果有单个单词,如"ls","date"等,它会很好地工作.但是如果命令大于此范围,程序就会死掉.
以下是代码
程序包主要进口 ("fmt""os/exec"同步")func exe_cmd(cmd string,wg * sync.WaitGroup){fmt.Println(cmd)c = cmd.Str出,错误:= exec.Command(cmd).Output()如果err!= nil {fmt.Println(发生错误")fmt.Printf(%s",err)}fmt.Printf(%s",输出)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>>f2.o":在$ PATHexec中找不到可执行文件:" echo newline>>f1.o":在$ PATH中找不到可执行文件
我想,这可能是由于未单独发送cmd和参数( http://golang.org/pkg/os/exec/#Command ).我想知道如何颠覆这一点,因为我不知道命令中将要执行多少个参数.
我找到了一种相对不错的方法来实现相同目的.
out,err:= exec.Command("sh",-c",cmd).Output()
直到现在为我工作.仍在寻找更好的方法来实现这一目标.
Edit1:
最后一个简单,高效(到目前为止)的方法就是这样
func exeCmd(cmd string,wg * sync.WaitGroup){fmt.Println(命令为",cmd)//劈头=>g ++部分=>命令的其余部分零件:= strings.Fields(cmd)头:=零件[0]零件=零件[1:len(parts)]出,错误:= exec.Command(head,parts ...).Output()如果err!= nil {fmt.Printf(%s",err)}fmt.Printf(%s",输出)wg.Done()//需要发信号给waitgroup这个goroutine已经完成}
感谢go中各种各样的争论,还有向我指出的人:)
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 don't know 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 exeCmd(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 :)
这篇关于如何执行带有未知参数的系统命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!