The Crystal Programming Language Forum

Pipes Breaking With System Function

I found some instructions on here yesterday for using this system function in this thread.

I was pleased to see the person in this example was able to use multiple pipes without an issue. Unfortunately I did not have the same luck. Here is some code I tried:

Process.run("sshpass -p \\"password\\" ssh -o StrictHostKeyChecking=no -T user@192.168.x.x \"ps faux | awk '/mysql/ { print $2 }'\\"", shell: true) do |proc|
  IO.copy(proc.output, STDOUT)
end

This outputs the following:

./system 
user     1114  0.0  0.1   8488  2656 ?        Ss   20:42   0:00          \_ bash -c ps faux | awk '/mysql/ { print  }'
user     1116  0.0  0.1  10428  2652 ?        S    20:42   0:00              \_ awk /mysql/ { print  }
mysql      435  0.0  3.1 729344 62924 ?        Ssl  19:35   0:03 /usr/sbin/mysqld

If you know anything about Bash, what’s after the pipe is not being executed. And if I try running something with more than one pipe outputs there’s no output at all. I tried this on both Debian and Arch Linux with the same results. Did something change with this function? Or is there another way to do this?

Thanks

It looks like just the $2 gets lost? Did you try escaping it?

Yep, that worked. A little embarrassed I didn’t see that, but I’ll blame that on it being late and me being tired. Thank you!