在Go语言中运行带有”从子shell重定向输出”语句的shell命令。

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

Run shell command with "redirecting of an output from a subshell" statement within go

问题

根据在新的shell中运行bash命令并在该命令执行后保留在新的shell中,我该如何在Golang中运行以下命令:

bash --rcfile <(echo "export PS1='> ' && ls")

我尝试了许多exec.Command()的组合,但它们都不起作用。例如:

exec.Command("bash", "--rcfile", `<("echo 'ls'")`)

我还阅读了这个os,os/exec:使用重定向符号'<' '>'失败,但我认为我的情况可能有点更复杂。

英文:

According to run bash command in new shell and stay in new shell after this command executes, how can I run command:

bash --rcfile &lt;(echo &quot;export PS1=&#39;&gt; &#39; &amp;&amp; ls&quot;)

within golang? I've tried many combinations of exec.Command() but they did't work. For example:

exec.Command(&quot;bash&quot;, &quot;--rcfile&quot;, `&lt;(&quot;echo &#39;ls&#39;&quot;)`)

I've also read this os, os/exec: using redirection symbol '<' '>' failed, but I think maybe my case is a bit more complicated.

答案1

得分: 3

你离成功很近了-我认为你的困惑在于你正在使用管道来调用bash,这意味着你实际上需要使用bash来调用bash:

exec.Command("bash", "-c", `bash --rcfile <(echo "export PS1='> ' && ls")`)
英文:

You're almost there - I think the confusion is you're using piping to call bash, which means you actually need to call bash using bash:

exec.Command(&quot;bash&quot;, &quot;-c&quot;, `bash --rcfile &lt;(echo &quot;export PS1=&#39;&gt; &#39; &amp;&amp; ls&quot;)`)

huangapple
  • 本文由 发表于 2017年8月16日 01:49:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/45698552.html
匿名

发表评论

匿名网友

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

确定