如何将tempfile作为命令行参数传递

问题描述 投票:-2回答:1

我正在尝试使用临时文件,以便我可以将tempfile作为参数传递给exec命令。

我声明了我将要使用的命令

CLI_CMD := "/home/go/src/hello/abc.sh"

xmlFile, err := ioutil.TempFile("", hostIP)
command := CLI_CMD + " " + xmlFile.Name()

我终于把命令当作了

cmd := exec.Command(command)
stdout, err := cmd.Output()

为了确认abc.sh存在,我做

[prompt] ls /home/go/src/hello/abc.sh
/home/go/src/hello/abc.sh

另外,为了确认Tempfile存在,我做了

[prompt] ls /tmp/10.166.30.47.xml187906126
/tmp/10.166.30.47.xml187906126

我可以使用ls命令在这里看到这两个文件。我不明白为什么我收到一个错误,无法找到这些文件。此外,在这里找不到什么文件,.sh文件或临时文件

2019/03/08 17:50:31 fork/exec /home/go/src/hello/abc.sh /tmp/10.166.30.47.xml187906126: no such file or directory

我试图总结一下代码,以便于理解。

go temporary-files
1个回答
2
投票

像这样更改你的执行部分。

cmd := exec.Command("bash","-c",command)
© www.soinside.com 2019 - 2024. All rights reserved.