drdzpknk76046 2019-03-08 17:57
浏览 48
已采纳

如何将临时文件作为命令行参数传递

I am trying to use a tempfile so that I can pass the tempfile as an argument to a exec command.

I declare the command I am going to use as

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

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

I finally run the command as

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

To confirm abc.sh is present I do

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

Also to confirm that the Tempfile is present , I do

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

I can see both of these files being present here using the ls command. I am not getting why I am getting an error which fails to find either of these files. Also what file is not found here, the .sh file or the tempfile

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

I have tried to summarize the code to make it easy to understand.

  • 写回答

1条回答 默认 最新

  • 普通网友 2019-03-08 18:25
    关注

    Change your execution part like this.

    cmd := exec.Command("bash","-c",command)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部