Reputation: 312
I want to execute a variable shell command through ruby. I am a ruby beginner. I had the command working without variables, but when I tried to set variables the exec command doesn't work and the script stops running. All 3 variables hold Strings.
The print is working fine, but exec not.
print "#{jobserv}" + options[:machines] + " -c touch #{noautooffPath} \n"
exec('"#{jobserv}" + options[:machines] + " -c touch #{noautooffPath}"')
What do i have to do to make the exec right or to get more hints by the system on how to correct this?
jobserv hold a path to an executable file plus options, options[:machines] holds a parameter I give to the last option flag of jobserv.
Upvotes: 0
Views: 374
Reputation: 4435
First, check you want exec
. Several other options, like system
, are more common choices for running a shell command. (Specifically, if exec
is successful, the Ruby script will stop executing and be replaced by the command it executes; the rest of the script will never be run.)
You seem to have an extra layer of single quotes in your exec call. You probably want something closer to this:
exec("#{jobserv}" + options[:machines] + " -c touch #{noautooffPath}")
(assuming the print
is showing the value you want)
In general, it's safer to avoid the single-string shell form, and pass all the argument individually. That might look more like this:
exec("#{jobserv}#{options[:machines]}", "-c", "touch", noautooffPath)
or:
exec(jobserv, options[:machines], "-c", "touch", noautooffPath)
(depending on whether jobserv
and options[:machines]
are expected to combine. And it'd look different again if any of those are expected to themselves contain a full multi-argument command invocation.)
Upvotes: 3