0

I have the following problem: I try to send mails with different content and to different recipients, when the following conditions are met:

  • a specific string in a logfile is NOT found and a specific file exists
  • a specific string in a logfile is NOT found and a specific file does NOT exist
  • a specific string in a logfile is found and a specific file exists
  • a specific string in a logfile is found and a specific file does NOT exist

The specific string is found in the newest logfile, the specific file was or was not created in a job prior to this one. The problem is: it worked when put in a .sh file and starting it via command line in putty, but since I transfered it to UC4, it doesn´t work. It keeps choosing wrong options.

Here´s my code (simplified, without the email part)

if [ ! grep -q 'errors: 0' "/mypath/$(ls -t /mypath/ | head -n1)" ] && test -e "/mypath2/outtakes.csv"; then
        
        echo 'error + filtered data'

elif [ ! grep -q 'errors: 0' "/mypath/$(ls -t /mypath/ | head -n1)" ] && ! test -e "/mypath2/outtakes.csv"; then
        
        echo 'error + no filtered data'


elif [ grep -q 'errors: 0' "/mypath/$(ls -t /mypath/ | head -n1)" ] && test -e "mypath2/outtakes.csv"; then

        echo 'no errors + filtered data'


else
        
        echo 'no errors + no filtered data'

fi

I tried to change brackets, test conditions individually and individually, they worked. It must have to do with the chain of conditions.

mjsqu
  • 5,151
  • 1
  • 17
  • 21
Asujia
  • 1
  • 1

1 Answers1

0

if checks the return status of the command. [ is a command. The expression if [ ! grep -q 'errors: 0' ... does not make much sense; it executes [ with the arguments !, grep, etc. It does not execute grep. If you want to check the return status of grep, just do:

if ! grep ....

This will execute grep and branch based on its return value. Because of the !, the if branch is entered if grep returns non-zero.

William Pursell
  • 204,365
  • 48
  • 270
  • 300