which shell?

W

Wybo Dekker

It looks like Kernel#system uses sh, not bash, even though I told linux to use bash:

#!/usr/bin/env ruby
system("echo $SHELL")
system("ls -d /{etc,sbin}")

=>

/bin/bash
ls: cannot access /{etc,sbin}: No such file or directory

How can I tel system to use bash?
 
R

Robert Klemme

It looks like Kernel#system uses sh, not bash, even though I told linux to use bash:

#!/usr/bin/env ruby
system("echo $SHELL")
system("ls -d /{etc,sbin}")

=>

/bin/bash
ls: cannot access /{etc,sbin}: No such file or directory

How can I tel system to use bash?

If I would want to enforce a particular shell I would always explicit
invoke it and not rely on the environment shell set appropriately (your
script will otherwise break immediately if someone uses a different
shell). Here's one way to do it:

robert@fussel:~$ ruby19 -e "system('bash','-c','ls -d /{etc,sbin}')"
/etc /sbin

But you don't really need a shell for this. In your case you could as
well do this:

robert@fussel:~$ ruby19 -e 'p Dir["/{etc,sbin}"]'
["/etc", "/sbin"]

or even

robert@fussel:~$ ruby19 -e 'p %w{/etc /sbin}.select{|d| test ?d, d}'
["/etc", "/sbin"]

or more verbose

robert@fussel:~$ ruby19 -e 'p %w{/etc /sbin}.select{|d| File.directory? d}'
["/etc", "/sbin"]

....

I can't really think of a reason to fork a particular shell from a Ruby
script other than for executing an existing shell script (in which case
it is taken care of automatically provided you have the proper shebang
line). All other tasks are probably done more efficiently from inside
the interpreter.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top