This is making me crazy! Can't get the output of an exec.Command call in Go.

xuanbao · · 527 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello All,</p> <p>I have been struggling with a basic problem for a day now I hope someone can help me with this as I am obviously missing something here.</p> <p>What I am trying to do is extremely basic, I am making a exec.Command call to run a perl command and then get the output of the command but the output is always empty. First here is the code:</p> <pre><code> func main() { cmd := exec.Command(&#34;/usr/bin/perl&#34;, &#34;-e&#34;, &#34;\&#34;print &#39;hello from perl&#39;\&#34;&#34;) var out bytes.Buffer cmd.Stdout = &amp;out cmd.Stderr = &amp;out err := cmd.Run() if err != nil { log.Fatal(err) } fmt.Println(len(out.Bytes())) } </code></pre> <p>If I were to execute /usr/bin/perl -e &#34;print &#39;hello from perl&#39;&#34; manually and outside Go I will get the output which proves that perl is installed properly</p> <p>even if I change the command in my go to simple /usr/bin/perl -h I will get to see the output but for some reason my code doesn&#39;t get the output from stdout when actually running a perl code.</p> <p>Has anyone experienced something similar before?</p> <hr/>**评论:**<br/><br/>v1rous: <pre><p>You&#39;ve got one too many pairs of quotation marks around your Perl command.</p> <p>When you run the following command in a shell (such as bash), there are three arguments:</p> <pre><code>$ /usr/bin/perl -e &#34;print &#39;hello from perl&#39;&#34; </code></pre> <ol> <li><code>/usr/bin/perl</code></li> <li><code>-e</code> and</li> <li><code>print &#39;hello from perl&#39;</code></li> </ol> <p>Typically, when typing commands at a shell, spaces are used to separate arguments. Quotation marks tell the shell to treat a whole chunk of text (in this case <code>print &#39;hello...</code>) as a single argument.</p> <p><code>exec.Cmd</code> is <strong>not</strong> a shell, so you don&#39;t need those extra quotes around your Perl statement. By including them, your Perl statement is <code>&#34;print &#39;hello from perl&#39;&#34;</code>. Note the quotation marks: Perl treats this as a string, not as a print statement, and so it doesn&#39;t actually print anything.</p> <p>Try using the following to construct your exec.Cmd:</p> <pre><code>cmd := exec.Command(&#34;/usr/bin/perl&#34;, &#34;-e&#34;, &#34;print &#39;hello from perl&#39;&#34;) </code></pre></pre>v1rous: <pre><p>P.S. Check out the <a href="https://www.reddit.com/wiki/commenting" rel="nofollow">formatting guide</a> for tips on how to format code in a post so that it&#39;s readable.</p></pre>forexross: <pre><p>Thank you my friend. That was the problem.</p></pre>bloodqc: <pre><p>Maybe try <a href="https://golang.org/pkg/os/exec/#Cmd.CombinedOutput" rel="nofollow">Cmd.CombinedOutput</a>.</p> <p>And format your code by putting four spaces at the start of each line.</p></pre>jahayhurst: <pre><p>You are using <code>Run</code>, don&#39;t. Don&#39;t use <code>Run</code> - you have to read from the pipe as you&#39;re running the command. Use <code>Start()</code> then read instead, and maybe <code>defer cmd.Wait()</code>?</p> <p><a href="https://golang.org/pkg/os/exec/#Cmd.StdoutPipe" rel="nofollow">Here&#39;s the example</a>.</p> <p>Basically, if you call <code>Run</code> or <code>Wait</code> on <code>exec.Command</code>, execution of that thread will pause until the <code>io.ReadCloser</code> closes.</p> <p>You have to set up the command, and the pipes, then call Start, then read your output, <em>then</em> call <code>Wait</code> when you&#39;re done reading. For anything more complex than a simple example, tbh, it might be better to just wrap the stuff and have a function that you call to use it, and return a <code>[]byte</code> of the <code>stdout</code> and <code>[]byte</code> of <code>stderr</code>.</p> <p>At least, that&#39;s what all of this reads like to me when reading the package.</p></pre>weberc2: <pre><p>He&#39;s not using pipes. Run is fine.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

527 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传