Reputation: 179
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
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
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
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