Meeshoo Meeshoo - 7 months ago 15
Ruby Question

How to call an external script with arguments?

I have a Ruby script that should call various scripts when the execution throws an error. How can I call a script with two arguments?

For the moment I am using the code below but I need to also send two arguments to script.sh:

my_script = "script.sh"
%x( #{my_script})

Answer

You almost figured it out, just add the params:

my_script = "script.sh"
%x( #{my_script} #{arg1} #{arg2})

You may also want to check the return code:

if $? == 0
   puts "script executed successfully"
else
   puts "script returned an error, exit code: #{$?.to_s.split('exit ')[-1]}"
end