0

I am new to shell scripting. Let me know how to write script for below senario.

A file named file1.txt contains some batch execution details and last 2 lines will contains success or failure of the job for the current date. Now I need to search the word success and followed by current date is there or not in last 2 lines of the file. if success with current date then set a variable $f1 with value 1 else 0. how can it do it?

sample last 2 lines of a file:

Trankey|BVCFG-B|989898|success|12/01/2016|01:34:07|12/01/2016|01:36:08
|file1.txt|ct32000001dev|transform

1 Answer 1

0

Quietly grep the last 2 lines of file1.txt for the current date.

tail -n 2 file1.txt | grep -q "success|$(date "+%d/%m/%Y")"

Set f1 depending on whether grep found the pattern (return code of the last executed command in $?):

if $?   ## could replace this with the pipe line above
then
    f1=1
else
    f1=0
fi

That last one is weird. You could probably negate the value in $? some way, but in more general terms, success in bash is 0, not 1. (The other way around to how it's in C, C++, Java etc.)

So, if you go with that semantic for f1, things become easier:

f1=$?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.