<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("/usr/bin/perl", "-e", "\"print 'hello from perl'\"")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &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 "print 'hello from perl'"
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'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'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 "print 'hello from perl'"
</code></pre>
<ol>
<li><code>/usr/bin/perl</code></li>
<li><code>-e</code> and</li>
<li><code>print 'hello from perl'</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 'hello...</code>) as a single argument.</p>
<p><code>exec.Cmd</code> is <strong>not</strong> a shell, so you don't need those extra quotes around your Perl statement. By including them, your Perl statement is <code>"print 'hello from perl'"</code>. Note the quotation marks: Perl treats this as a string, not as a print statement, and so it doesn't actually print anything.</p>
<p>Try using the following to construct your exec.Cmd:</p>
<pre><code>cmd := exec.Command("/usr/bin/perl", "-e", "print 'hello from perl'")
</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'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't. Don't use <code>Run</code> - you have to read from the pipe as you'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'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'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's what all of this reads like to me when reading the package.</p></pre>weberc2: <pre><p>He's not using pipes. Run is fine.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传