0

I am executing a system command with ruby. The code is like this

commandtoexecute=“pararellrspec --type rspec -n 1 --test-options '--test "cases's owner"' testing/multi/getcreate.rb “

system(commandtoexecute)

On executing the above line it gives error

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file

But when I execute by replacing cases's with casess above it works. Does any one has any idea how to prevent this error above without replacing "cases's" with "casess"

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
user1788294
  • 1,823
  • 4
  • 24
  • 30

3 Answers3

1

You need to escape your double-quotes:

"pararellrspec --type rspec -n 1 --test-options '--test \"cases's owner\"' testing/multi/getcreate.rb"

Edit: possibly that line single quote as well, come to think of it:

"pararellrspec --type rspec -n 1 --test-options '--test \"cases\'s owner\"' testing/multi/getcreate.rb"

Or perhaps remove the single quotes instead of that if works as expected:

"pararellrspec --type rspec -n 1 --test-options --test \"cases's owner\" testing/multi/getcreate.rb"

Alternatively (and better yet), use the array flavor of system when appropriate, so as to have Ruby take care of quoting arguments for you:

system("echo *")
system("echo", "*")

http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-system

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
0
commandtoexecute="pararellrspec --type rspec -n 1 --test-options '--test "cases's owner"' testing/multi/getcreate.rb"
system(commandtoexecute)

as you can see you have an escaping issue with " and ' within your command string. to circumvent this issue, you can use

%x(ruby --copyright)
> "ruby - Copyright (C) 1993-2013 Yukihiro Matsumoto\n"

to execute the shell command within ruby. This shorthand method takes care of any necessary escaping. So your original code comes down to:

%x(pararellrspec --type rspec -n 1 --test-options '--test "cases's owner"' testing/multi/getcreate.rb)

as you can see, because you dont need " to create a string that holds your command (as you did above for commandtoexecute) the escaping issue is gone.

Sascha Kaestle
  • 1,293
  • 12
  • 15
  • I didn't get it ... should the command be like system(%x(commandtoexecute)) – user1788294 Apr 26 '14 at 12:00
  • no, something like: `%x(pararellrspec --type rspec -n 1 --test-options --test "cases's owner" testing/multi/getcreate.rb)` this will execute the command as a system command, and should take care of correct escaping – Sascha Kaestle Apr 26 '14 at 12:01
  • sry unable to understand this. Tried '%x(commandtoexecute)' and 'system(%x(commandtoexecute))' – user1788294 Apr 26 '14 at 12:05
0

Not sure please try like this:-

1.9.3p448 :025 > commandtoexecute="pararellrspec --type rspec -n 1 --test-options '--test 'cases\'s owner' testing/multi/getcreate.rb"

  => "pararellrspec --type rspec -n 1 --test-options '--test 'cases's owner' testing/multi/getcreate.rb"

1.9.3p448 :026 > system(commandtoexecute)

this will work please try it.

Please click more details.

Community
  • 1
  • 1
Jenorish
  • 1,694
  • 14
  • 19