Bash syntax error from valid command via ruby

Hi,

OS: Ubuntu 14.04.1 LTS
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
ruby 2.0.0p384 (2014-01-12) [x86_64-linux-gnu]

$ grep ‘grep’ <(ps aux)
dave 17227 0.0 0.0 11744 652 pts/2 S+ 20:29 0:00 grep
[…]

This valid bash command [using bash process substitution** “<(cmd)”]
fails when trying to issue it through the usual ruby methods.
** http://tldp.org/LDP/abs/html/process-sub.html

I can work around it, easily, but can anyone identify the problem,
please?

$ ruby -e “%x{grep ‘grep’ <(ps aux)}”
sh: 1: Syntax error: “(” unexpected

$ ruby -e “system("grep ‘grep’ <(ps aux)")”
sh: 1: Syntax error: “(” unexpected

$ ruby -e “IO.popen("grep ‘grep’ <(ps aux)")”
sh: 1: Syntax error: “(” unexpected

$ ruby -e “Process.spawn("grep ‘grep’ <(ps aux)")”
sh: 1: Syntax error: “(” unexpected

$ ruby2.0 -e “%x{grep ‘grep’ <(ps aux)}”
sh: 1: Syntax error: “(” unexpected


Back to the old way, for now:
$ ruby -e “puts %x{ps aux | grep ‘grep’}”
dave 17876 0.0 0.0 30644 4860 pts/2 Sl+ 20:47 0:00 ruby -e
puts %x{ps aux | grep ‘grep’}
dave 17878 0.0 0.0 4444 636 pts/2 S+ 20:47 0:00 sh -c
ps aux | grep ‘grep’
dave 17881 0.0 0.0 11748 888 pts/2 S+ 20:47 0:00 grep
grep

daz

Seems I accidentally revealed the answer in my final example …

Ruby invokes sh -c (which is dash on my system - not bash).

$ sh -c “ps aux | grep ‘grep’”
dave 18746 0.0 0.0 4444 640 pts/2 S+ 21:12 0:00 sh -c
ps aux | grep ‘grep’
dave 18748 0.0 0.0 11748 888 pts/2 S+ 21:12 0:00 grep
grep

$ sh -c “grep ‘grep’ <(ps aux)”
sh: 1: Syntax error: “(” unexpected

Guess I should link “sh” to bash, or something.

Thanks.

daz

It surprised me that ruby already knows what shell I want …

$ ruby -e “p ENV[‘SHELL’]”
“/bin/bash”

… I was hoping I could just change that in my script because globally
changing “sh” to use bash would slow down the system (that’s why Ubuntu
changed the default).

daz

Dave Butcher wrote in post #1167185:

$ grep ‘grep’ <(ps aux)
dave 17227 0.0 0.0 11744 652 pts/2 S+ 20:29 0:00 grep
[…]

You can do

processes=%x{pgrep grep}.scan(/\d+/)

unless processes.empty?
$cmd=processes.join ’ ’
result=%x{ps aux #$cmd}
end

But do you want to accomplish?

Kind regards

robert

On my system, that would be %x{ps aux -p #$cmd} following pgrep.

But I just found %x{ps -C procxxx} which gives the right result if the
process name is known.

In reality, I might need to grep for (e.g.) “/usr/bin/procxxx args” in
the startup command.

Thanks, anyway. It was the syntax error I was puzzling about.

daz