使用Go运行带参数的sh/bash/python脚本。

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

Running sh/bash/python scripts with arguments using Go

问题

我已经卡在这个问题上几天了,我试图运行一个基于第一个参数的bash脚本(也许我应该放弃所有希望,哈哈)。

可以假设运行脚本的语法是:

sudo bash script argument 或者由于它具有og+x权限,可以简单地运行 sudo script argument

在Go语言中,我使用以下代码运行它:

package main

import (
    "os"
    "os/exec"
    "fmt"
)

func main() {
    c := exec.Command("/bin/bash", "script " + argument)
    if err := c.Run(); err != nil {
        fmt.Println("Error: ", err)
    }
    os.Exit(0)
}

我完全没有运气,我还尝试了很多其他变体...

exec.Command("/bin/sh", "-c", "sudo script", argument)

exec.Command("/bin/sh", "-c", "sudo script " + argument)(我的第一次尝试)

exec.Command("/bin/bash", "-c", "sudo script" + argument)

exec.Command("/bin/bash", "sudo script", argument)

exec.Command("/bin/bash sudo script" + argument)

大多数情况下,我遇到了/bin/bash sudo ect没有这样的文件或目录,或者Error: exit status 1。我甚至写了一个Python包装器,寻找一个参数并使用subprocess执行bash脚本,以排除脚本路径未定义的可能性,我尝试了上述所有方法,而不是脚本名称。

为了保留我剩下的头发,我在这里做错了什么?如何更好地诊断这个问题,以便我可以获得更多信息,而不仅仅是退出状态1?

英文:

I've been stuck on this one a few days, I'm trying to run a bash script which runs off of the first argument (maybe I should give up all hope, haha)

Syntax for running the script can be assumed to be:

sudo bash script argument or since it has og+x it can be ran as just sudo script argument

In go I'm running it using the following:

package main

import (
    "os"
    "os/exec"
    "fmt"
)

func main() {
    c := exec.Command("/bin/bash", "script " + argument)
    if err := c.Run(); err != nil {
        fmt.Println("Error: ", err)
    }
    os.Exit(0)
}

I have had absolutely no luck, I've tried loads of other variations as well for this...

exec.Command("/bin/sh", "-c", "sudo script", argument)

exec.Command("/bin/sh", "-c", "sudo script " + argument) (my first try)

exec.Command("/bin/bash", "-c", "sudo script" + argument)

exec.Command("/bin/bash", "sudo script", argument)

exec.Command("/bin/bash sudo script" + argument)

Most of these I am met with '/bin/bash sudo ect' no such file or directory, or Error: exit status 1 I have even gone as far as to write a Python wrapper looking for an argument and executing the bash script with subprocess. To rule out the path to the script not being defined I have tried all of the above with a direct route to the script rather than script name.

For the sake of my remaining hair, what am I doing wrong here? How can I better diagnose this problem so that I can get more information rather than exit status 1?

答案1

得分: 4

你根本不需要调用bash/sh,只需单独传递每个参数,而且要获取错误信息,你需要捕获命令的stderr。下面是一个可行的示例:

func main() {
    c := exec.Command("sudo", "ls", "/tmp")
    stderr := &bytes.Buffer{}
    stdout := &bytes.Buffer{}
    c.Stderr = stderr
    c.Stdout = stdout
    if err := c.Run(); err != nil {
        fmt.Println("错误: ", err, "|", stderr.String())
    } else {
        fmt.Println(stdout.String())
    }
    os.Exit(0)
}

这段代码会执行sudo ls /tmp命令,并将stdout和stderr分别存储在stdoutstderr变量中。如果命令执行出错,将打印错误信息和stderr的内容;否则,将打印stdout的内容。最后,使用os.Exit(0)退出程序。

英文:

You don't need to call bash/sh at all, simply pass each argument alone, also to get the error you have to capture the command's stderr, here's a working example:

func main() {
	c := exec.Command("sudo", "ls", "/tmp")
	stderr := &bytes.Buffer{}
	stdout := &bytes.Buffer{}
	c.Stderr = stderr
	c.Stdout = stdout
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err, "|", stderr.String())
	} else {
		fmt.Println(stdout.String())
	}
	os.Exit(0)
}

huangapple
  • 本文由 发表于 2014年7月12日 19:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/24712431.html
匿名

发表评论

匿名网友

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

确定