1
#!/bin/bash
mdate="$(date | awk '{print $2$3}')";  
cd /var/tmp/precheck;  
found="$(ls -l *.txt | awk '{print $6$7}')"  
for txtdate in $found  
do  
if [ $mdate = $txtdate ]; then  
echo "Pre-Check Success"  
else  
echo "Pre-Check FAILURE"  
fi  
done

In the above script, .txt files verified with current date. If the date stamp matches, it returns SUCCESS else FAILURE..

Here, it works for me except it returns condition for all files. I need only one condition to be returned: either SUCCESS or FAILURE.

If any one of the files not matches with current date stamp, it should return one failure condition. If all matches it should return only one SUCCESS condition.

Manoj.V
  • 37
  • 8
  • 1
    Introduce a flag called DATE_FAIL and upon any entry of your else branch set it to 1 and exit early. Check this flag upon exit from the loop. –  Sep 14 '17 at 16:31
  • Also see: https://stackoverflow.com/q/2237080/1531971 and https://stackoverflow.com/q/18488651/1531971 –  Sep 14 '17 at 16:33
  • 1
    Please try and improve your title, at the moment it is very vague. – Tom Fenech Sep 14 '17 at 16:34

3 Answers3

1

Set a variable before the loop, then if you find a failure change the variable and exit the loop.

status=Success
for txtdate in $found
do
    if [ $mdate != $txtdate ]; then
        status=FAILURE
        break
    fi
done
echo Pre-Check $status
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You need to store previous file result

#!/bin/bash
mdate="$(date | awk '{print $2$3}')";  
cd /var/tmp/precheck;  
found="$(ls -l *.txt | awk '{print $6$7}')"  
result="SUCCESS"
for txtdate in $found  
do  
if [ $mdate != $txtdate ]; then  
result="FAILURE"  
fi  
done
echo "Pre-Check " $result
mrq
  • 460
  • 2
  • 12
0

Let's try a completely different approach:

if find /var/tmp/precheck -name '*.txt' -mtime +0 | grep -q .; then 
    status="FAILURE"
fi

printf 'Pre-Check %s\n' "${status:-Success}"

If any files are found that were modified more than one day ago, then set status to "FAILURE". Print the status, defaulting to "Success". grep -q . returns "success" and quits as soon as it receives any input.

Problems in your existing approach have already been highlighted elsewhere - if you output inside the loop then you're going to end up with one line per file.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Thank you Tom!!.. Simple and Powerful.... As I'm beginner, usually works with "then" and "else" in if statement.. Here, no "else", something new to me to understand.. How its printing default "Success"? Also, how do I get .txt files in the display for failure ones.. ? – Manoj.V Sep 14 '17 at 19:01
  • @Manoj.V the parameter expansion uses `:-Success` to provide a default value. You can remove the pipe to grep if you want to see the files that are found. – Tom Fenech Sep 14 '17 at 19:07
  • I'm using function for INFO, ERR & CRITICAL... Function name is 'log'.. and the usage is "log info", "log err". Here, if success returns, I should use "log info" and "log err" for failure. How to I integrate the function in the method you suggested? – Manoj.V Sep 14 '17 at 20:13
  • I guess you write `log err "Pre-Check FAILURE"` in the `if` branch and `log info "Pre-Check Success"` in the `else` branch, but this requirement doesn't really have anything to do with your original question. – Tom Fenech Sep 14 '17 at 21:00
  • Right, I write in the same way.. Since else branch is not there and the approach is different, I'm confused to call the function "log" here.. Can you please help on this as well? ☺... – Manoj.V Sep 14 '17 at 21:21