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!

KSH script please help

Status
Not open for further replies.

sjcrane

MIS
Jan 18, 2006
41
US
I need a ksh script that will find the newest file in a directory:

root@phxsa2 in /usr2/HHM/pub/SMM/transfer/request
# ls -t | head -n1|cat
izdata_656701_20140702_64897.rejected.dat
root@phxsa2 in /usr2/HHM/pub/SMM/transfer/request

Then email that file to a specified user:

mail -s " BYIM rejection file for 'DATE'" user@local.org < "filename"

I am unsure of how to either assign the file to a value or send the contents of the file in the email...???

Any help would be gratefully appreciated...
 
Ok, this is a very basic script.

How often the file is written in the directory?

If you think the file is generated every 10 minutes you can add a crontab.

Code:
#!/bin/ksh
DIR="/usr2/HHM/pub/SMM/transfer/request"
ls -ltr $DIR | tail -1 |awk '{print $9}' |while read i
do
uuencode $i filename |mailx -s "BYIM rejection file for $(date)" youremailaddress@provider.com
done


SARFARAZ AHMED SYED,
Sr. Systems Engineer
 
Thanks,

I added one line for it to work:

#!/usr/bin/ksh
# BYIM relect notification
# stephenc@grhc.org
set -x
cd /usr2/HHM/pub/SMM/transfer/request/ < - - - - - - - - - - - - Added this line
DIR="/usr2/HHM/pub/SMM/transfer/request"
ls -ltr $DIR | tail -1 |awk '{print $9}' |while read i
do
uuencode $i filename |mailx -s "BYIM rejection file for $(date)" stephenc@grhc.org, laacosta@grhc.org
done
root@phxsa2 in /usr/local/scripts

For some reason it would say "INVALID DIRECTORY" when ran in my /usr/local/scripts directory.
Much appreciated !!!
 
Why did you added
Code:
cd /usr2/HHM/pub/SMM/transfer/request/ < - - - - - - - - - - - - Added this line

You don't need that.

SARFARAZ AHMED SYED,
Sr. Systems Engineer
 
I had to modify the file because it was grabbing a directory /usr2/HHM/pub/SMM/transfer/request/.more which was the last entry in "ls -ltr"

#!/usr/bin/ksh
# BYIM relect notification
# stephenc@grhc.org
set -x
cd /usr2/HHM/pub/SMM/transfer/request/
DIR="/usr2/HHM/pub/SMM/transfer/request"
#ls -ltr $DIR | tail -1 |awk '{print $9}' |while read i
ls -ltr $DIR | tail -2 |awk '{print $9}' |while read i
do
#uuencode $i filename |mailx -s "BYIM rejection file for $(date)" stephenc@grhc.org, laacosta@grhc.org
uuencode $i filename |mailx -s "BYIM rejection file for $(date)" stephenc@grhc.org
done


===========

# ./byim_daily_reject.ksh
+ cd /usr2/HHM/pub/SMM/transfer/request/
+ DIR=/usr2/HHM/pub/SMM/transfer/request
+ ls -ltr /usr2/HHM/pub/SMM/transfer/request
+ tail -2
+ awk {print $9}
+ read i
+ uuencode izdata_656701_20140613_42644.rejected.dat filename
+ date
+ mailx -s BYIM rejection file for Thu Jul 10 14:00:10 MST 2014 stephenc@grhc.org
+ read i
+ uuencode .work filename < - - - - - -- - - - Do not want this one...........................
+ date
+ mailx -s BYIM rejection file for Thu Jul 10 14:00:11 MST 2014 stephenc@grhc.org
+ read i
root@phxsa2 in /usr/local/scripts
#

Now it sends 2 emails. One with the bad data and one with the good

Is there a way to "ls -ltr $DIR | tail -2 |awk '{print $9}' |while read i" with only sending the one (1) file?








 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top