How to run script file using system command

Hi,
I am trying to run a script file using system command, but it is not
working.

Code… test.rb

abc = system(". /export/home/dhiraj/scriptfile.sh")
puts abc

The value of abc is false.

Please tell me why script file is not running?

Dhiraj

On 26 Apr 2007, at 15:04, Dhiraj G. wrote:

Please tell me why script file is not running?

If that is really what you ran then you have a space in your command.

Alex G.

Bioinformatics Center
Kyoto University

Alex G. wrote:

On 26 Apr 2007, at 15:04, Dhiraj G. wrote:

Please tell me why script file is not running?

If that is really what you ran then you have a space in your command.

Alex G.

Bioinformatics Center
Kyoto University

Please tell me if i have a script file say scriptfile, then how i can
run that file using ruby.
Please explaain with example.

Dhiraj

Hi,

Please see below.

On 4/26/07, Dhiraj G. [email protected] wrote:

Bioinformatics Center
Kyoto University

Please tell me if i have a script file say scriptfile, then how i can
run that file using ruby.
Please explaain with example.

(assuming Unixish system)
bash$ cat > t
p “hello world!”
Ctrl.d (Pressing Ctrl and d key)

bash$ ruby -e ‘system(“ruby”, “t”)’
hello world!
bash$

Dhiraj


Posted via http://www.ruby-forum.com/.


अभिजीत

[ written in http://www.paahijen.com/scratchpad ]

[ http://www.paahijen.com ]

Alex G. wrote:

On 26 Apr 2007, at 15:33, Dhiraj G. wrote:

Bioinformatics Center
Kyoto University

Please tell me if i have a script file say scriptfile, then how i can
run that file using ruby.
Please explaain with example.

I was a bit terse with my previous answer. I was just pointing out
that you had a space between the initial ‘.’ and the rest of your
command which would cause system() to fail. Here is an example of how
to run a script using system():

[alexg@powerbook]/Users/alexg(5): cat test.sh
#!/bin/sh

echo ‘foo’
[alexg@powerbook]/Users/alexg(6): ./test.sh
foo
[alexg@powerbook]/Users/alexg(7): ruby -e “system(’./test.sh’)”
foo

Alex G.

Bioinformatics Center
Kyoto University

I tried above in following manner, but i got following error.

  1. I created one script file name :sample
    with following data
    #!/bin/sh

echo ‘foo’

Then i created a test.rb file with following code

abc = system("./sample")
puts $?
puts abc

Note: Both files are in same folder.

when i executed the test file with following command

ruby test.rb

I got following result

test.rb:2: warning: Insecure world writable dir
/opt/oracle/product/10.2.0/client_1/bin, mode 040777
32512
false

Please try the same.
OS : Solaris
Ruby Version: 1.8.5

Dhiraj

On 26 Apr 2007, at 15:33, Dhiraj G. wrote:

Bioinformatics Center
Kyoto University

Please tell me if i have a script file say scriptfile, then how i can
run that file using ruby.
Please explaain with example.

I was a bit terse with my previous answer. I was just pointing out
that you had a space between the initial ‘.’ and the rest of your
command which would cause system() to fail. Here is an example of how
to run a script using system():

[alexg@powerbook]/Users/alexg(5): cat test.sh
#!/bin/sh

echo ‘foo’
[alexg@powerbook]/Users/alexg(6): ./test.sh
foo
[alexg@powerbook]/Users/alexg(7): ruby -e “system(’./test.sh’)”
foo

Alex G.

Bioinformatics Center
Kyoto University

On Apr 26, 2007, at 2:04 AM, Dhiraj G. wrote:

Hi,
I am trying to run a script file using system command, but it is not
working.

Code… test.rb

abc = system(". /export/home/dhiraj/scriptfile.sh")
puts abc
The problem is right here. system doesn’t need a puts to be used, so
when you try to puts the value of a block (a block of code, and the
block is abc = system("./export/home/dhiraj/scriptfile.sh")), you get
false. It doesn’t have a value. You can just have it say:

system("./export/home/dhiraj/scriptfile.sh")

and it will work.

If this made any sense,
HTH

-------------------------------------------------------|
~ Ari
crap my sig won’t fit

On 26 Apr 2007, at 15:59, Dhiraj G. wrote:

foo
with following data
Note: Both files are in same folder.
false

Please try the same.
OS : Solaris
Ruby Version: 1.8.5

Sorry, I don’t have access to a Solaris machine so I can’t reproduce
the error. And I have no idea what a 32512 error code means. Your
code looks fine to me, so I’m not sure what the problem could be.

Good luck.

Alex G.

Bioinformatics Center
Kyoto University

On Behalf Of Dhiraj G.:

abc = system(". /export/home/dhiraj/scriptfile.sh")

puts abc

The value of abc is false.

irb(main):001:0> system “cat ./test1”
echo hello
=> true
irb(main):002:0> system “./test1”
=> false
irb(main):003:0> system “. ./test1”
=> false
irb(main):004:0> system “bash . ./test1”
.: .: is a directory
=> false
irb(main):005:0> system “bash ‘. ./test1’”
bash: . ./test1: No such file or directory
=> false
irb(main):006:0> system “bash -c ‘. ./test1’”
hello
=> true
irb(main):007:0> system “bash -c ‘. ./testx’”
bash: ./testx: No such file or directory
=> false
irb(main):008:0>
irb(main):009:0* system “bash ./test1”
hello
=> true
irb(main):011:0> system “ls test*”
test1 test2.rb test.rb test.txt
=> true

Please tell me why script file is not running?

you’ll have to test it, i’m not sure if system() spawns a shell;
but we’ll have to confirm from Nobu :slight_smile:

kind regards -botp

On Apr 26, 2007, at 7:29 AM, Dhiraj G. wrote:

Here variable abc is ued to collect the return value of system command
as
system command returns some value.

Return value of system states that whether the command executed
successfully or not.

Ohhhhhhh! Well now THAT makes sense. In that case, i retract my
comment and fully commit myself to ruby indulgence and education.

Pshhh, not like I already haven’t/
---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it” --1337est
man alive

Ari B. wrote:

On Apr 26, 2007, at 2:04 AM, Dhiraj G. wrote:

Hi,
I am trying to run a script file using system command, but it is not
working.

Code… test.rb

abc = system(". /export/home/dhiraj/scriptfile.sh")
puts abc
The problem is right here. system doesn’t need a puts to be used, so
when you try to puts the value of a block (a block of code, and the
block is abc = system("./export/home/dhiraj/scriptfile.sh")), you get
false. It doesn’t have a value. You can just have it say:

system("./export/home/dhiraj/scriptfile.sh")

and it will work.

If this made any sense,
HTH

-------------------------------------------------------|
~ Ari
crap my sig won’t fit

Hello Ari

Here variable abc is ued to collect the return value of system command
as
system command returns some value.

Return value of system states that whether the command executed
successfully or not.

Dhiraj