drdzpknk76046 2019-03-09 01: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-09 02:25
    关注

    Change your execution part like this.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看