简体   繁体   中英

Run shell script with parameter in Ruby

I know how to run a script from within Ruby. I usually use back ticks and they work fine. Recently we got request to feed one more argument to the script and problem happens here.

Originally, it looks like this: a global variable defined as:

SCRIPT = "/opt/customer/send_command.pl"

Then, we call

status =`"#{SCRIPT}" -c "ctl.pl qstatus"`
queue = `"#{SCRIPT}" -c "ctl.pl queue"`

Everything is fine. Now, there's one more parameter to feed in the script send_command.pl. So I changed global variable SCRIPT to:

SCRIPT = "/opt/customer/send_command.pl -p production"

And I use the same way to call that script:

status =`"#{SCRIPT}" -c "ctl.pl qstatus"`

But I got the error message:

sh: /opt/customer/send_command.pl -p production: not found

If I ditch SCRIPT variable, just call the script directly like:

status = `/opt/customer/send_command.pl -p production -c "ctl.pl qstatus"`

and it works as expected.

My question is why that happens and how to fix it? I guess I could just manually put -p production everywhere, but it's less ideal and very error prone.

It didn't work because you have extra double quotes. To fix it, remove them.

status =`#{SCRIPT} -c "ctl.pl qstatus"`

Either remove the double quotes:

from:

status =`"#{SCRIPT}" -c "ctl.pl qstatus"`

to

status =`#{SCRIPT} -c "ctl.pl qstatus"`

unless you want to try escaping them like:

status ="#{SCRIPT} -c \"ctl.pl qstatus\""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM