I know how to run a shell command in Ruby like:
%x[#{cmd}]
But, how do I specify a directory to run this command?
Is there a similar way of shelling out, similar to subprocess.Popen
in Python:
subprocess.Popen(r'c:\mytool\tool.exe', cwd=r'd:\test\local')
Thanks!
I had this same problem and solved it by putting both commands in back ticks and separating with '&&':
also, taking the shell route
Maybe it's not the best solution, but try to use Dir.pwd to get the current directory and save it somewhere. After that use Dir.chdir( destination ), where destination is a directory where you want to run your command from. After running the command use Dir.chdir again, using previously saved directory to restore it.
The closest I see to backtricks with safe changing dir is
capture2
:You can see other useful Open3 methods in ruby docs. One drawback is that
jruby
support foropen3
is rather broken.You can use the block-version of
Dir.chdir
. Inside the block you are in the requested directory, after the Block you are still in the previous directory:Ruby 1.9.3 (blocking call):