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

How do I choose latest file from directory?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi all,<br><br>&nbsp;&nbsp;I'm trying to add a line to an existing script that is &quot;supposed&quot; to e-mail the latest <i>posting.*.*.log</i> (*=yymmdd.hhmm) from a directory. But the script is failing because it can't find the latest posting logfile.&nbsp;&nbsp;I've written a command that locates the latest posting logfile and isolates it from the list, but I need to know how I should tell the script to use the output of the command to pick the file.<br><br>Here's the line:<br><i>ls -ltar ¦awk '{print $9}' $STDOUT ¦tail -3 ¦grep posting</i><br><br>I want to put this in the script, but after the grep maybe make the output a variable, can that be done on one line or should it be redirected to another file and name the file a variable?&nbsp;&nbsp;Sorry if this sounds confusing, I hate having to fix other people's scripts, especially when they don't know exactly what it is supposed to do themselves.<br><br>Thanks for the help<br> <p>Jon Zimmer<br><a href=mailto:b0rg@pcgeek.net>b0rg@pcgeek.net</a><br><a href= Aetea Information Technology</a><br>The software required `Windows 95 or better', so I installed Linux.<br>
 
hi
As far as i understand your question u can solve this problem as
z=`your command`
and then u can refernce your variable as $z anywhere in you script.
hope it will solve your problem.

Regds

Shiraz
 
Actually, the problem I'm having is that what I want to do is for the output of my command to 1) list the latest file 2) tail -10 the file and mail the output. This command will return the name of the file onto the console, but I can't get it to interact on the file name to tail the information so that I can cat it to a mailfile for delivery.
This works:
ls -ltar ¦awk '{print $9}' $STDOUT ¦tail -3 ¦grep posting
But, this doesn't:
ls -ltar ¦awk '{print $9}' $STDOUT ¦tail -3 ¦grep posting |tail -10 $STDOUT > $mailfile.
 
You want first to locate the last dated file (with a version name scheme):
[tt]
ls -1 | grep &quot;posting.*log&quot; | sort
[/tt]

... get the last ten lines of the file named
[tt]
tail -10 $( ls -1 | grep &quot;posting.*log&quot; | sort )
[/tt]

... and then mail these lines somewhere
[tt]
tail -10 $( ls -1 | grep &quot;posting.*log&quot; | sort ) | mail -s &quot;The last then lines&quot; user@somewhere.com
[/tt]

I hope it works...
 
Hi Jon:

If I understand your first request, you are coding within an existing script and you want to be able to use the output of this sequence:
ls -ltar ¦awk '{print $9}' $STDOUT ¦tail -3 ¦grep posting

Given that this is true, pipe the result of the grep into READ and add a variable name for use in the rest of the script. E.G:

ls -ltar ¦awk '{print $9}' $STDOUT ¦tail -3 ¦grep posting | read POST

echo $POST &quot; is the result&quot;

This could be one way of working this problem..

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top