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.

3
  • 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. Commented Sep 14, 2017 at 16:31
  • Also see: stackoverflow.com/q/2237080/1531971 and stackoverflow.com/q/18488651/1531971 Commented Sep 14, 2017 at 16:33
  • 1
    Please try and improve your title, at the moment it is very vague. Commented Sep 14, 2017 at 16:34

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

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 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.
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?
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.
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? ☺...
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.