英文:
Creating a function that runs arbitrary shell commands in Go
问题
我正在尝试在Go语言中创建一个通用函数,该函数接受一个带有参数的shell命令并执行它。
请注意,shell命令的参数数量可能不同。
我了解到Go语言中的exec.Command()
函数以("ls", "-la", "/var")
的格式接受命令和参数。
下面的代码片段无法正常工作:
func exe_cmd(commandarray []string) {
var shell_command string = ""
for i := 0; i < len(commandarray); i++ {
shell_command = shell_command + " " + commandarray[i]
}
cmd := exec.Command(shell_command)
fmt.Println("Command is: ", shell_command)
fmt.Printf("==> Executing: %s\n", strings.Join(cmd.Args, " "))
out, err := cmd.Output()
fmt.Printf("OutVar: %s\n", out)
fmt.Printf("ErrVar: %s\n", err)
}
func dummy() {
dummycmd := []string{"ls", "-la", "/var"}
exe_cmd(dummycmd)
}
在for
循环结束时,shell_command
变量将包含"ls -la /var"
,我很难弄清楚如何以符合输入格式要求的方式形成它,即"ls", "-la", "/var"
。
我还尝试将dummycmd
设置为变量,并赋值为"ls -la /var"
,但我发现如果要求是以上述格式将其传递给exec.Command
,那么这样做更糟糕。
有什么想法吗?
谢谢。
英文:
I am trying to create a generic function in go that takes in input a shell command (with arguments) and execute it.
Note the shell commands may have a different number of arguments.
I understand that the exec.Command()
in Go takes as input the format ("ls", "-la", "/var")
for the command + arguments.
This code snipped below doesn't work:
func exe_cmd(commandarray []string) {
var shell_command string = ""
for i := 0; i < len(commandarray); i++ {
shell_command = shell_command + " " + commandarray[i]
}
cmd := exec.Command(shell_command)
fmt.Println("Command is: ", shell_command)
fmt.Printf("==> Executing: %s\n", strings.Join(cmd.Args, " "))
out, err := cmd.Output()
fmt.Printf("OutVar: %s\n", out)
fmt.Printf("ErrVar: %s\n", err)
}
func dummy() {
dummycmd := []string{"ls", "-la", "/var"}
exe_cmd(dummycmd)
}
A the end of the for
the shell_command
variable will contain "ls -la /var"
and I am having a hard time to figure out how to form it in a way that is what the input format requires i.e. "ls", "-la", "/var"
I have also tried to set dummycmd
as a variable with "ls -la /var"
but I figure that was even worse if requirement is to pass it to the exec.Command in the format above.
Thoughts?
Thanks.
答案1
得分: 5
你可以简单地匹配exec.Command
函数的参数签名,它是:
(name string, arg ...string)
然后使用可变参数语法将函数参数传递给exec.Command
。
func execCmd(name string, args ...string) {
cmd := exec.Command(name, args...)
fmt.Printf("Command is: %s, %s", name, args)
out, err := cmd.Output()
fmt.Printf("OutVar: %s\n", out)
fmt.Printf("ErrVar: %s\n", err)
}
请注意,这里不会调用shell,因此依赖于任何shell内置命令将会失败。
英文:
You can simply match the argument signature of the exec.Command
function, which is
(name string, arg ...string)
And use the variadic syntax to pass the function argument along to exec.Command
func execCmd(name string, args ...string) {
cmd := exec.Command(name, args...)
fmt.Printf("Command is: %s, %s", name, args)
out, err := cmd.Output()
fmt.Printf("OutVar: %s\n", out)
fmt.Printf("ErrVar: %s\n", err)
}
Note however, you're not invoking a shell here, so relying on any shell builtin's will fail.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论