0

I want to pass a command and get the answer printed. I have tried this

#!/bin/sh
cmd = `ps -ef|grep processname`
echo "$cmd"

But i am getting the error as

: command not found 4: cmd

the output of this command may be 5 or 6 lines. I just want to view them.

Srini V
  • 11,045
  • 14
  • 66
  • 89
Anna
  • 1
  • 2
  • check this https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-from-a-command-in-bash – LuckyAshnar Nov 01 '17 at 12:51

2 Answers2

0

Remove extra spaces you have either side of the equals symbol.

cmd=`ps -ef|grep processname`
  • what if $cmd does not give an output.How to write a condition. If $cmd gives an output and what if it does not – Anna Nov 06 '17 at 05:52
0

try switching to bash, rather than sh

cmd="$(ps -ef|grep apache)"
echo "$cmd"

yields

bob@cindy:~$ echo "$cmd"
root      1914     1  0 Sep08 ?        00:01:37 /usr/sbin/apache2 -k start
www-data  1990     1  0 Sep08 ?        00:01:09 /usr/bin/htcacheclean -d 120 -p /var/cache/apache2/mod_cache_disk -l 300M -n
bob 13511 13509  0 08:18 pts/0    00:00:00 grep apache
www-data 29841  1914  0 00:08 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 29842  1914  0 00:08 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 29843  1914  0 00:08 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 29844  1914  0 00:08 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 29845  1914  0 00:08 ?        00:00:00 /usr/sbin/apache2 -k start
BozoJoe
  • 6,117
  • 4
  • 44
  • 66
LuckyAshnar
  • 355
  • 2
  • 12
  • what if $cmd does not give an output.How to write a condition. If $cmd gives an output and what if it does not. – Anna Nov 03 '17 at 11:53