[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Multiple commands in shell

Gu stav

1/7/2009 4:15:00 PM

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!
--
Posted via http://www.ruby-....

7 Answers

Sebastian Hungerecker

1/7/2009 4:24:00 PM

0

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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Jan-Erik R.

1/7/2009 4:29:00 PM

0

Gu stav schrieb:
> 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!
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...
and
http://www.ruby-doc.org/core/classe...

Gu stav

1/7/2009 6:08:00 PM

0

Works great. Thanks a million!
--
Posted via http://www.ruby-....

dusty

1/7/2009 6:08:00 PM

0

On Jan 7, 11:14 am, Gu stav <gus...@vonsydow.tv> wrote:
> 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!
> --
> Posted viahttp://www.ruby-....


[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

Thomas Preymesser

1/7/2009 6:20:00 PM

0

2009/1/7 Gu stav <gustav@vonsydow.tv>:
> 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"

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

--
Thomas Preymesser
thopre@gmail.com
http://thopre.google...
http://thopre.word...

Robert Klemme

1/7/2009 10:19:00 PM

0

On 07.01.2009 19:08, dusty wrote:
> On Jan 7, 11:14 am, Gu stav <gus...@vonsydow.tv> wrote:
>> 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!
>> --
>> Posted viahttp://www.ruby-....
>
>
> [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

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

--
remember.guy do |as, often| as.you_can - without end

Randy G

1/24/2011 3:09:00 PM

0

On Jan 23, 9:02 pm, band beyond description
<shadowboxing....@apocalypse.com> wrote:
> "When we're done with it, it's yours -- for $450!!!"

*like*