创建一个在Go中运行任意shell命令的函数。

huangapple go评论83阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年12月22日 21:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/34417008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定