mercredi 24 juillet 2019

Or statement in bash with grep

I am writing a bash script that cycles through a list of files and does something either when the file contains a certain line, or the file was created within the last X seconds. The script works fine with only the first requirement

if grep -q $error $f; then

, but I don't know how to include the "or" statement.

I have tried the following ( I am new to bash so I tried things that may not even make sense ).

FILES=logdir/*.out
OLDTIME=86400
CURTIME=$(date +%s)
error='exit'
for f in $FILES
do
    FILETIME=$(stat $f -c %Y)
    TIMEDIFF=$(expr $CURTIME - $FILETIME)
    if [[ grep -q $error $f ]] || [[$OLDTIME -gt $TIMEDIFF ]] ; then

or

if [[ grep -q $error $f || $OLDTIME -gt $TIMEDIFF ]] ; then

gives me

./script.sh: line 12: conditional binary operator expected
./script.sh: line 12: syntax error near `-q'

and

if [ grep -q $error $f ] || [ $OLDTIME -gt $TIMEDIFF ] ; then

gives me

./script.sh: line 12: [: too many arguments
./script.sh: line 12: [: 1: unary operator expected

How can I have both conditions in the if statement?

Aucun commentaire:

Enregistrer un commentaire