-2

I try to convert one command line output into a (or store into a ) variable. The command is

ps -U root u | grep ruby | wc -l

The output is 1, but when I use

$(ps -U root u | grep ruby | wc -l)

the output is

1: command not found

What happens here? Here is my snapshot enter image description here

enter image description here

tripleee
  • 175,061
  • 34
  • 275
  • 318
Jack Wang
  • 77
  • 1
  • 7
  • I rolled back your latest edit. If you want to ask a new question, *ask* a new question. You might want to add a comment here with a link to the new question if it's related (though in this case it isn't really). In the meantime, please accept one of the answers here, so that this question no longer comes up as unresolved. – tripleee Dec 15 '15 at 15:40
  • Though before you post anything more, please review the [`bash` tag wiki](http://stackoverflow.com/tags/bash/info) for common beginner problems and troubleshooting tips. – tripleee Dec 15 '15 at 15:43
  • Possible duplicate of http://stackoverflow.com/questions/4651437/how-to-set-a-bash-variable-equal-to-the-output-from-a-command – tripleee Dec 15 '15 at 15:44
  • OK.. is that possible to display my update question. the question above is kind of worthless to me. – Jack Wang Dec 15 '15 at 15:57

3 Answers3

2

Here,

$(ps -U root u | grep ruby | wc -l)

You are not saving the output into a variable. So the shell attempts to execute the result (which happens to be 1 in your case). Since it couldn't find a command/function named 1, you get the error.

You probably want:

output=$(ps -U root u | grep ruby | wc -l)

Now, output will have 1 which you can print with:

echo "${output}"

Btw, grep can count itself using the -c option. So wc is unnecessary here:

output=$(ps -U root u | grep -c ruby)
echo "${output}"
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Yeah..I try the second way, actually I still got 1: command not found when I echo the $output – Jack Wang Dec 15 '15 at 15:05
  • @JackWang I don't believe you ;-) Did you really have `output=$(ps -U root u | grep ruby | wc -l)` and `echo "${output}"` ? – P.P Dec 15 '15 at 15:07
  • Hi, could you please checkout my update? Thank you so much. – Jack Wang Dec 15 '15 at 15:22
  • 1
    @JackWang You seem to be using bash features such as `[[` and paramter expansion and I don't see a *sebhang*. Probably you are executing it using old sh, which doesn't recognize this. Add `#!/bin/bash` as the first line of your script and run. Or run as `bash changename.sh`. – P.P Dec 15 '15 at 15:26
  • Good call. That is not my top of code. I have some junk comment on the top so that it's not showed. But when I check the top again, I found myself somehow delete the sebhang.... I didn't pay attention to it...Thank you! A further question is why it looks fine when I use ./changename.sh but something happens when I use nohup? – Jack Wang Dec 15 '15 at 15:43
1

In the latter case, the command inside the $(…) is evaluated and the result is then used to create a new command, which the shell then tries to execute. Since there is no command or program named 1, you get the message you are seeing. It easier to see what's happening if you writer echo Result: $(ps -U root u | grep ruby | wc -l). your output would be Result: 1.

tarleb
  • 19,863
  • 4
  • 51
  • 80
1

To assign to a variable do it like this, using backtics

a=`ps -U root u | grep ruby | wc -l`  
Nuetrino
  • 1,099
  • 1
  • 14
  • 32