0

I am getting the output like this:

ps -ef | grep xyz | grep -v grep | grep -v ruby |  awk '{print $8}'

as xyz

but when I am assign this value to the variable and print the variable then I am getting some weird output like this-

string='ps -ef | grep xyz | grep -v grep | grep -v ruby |  awk '{print $8}''
echo $string

Output:

output= sudo sh sh xyz

How can I just get the output as xyz

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • I'm not sure how you are even getting that output. You assign a string and output the string. But, I think what you actually want is `string="$(ps -ef | grep xyz | grep -v grep | grep -v ruby | awk '{print $8}')"`. Which is sort of what your output looks like. If you are getting multiple results, then the string `xyz` exists in all of those lines. I'm not sure if this is what you are looking for, but one more `| grep xyz` would give you what you want... – Jason May 25 '18 at 18:56
  • @Jason, It did not make any difference. Still getting the same output- – Khanal Bhaskar May 25 '18 at 19:01
  • Okay, try just `ps -ef | grep xyz | grep -v grep | grep -v ruby` and see what you get. That should clear things up for you. However many lines of output you get is how much output you will get after the awk call. – Jason May 25 '18 at 19:02
  • This is what I get if I run the command without print line- deploy 32562 1 0 03:13 ? 00:03:19 xyz 3.2.5 yzx [0 of 40 busy] – Khanal Bhaskar May 25 '18 at 19:28
  • The command `echo "deploy 32562 1 0 03:13 ? 00:03:19 xyz 3.2.5 yzx [0 of 40 busy" | awk '{print $8}'` outputs `xyz`... Try my command in my original comment. The quotes around the command substitution may be important here. Also `echo $0` to make sure you are using bash... – Jason May 25 '18 at 20:00
  • you can use : STRING=$(ps -ef |grep xyz | grep -v grep | grep -v ruby | awk '{print $8}' ) and then for print use : printf "%s\n" "$TTTT" – Elham_Jahani May 27 '18 at 06:34
  • please dont copy from here and try to type into your script (because of character set) – Elham_Jahani May 27 '18 at 06:35

0 Answers0