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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

greping the latest file

Status
Not open for further replies.

Bungheid

Technical User
Nov 14, 2002
16
0
0
US
I am trying to write a shell script that calls a number of shell scripts in order but checks thir logs to make sure that they completed OK before kicking off the next one, any suggestions on how to do this? The log files have the same name abart from date/time, which is created by the seperate scripts I call, I was thinking that I could grep the latest updated file but was not sure how to do this, thanks
 
Why not code the called scripts to return a value, traditionally 0 for success, non zero for failure

then
Code:
#!/bin/sh

script1
if [ $? -ne 0 ]
then
  echo script1 failed
  exit
fi

script2
if [ $? -ne 0 ]
then
  echo script2 failed
  exit
fi

...
...

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
If no other files are being written to the directory that the log files are being written to, I would use a command like this:
Code:
ls -1tr | tail -n1 | grep [i]"search string"[/i]

You could also add the first part of the filename after the ls command like this:

Code:
ls -1tr [i]filename*[/i] | tail -n1 | grep [i]"search string"[/i]

The ls command is how the latest file is discovered - the "t" flag orders by time, the "r" flag reverses the listing so you get the latest file at the bottom, and the "1" flag makes sure to only list the name of the file.

This is piped to the tail -n1 command. The "n1" flag only gets the last file.

This file is piped to grep - just give it the string you need to search for.

This way there can me many files for each script, but you always get the last one created for that script.

Hope this is what you were looking for...
 
Brian,

please excuse my interfering, but somebodey should tell you that your solution won't work.
You would have noticed, if you had tried yourself.

The first part of your explanation is correct,
ls -1tr | tail -n1
will give you the latest file in your current directory, that is, the filename.

But then:
This file is piped to grep
That's wrong.
grep won't see the contents of this file.
Only the last line of ls command is piped to grep.

I don't say that your whole idea is bad.
It can be done, by a command like this:
Code:
grep "search string" `ls -rt | tail -1`

For the use of ` (backtick sign) read the manual of your shell, section about command substitution.

regards
 
I stand corrected. I forgot to put xargs before grep. I wasn't at a UNIX box when I posted yesterday, but I knew when I saw Bungheid's post that I had done a simlar thing - so when I posted, I obviously left out xargs.
Correct code would be:
Code:
ls -1tr | tail -1 | xargs grep "search string"
I usually use the above command when searching through a specific list of files, but I added the tail command for this question. My memory just left out the important xargs part....

And yes, using backticks will work also as hoinz shows above.

My apologies for my forgetfulness.

Thanks for the advice on reading my shell manual too - I indeed have read almost every book on shell and am very familiar with command substitution. Usually when explaining how to do tasks in Unix I don't explain it using command substitution because it can be harder to understand - It is usually more straight forward and easier to understand when commands are piped straight into another command in my opinion. But, as with perl, There is more than one way to do it!

So thanks for kindly correcting me, and providing another solution to Bungheid's post. In the future I will test my code before posting.
 
Brian,

ok, I see.
When I read your post, I did not think of an xargs solution. And this gave me the (obviously wrong) belief that you did not know about command substitution.
Maybe I should be more careful when judging someone else's knowledge ...
 
Thanks guys - two solutions are better than one!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top