MLSC
MLSC

Reputation: 5972

If structure to compare result of two commands

I want to compare result of two commands in bash like so:

if `cat /root/pid` == `ps aux | grep "python" | grep -v grep | awk '{print $2}'`
then
   <SomeCommands>
else
   <OtherCommands>
fi

I want to check if the result of cat /root/pid and ps aux | grep "python" | grep -v grep | awk '{print $2}' is equal or not.

I also want to check if specific command is running in my server or not. This method I think works. Any other methods are welcome.

Thank you

Upvotes: 1

Views: 95

Answers (3)

Tiago Lopo
Tiago Lopo

Reputation: 7959

You can make it even shorter:

   if  [ "$(cat /root/pid)" == "$(pidof python)" ]; then
        <someCommands>
   else
        <otherCommands>
   fi

Upvotes: 1

user3442743
user3442743

Reputation:

if [[ "$(cat /root/pid)" == "$(ps aux | grep "python" | grep -v grep | awk '{print $2}')" ]]
then
<SomeCommands>
else
<OtherCommands>
fi

Your if command is syntactically incorrect as it does not contain [[ [ or (( brackets around it, also probably best to doublequote the commands so they are read as a whole string

Upvotes: 1

anubhava
anubhava

Reputation: 785286

You can use:

[[ "$(cat /root/pid)" == "$(ps aux | grep 'python' | grep -v grep | awk '{print $2}')" ]]

Though I believe you can cut down both grep and do all using awk itself.

Upvotes: 2

Related Questions