0

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.

4 Answers 4

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Your codes return a 1 even though the current process is 0
@user3148070,I executed it working fine.Because none of the process executed ,ps aux | grep [f]fmpeg -c return no line then wc -l return 0 .
0

try this:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.