Dejell
Dejell

Reputation: 14317

rake - running shell command returns error

I am trying to run in Rake the following shell command:

sh "d='jps -l | grep jar | cut -d ' ' -f 1'; if [ -z \"$d\" ]; then :; else kill  \"$d\"; fi;"

However I get:

sh:  -f 1: not found

If I run it in linux shell it works fine.

What is wrong?

Upvotes: 0

Views: 233

Answers (2)

Kulbir Saini
Kulbir Saini

Reputation: 3915

I interpreted your question wrong earlier. This is what you want.

d='jps -l | grep jar | cut -d " " -f 1; if [ -z "$d" ]; then :; else kill "$d"; fi;'
system(d)

OR

If you want output of the command (which I guess you don't in this case)

output = `jps -l | grep jar | cut -d " " -f 1; if [ -z "$d" ]; then :; else kill "$d"; fi;`

Upvotes: 1

Thilo
Thilo

Reputation: 17735

You need to escape your single quotes and quote the whole string:

d='jps -l | grep jar | cut -d \' \' -f 1; if [ -z "$d" ]; then :; else kill  "$d"; fi;'

Upvotes: 0

Related Questions