0
0

More than 1 year has passed since last update.

ruby の popen で sh: -c: line 0: syntax error near unexpected token `('

Posted at

はじめに

diff -u <(du -ak hoge) <(du -ak hoge2)

のような <() で結果をpipeで渡すのを IO.popenで実行しようとしたら、

sh: -c: line 0: syntax error near unexpected token `('

が出たときは、実は、sh -cで実行されてしまっているところが問題。
shでは、<を解釈できない。

というわけで、解決法

IO.popen(["bash", "-c", exec_cmd], "r", :chdir=>execPath) {}

のように[]で、bash, -c, 実行したいコマンドとして入れてあげれば良い。

こうすれば、bashで実行される。

駄目な例

IO.popen(exec_cmd, "r", :chdir=>execPath) {}

のようになっていると、sh -cで実行されてしまう。

exec_cmdにbash -c 実行したいコマンドとやってもうまくはいかなく、上のように、[]で与えるべきである。

exec_cmd = "bash -c #{Shellwords.shellescape(exec_cmd)}"
のように、してもダメで、[]として、["bash", "-c", exec_cmd]popenに渡せばOkであった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0