Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EASIER WAY TO CHECK FILE SUCCESS

Status
Not open for further replies.

hubud

Instructor
Oct 18, 2002
123
GB
The following script checks the last line of a file to see if it has finished the log file or not. It then reports this a part of a simple report. I can't figure out why it's not working:

#find the last file in the directory and remove the .log suffix, for the report
current=`ls -rt1 | tail -1 | cut -d. -f1`

#put the .log suffix back on for following processing
current2=`tail -1 $current.log`


grep "successfully" $current2 > /dev/null 2>&1

if [ $? = 0 ]
then
lastfile=" has completed"
fi

grep "Session run terminated" $current2 > /dev/null 2>&1

if [ $? = 0 ]
then
lastfile=" was terminated by user"
else

lastfile=" is still running"
fi

Depending on the last line of the file the variable $lastfile will return a different value. Why doesn't this work?
Also some files when still running have a blank line on the last line. How do you check for this..is it like "^$" ?


cheers

simmo
 
Grep takes a File ( or STDIN ).

Given the way your script is coded the last line of $current.log contains the name of the file you want to grep for the word "successfully".


If $current2 is actually the TEXT from the last line then you need to code it like

echo $current2 | grep "successfully" > /dev/null 2>&1

Also you probably need an ELSE ( or an exit ) since even if the first IF sets

$lastfile = "has completed"

the script will fall down into the second if and set

$lastfile = "Still running"


Putting these changes together you get.

echo $current2 | grep "successfully" > /dev/null 2>&1

if [ $? = 0 ]
then
lastfile=" has completed"
else

echo $current2 | grep "Session run terminated" > /dev/null 2>&1

if [ $? = 0 ]
then
lastfile=" was terminated by user"
else

lastfile=" is still running"
fi
fi



 
cheers tdatgod,

I've stuck that in and it's working. I'm not sure what you mean by

the script will fall down into the second if and set

$lastfile = "Still running"
If the first test fails it tries the second grep, if that fails the only option the file can be is still running. However your observations seem to pan out as that was the response I was getting. Can you clarify, I must be missing the obvious for looking at it.

The point about grep requiring a file or stdin. The $current.log was a file name so it should have picked it up in the grep. Not sure it didn't.

cheers

simmo
 
Hi again,
Scratch the last, I've got it now

cheers

simmo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top