56

Is it possible to execute shell script in command line like this :

counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ] then; echo "true";
>

Above example is not working I get only > character not the result I'm trying to get, that is "true"

When I execute ps -ef | grep -c "myApplication I get 1 output. Is it possible to create result from single line in a script ? thank you

London
  • 14,476
  • 34
  • 103
  • 139

5 Answers5

89

It doesn't work because you missed out fi to end your if statement.

counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ]; then echo "true"; fi

You can shorten it further using:

if [ $(ps -ef | grep -c "myApplication") -eq 1 ]; then echo "true"; fi

Also, do take note the issue of ps -ef | grep ... matching itself as mentioned in @DigitalRoss' answer.

update

In fact, you can do one better by using pgrep:

if [ $(pgrep -c "myApplication") -eq 1 ]; then echo "true"; fi
Community
  • 1
  • 1
Shawn Chin
  • 79,172
  • 18
  • 156
  • 188
  • `if pgrep myApplication 2>/dev/null; then ...` No need for brackets or command substitution. The bracket around the "m" isn't needed either since pgrep doesn't match itself unless you tell it to. – Dennis Williamson Feb 13 '11 at 21:32
  • That won't be accurate if he wants to match exactly one instance. Good point about pgrep not matching self. Updated example. Thanks. – Shawn Chin Feb 13 '11 at 21:43
  • In the first code example, is the semi-color after `then` in the wrong place? – egrunin Nov 30 '12 at 16:58
  • `-c` for pgrep is not a valid switch at least for version 3.2.8 of procps. It's not working in my case... – Oktav Oct 15 '13 at 12:38
8

Other responses have addressed your syntax error, but I would strongly suggest you change the line to:

test $(ps -ef | grep -c myApplication) -eq 1 && echo true

If you are not trying to limit the number of occurrences to exactly 1 (eg, if you are merely trying to check for the output line myApplication and you expect it never to appear more than once) then just do:

ps -ef | grep myApplication > /dev/null && echo true

(If you need the variable counter set for later processing, neither of these solutions will be appropriate.)

Using short circuited && and || operators is often much clearer than embedding if/then constructs, especially in one-liners.

William Pursell
  • 190,037
  • 45
  • 260
  • 285
7

Yes, with syntax issues fixed

That almost worked. The correct syntax is:

counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ]; then echo "true"; fi

But note that in an expression of this sort involving ps and grep, the grep will usually match itself because the characters "grep -c Myapplication" show up in the ps listing. There are several ways around that, one of them is to grep for something like [M]yapplication.

DigitalRoss
  • 139,415
  • 24
  • 238
  • 326
2

I am using Mac OS and following worked very well

$ counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ]; then echo "true";fi;

true

Space is needed after [ and before ]

Matt Seymour
  • 8,316
  • 7
  • 56
  • 99
minhas23
  • 8,723
  • 3
  • 56
  • 39
0

I was struggling to combine both multiple lines feed into command and getting its results into a variable (not a file) and come up with this solution:

    FRA_PARAM="'db_recovery_file_dest'"
    FRA=$(
    sqlplus -S "/as sysdba" <<EOF
set echo off head off feed off newpage none pages 1000 lines 1000
select value from v\$parameter where name=$FRA_PARAM;
exit;
EOF
        )

Please note that single-quotes word was substituted, because otherwise I was receiving its autosubstitution to double quotes... ksh, HP-UX.

Hopefully this will be helpful for somebody else too.

Tagar
  • 12,247
  • 5
  • 84
  • 103