Skip to content
Advertisement

Linux Terminal Two Conditions to check

I need to check if node is running and if file /root/app/application.js exists and return corresponding message in terminal…

For checking if node is running or not i do this and this works:

if [ $(pgrep node | wc -l) -eq 0 ]; then echo "node is not running..."; else echo "node is running"; fi

Now i need to add to this to check also if file not exists /root/app/application.js so i found that this command is what i need:

if [ ! -f /root/app/application.js ]; then
    echo "File not found!"
fi

I try to add this two commands in one with the following code:

if [ $(pgrep node | wc -l) -eq 0 || ! -f /root/app/application.js]; then echo "node is not running..."; else echo "node is running"; fi

But i get error -bash: [: missing]’`

So how to combine two commands that check if node is running and if file not exists into one command?

Advertisement

Answer

if [ $(pgrep node | wc -l) -eq 0 -o ! -f /root/app/application.js ]; then echo "node is not running..."; else echo "node is running"; fi

If you need logical “or” in bash, you must use -o key. (Also, -a key if you need logical “and”)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement