ruby で cd コマンドを実行する
ruby スクリプト内で cd コマンドを利用すると、「command not found: cd」とエラーが表示される。
スクリプト内で、cd コマンドを実行するには、「cd hoge;」と記述するとエラーが表示されず、カレントディレクトリから移動ができる。
コマンド実行後は、ruby スクリプトが存在するディレクトリに戻る。
irb で簡単に試してみる。
% pwd /home/littlebuddha % mkdir test % touch test/test.txt % ls -l test/ -rw-r--r-- 1 littlebuddha webadmins 0 Apr 11 16:47 test.txt % irb irb(main):001:0> %x{pwd} => "/home/littlebuddha\n" irb(main):002:0> %x{cd test} (irb):2: command not found: cd test => "" irb(main):003:0> %x{cd test; ls -l; cd ~} => "total 0\n-rw-r--r-- 1 littlebuddha webadmins 0 Apr 11 16:47 test.txt\n" irb(main):004:0> %x{cd test; ls -l; pwd} => "total 0\n-rw-r--r-- 1 littlebuddha webadmins 0 Apr 11 16:47 test.txt\n/home/littlebuddha/test\n" irb(main):005:0> %x{pwd} => "/home/littlebuddha\n" irb(main):006:0> exit %