Jeremiah Lim
Jeremiah Lim

Reputation: 179

Bash compare string vs integer not working

i have a bash script as follows

#!/bin/bash
PROCESS=ps aux | grep [f]fmpeg -c

if [[ $PROCESS -gt 0 ]]
then
echo "process is more then 0"
fi

However, when i do run a check, even when the results found are more then 0, my if statement should be true but it is not be kicked off. I am not sure what is wrong with the comparison.

Upvotes: 0

Views: 155

Answers (4)

Landys
Landys

Reputation: 7727

To get the output from some command, you should quote with "`" as follows.

#!/bin/bash
PROCESS=`ps aux | grep [f]fmpeg -c`

if [[ $PROCESS -gt 0 ]]
then
    echo "process is more then 0"
fi

Upvotes: 0

folibis
folibis

Reputation: 12854

try this:

if [ "$PROCESS" -ne "0" ]

Upvotes: 0

Kalanidhi
Kalanidhi

Reputation: 5092

Back tic execute the command and the result assign to the PROCESS variable .
But you are checking number of process , so wc -l find the total number of line .

#!/bin/bash
PROCESS=`ps aux | grep [f]fmpeg -c | wc -l`

if [[ $PROCESS -gt 0 ]]
then
echo "process is more then 0"
fi

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249123

You want this:

if ps aux | grep [f]fmpeg
then
    ....

What this does is to skip the "if" by "failing" in the grep if there are no lines matched. If you really want to do the counting way, you can fix your original code like this:

PROCESS=$(ps aux | grep [f]fmpeg -c)

Upvotes: 1

Related Questions