For example, how would I perform the following sequence of commands in
ruby:
cd /dir
ls
system “cd /dir; ls”
or:
Dir.chdir("/dir") do
system “ls”
end
or even better:
puts Dir["/dir/*"]
Generally there’s no way to have the state from one system command
affect
another, but you can change the pwd with Dir.chdir and you can change
environment variables using ENV.
two seperate calls do not work because every system-call creates a new
shell process. If you do a ‘cd’ the first shell does the ‘cd’ and is
then terminated.
The second system call starts a new shell process, which does not know
about the former cd.
You can call system(“(cd /dir; ls)”)
The () inside the system call executes all the commands in one shell
process.
Not counting “ls /dir” that is
Posted viahttp://www.ruby-forum.com/.
=> true
Here’s another way to do it using a here document by having multiple
commands on separate lines - much the same way as in a shell script:
robert@fussel ~
$ ruby /tmp/x.rb
pwd
/cygdrive/c/Dokumente und Einstellungen/robert
cd /tmp
pwd
/tmp
ls
x.rb
robert@fussel ~
$ cat /tmp/x.rb
system <<EOC
set -x
pwd
cd /tmp
pwd
ls
EOC
robert@fussel ~
$
Cheers
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.