Forum: Ruby Multiple commands in shell

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
798c7f10aed317c27155ba7c53221df6?d=identicon&s=25 Gu stav (gustav)
on 2009-01-07 17:14
I'm trying to run multiple commands in the shell from ruby but I can't
seem to grasp how I keep the "state" from the previous shell command.

For example, how would I perform the following sequence of commands in
ruby:

1. cd /dir
2. ls

Not counting "ls /dir" that is ;)

I've tried using:

system "cd /dir"
system "ls"

..but it doesn't work.

Thanks!
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-01-07 17:26
(Received via mailing list)
Gu stav wrote:
> For example, how would I perform the following sequence of commands in
> ruby:
>
> 1. cd /dir
> 2. 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.

HTH,
Sebastian
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 Jan-Erik R. (Guest)
on 2009-01-07 17:29
(Received via mailing list)
Gu stav schrieb:
>
> I've tried using:
>
> system "cd /dir"
> system "ls"
>
> ..but it doesn't work.
>
> Thanks!
Ruby provides excellent classes for this called Dir and File (and maybe
FileUtils)
RDoc can be found at:
http://www.ruby-doc.org/core/classes/File.html
and
http://www.ruby-doc.org/core/classes/Dir.html
798c7f10aed317c27155ba7c53221df6?d=identicon&s=25 Gu stav (gustav)
on 2009-01-07 19:08
Works great. Thanks a million!
2e8848a777f7165f80da876ab1e6e7e5?d=identicon&s=25 dusty (Guest)
on 2009-01-07 19:11
(Received via mailing list)
On Jan 7, 11:14 am, Gu stav <gus...@vonsydow.tv> wrote:
>
> I've tried using:
>
> system "cd /dir"
> system "ls"
>
> .but it doesn't work.
>
> Thanks!
> --
> Posted viahttp://www.ruby-forum.com/.


[dusty@dustylaptop:~] $ pwd
/Users/dusty
[dusty@dustylaptop:~] $ ls tmp/
test.txt

irb(main):003:0> system('pwd; cd tmp; ls')
/Users/dusty
test.txt
=> true
Cd6b438f1238ee36cf4daecbae1d3917?d=identicon&s=25 Thomas Preymesser (Guest)
on 2009-01-07 19:21
(Received via mailing list)
2009/1/7 Gu stav <gustav@vonsydow.tv>:
>
> I've tried using:
>
> system "cd /dir"
> system "ls"

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.

-Thomas
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-07 23:20
(Received via mailing list)
On 07.01.2009 19:08, dusty wrote:
>> 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 topic is locked and can not be replied to.