コマンド結果を受け取らず、実行完了まで待機
- Run
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"os/exec"</span> <span class="p">)</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">exec</span><span class="o">.</span><span class="n">Command</span><span class="p">(</span><span class="s">"pwd"</span><span class="p">)</span><span class="o">.</span><span class="n">Run</span><span class="p">()</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Command Exec Error."</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
コマンド結果を受け取る
- Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"os/exec"</span> <span class="p">)</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">exec</span><span class="o">.</span><span class="n">Command</span><span class="p">(</span><span class="s">"pwd"</span><span class="p">)</span><span class="o">.</span><span class="n">Output</span><span class="p">()</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Command Exec Error."</span><span class="p">)</span> <span class="p">}</span> <span class="c">// 実行したコマンドの結果を出力</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"pwd result: %s"</span><span class="p">,</span> <span class="kt">string</span><span class="p">(</span><span class="n">out</span><span class="p">))</span> <span class="p">}</span> |
標準出力エラーを受け取る
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="o">-</span> <span class="n">CombinedOutput</span> <span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"os/exec"</span> <span class="p">)</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">exec</span><span class="o">.</span><span class="n">Command</span><span class="p">(</span><span class="s">"ls"</span><span class="p">,</span> <span class="s">"dummy"</span><span class="p">)</span><span class="o">.</span><span class="n">CombinedOutput</span><span class="p">()</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Command Exec Error."</span><span class="p">)</span> <span class="p">}</span> <span class="c">// 実行したコマンドの標準出力+標準エラー出力の内容</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"ls result: </span><span class="se">\n</span><span class="s">%s"</span><span class="p">,</span> <span class="kt">string</span><span class="p">(</span><span class="n">out</span><span class="p">))</span> <span class="p">}</span> |
コマンド実行終了を待たない
- start
- waitとの組み合わせで待つことができる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"os/exec"</span> <span class="p">)</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">cmd</span> <span class="o">:=</span> <span class="n">exec</span><span class="o">.</span><span class="n">Command</span><span class="p">(</span><span class="s">"sleep"</span><span class="p">,</span> <span class="s">"10"</span><span class="p">)</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Command Start."</span><span class="p">)</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">cmd</span><span class="o">.</span><span class="n">Start</span><span class="p">()</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Command Exec Error."</span><span class="p">)</span> <span class="p">}</span> <span class="c">//コメント外すと待機する</span> <span class="c">//cmd.Wait()</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Command Exit."</span><span class="p">)</span> <span class="p">}</span> |
参考