exec.Command with variables

polaris · · 591 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, I am trying to run a linux command via Go. Here is what I have, for some reason it is not working... Any advice?</p> <p>exec.Command(&#34;echo&#34;, &#34;&#39;hello! your email is: &#39;&#34; + email + &#34; | mail -s &#39;Activate Book Hook Account&#39; &#34; + email)</p> <p>Thanks!</p> <hr/>**评论:**<br/><br/>_dirtyburger: <pre><p>You can do something like:</p> <pre><code>email := &#34;fake@fake.com&#34; line := &#34;echo hello your email is: &#34; + email + &#34; | cut -c 1-8&#34; out, err := exec.Command(&#34;bash&#34;, &#34;-c&#34;, line).Output() if err != nil { log.Fatalf(&#34;Command line error: %v&#34;, err) } fmt.Printf(&#34;Command line output: %v&#34;, string(out)) </code></pre> <p>Use the bash command and give the -c option to read commands from the string which contains the command line. Im not sure what your command line is trying to do though. </p></pre>justinisrael: <pre><p>The command is trying to dynamically format an email to a particular user by piping it </p></pre>wwader: <pre><p>Hi, that only defines a command, you have to run it also using .Output(), .Run() or .Start() etc</p></pre>emersion_fr: <pre><p>Be very careful when doing things like this. It&#39;s really super-easy to insert <code>;rm -rf *;</code> in the command.</p></pre>

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

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