David542
David542

Reputation: 110382

Convert shell statement into OR

How would I do the following OR statement with the proper syntax:

if ps aux | (grep "[i]ngest_all.py" OR grep "[h]ello.py" ) > /dev/null
then
    echo "Script is already running. Skipping"
else
    exec "$DIR/ingest_all.py"
fi

Upvotes: 2

Views: 26

Answers (2)

Rob
Rob

Reputation: 3746

You can use an alternator (pipe character: |) to specify an OR inside of the grep regex.

For BSD grep, with basic regular expressions, (at least on OS X) use an escaped pipe: \|:

grep "[i]ngest_all.py\|[h]ello.py"

If you want you can also use extended regular expressions, where you don't need to escape the pipe character:

grep -E "[i]ngest_all.py|[h]ello.py"

or

egrep "[i]ngest_all.py|[h]ello.py"

Upvotes: 4

hek2mgl
hek2mgl

Reputation: 158110

You can use awk:

if ps aux | awk '/[i]ngest_all.py/||/[h]ello.py/{exit 0}END{exit 1}'

Upvotes: 1

Related Questions