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!

Script to Cat specific file based on Date in File name

Status
Not open for further replies.

hg4372

Technical User
Feb 9, 2009
43
0
0
US
Please Help.

I'm trying to write a script on Machine A that logs into Machine B that is rotating log files every half hour. So what I want to do is pull the latest file based on the date.

cd /temp
file_20091027130000
file_20091027133000
file_20091027140000
file_20091027143000

So at 13:15 I would want to perform a cat on the 1300 file.
13:45 the 1330 file
14:15 the 1400 file
etc...

I plan on putting the script into the cron on machine A, to login to machine B twice an hour and capture the information to a file on machine A.

Any suggestions ?
 
This would just cat the latest matching file:

Code:
ssh machineb 'cat $(ls -t /tmp/file_* | head -1)'

I'm presuming that the current, unrotated log file would not match the file_* filespec.

Annihilannic.
 
Thanks for the information. This works great if I do it manually, however when I try it in a script on MachineA I get the following error.

/tmp/File_*: No such file or directory.

Thanks again for the help.
 
It sounds like there may be a quoting problem. Can you post the actual script contents please?

Annihilannic.
 
I have another script that launches this script to go out to several machines based on the Host name. The only thing that is really different I guess, is I'm using telnet at this point rather than ssh. So I dropped the single quotes and it works if I do it manually , just not in my script. I assume it has to do with the echo command and the "'s. Thanks for all your help on this. It will really help me out.

switch=$1
dt=$(date +%Y%m%d%H%M)
username=xxxx
password=yyyy

for x in 1
do
{
echo $username
sleep 5
echo $password
sleep 5
echo "cd /opt/log"
sleep 3
echo "cat $(ls -t /opt/bsm/log/SBSCSubsystem-* | head -1)"
sleep 360
exit;

}|telnet $switch>>/logs/sbslogs/$switch.STAT_$dt
 
Replace the "s with 's and it should work as you expect. With "s the $( ... ) is expanded on the local system, in the same way as $username is, for example, whereas with 's it is transmitted to the remote system as-is.

Annihilannic.
 
Thanks so much. It took me a while before I could try it, but it works great. Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top