1833838 Members
2421 Online
110063 Solutions
New Discussion

Shell script help

 
SOLVED
Go to solution
Andy Koumi_1
Occasional Contributor

Shell script help

Hi,

I've been tasked with creating a unix shell script that will select the latest created file and ftp it to another i.e. the file is currently generated every 12 hours (at 00:00 & 12:00) and 7 days worth are kept at any one time. Therefore the files in the directory are as follows:

-rw-rw-rw- 1 nortel nortel 12423 Jul 5 00:03 GSIT20040705000002.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 5 12:03 GSIT20040705120004.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 6 00:03 GSIT20040706000002.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 6 12:03 GSIT20040706120004.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 7 00:03 GSIT20040707000003.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 7 12:03 GSIT20040707120005.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 8 00:03 GSIT20040708000003.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 8 12:03 GSIT20040708120005.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 9 00:03 GSIT20040709000003.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 9 12:03 GSIT20040709120002.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 10 00:03 GSIT20040710000002.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 10 12:03 GSIT20040710120005.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 11 00:03 GSIT20040711000002.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 11 12:03 GSIT20040711120005.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 12 00:03 GSIT20040712000002.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 12 12:03 GSIT20040712120005.drf
-rw-rw-rw- 1 nortel nortel 12423 Jul 13 00:03 GSIT20040713000003.drf


Therefore what I need to do is at midnight after the file has been created I will need to select the latest file and ftp it to another server and at mid-day again I will have to perform the same task but with the latest file generated at midday etc, etc. As can be seen above the files are generated automatically and contain the date & time in the filename.

I'm a beginner at shell scripting and would greatly appreciate any help that can be provided.

Cheers

AK
10 REPLIES 10
Steve Steel
Honored Contributor
Solution

Re: Shell script help

Hi

Use the system commands to find the newest file such as

dir=/home/steves/tmp
ls -1tr $dir|tail -n 1|while read line
do
file=$(find $dir -name $line)
echo $file
done

Put your dir where mine is and file is the newest one

http://www.shelldorado.com/

Is a good place for a learner


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Victor BERRIDGE
Honored Contributor

Re: Shell script help

Hi,
Since I dont know how you want your ftp, all I can do here is how to select your file:
ll -lt¦grep GSIT ¦ head -n 1

Good luck
Vi
Franky_1
Respected Contributor

Re: Shell script help

Hi Andy,

you could find out the latest file by using "tail -1" command

For example :

for i in `ls /|tail -1`
do
ftp $i ...
done

HTH

Franky
Don't worry be happy
Victor BERRIDGE
Honored Contributor

Re: Shell script help

Sorry for the strange char, dont know where they come from...
You should read "pipe" I will give a try with another char | and se if it displays correctly

All the best
Victor
Victor BERRIDGE
Honored Contributor

Re: Shell script help

Better so:
Start again:
You wanted 1 file - the latest...

ll -lt | grep GSIT | head -n 1

There you are

All the best
Victor
Sanjay Kumar Suri
Honored Contributor

Re: Shell script help

Check this

for i in `ls -t`
do
echo $i
# Other commands...
exit
done

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Andy Koumi_1
Occasional Contributor

Re: Shell script help

Thanks guys!!

Cheers

Andy
Indira Aramandla
Honored Contributor

Re: Shell script help

Hi AK,

YOu can schedule the script to run twice a day through cron job. i.e

00 00,12 * * * /path/script_name > output_log

and the script_name can contain the following line.
Initiall touch a temp_file. Then find the GSIT file that was newer than the touched file as.

find /path -name *GSIT* -newer touched_temp_file |cpio...........
and after the copy to thebother server
remove the touch_temp_file
touch temp_file again.

So that next time the GSIT* file generated will be newer than the touched file. And you have this files generated only twice a day at 00:00 and 12:00. So when the cron job copies the file and touched the temp file, next GSIT file will be newer than the touched file.

I hope this helps You.


Indira A
Never give up, Keep Trying
Bharat Katkar
Honored Contributor

Re: Shell script help

Hi,
Just to participate,
Similar to Victor but this one will only return the Filename:

# ls -t | head -n 1

You can even assign the name to an variable and write a script to FTP the file using that varible.

# var1=`ls -t | head -n 1`
(remember these are back quotes)

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: Shell script help

hai,

Get the recent created file with a line script as,

#script.ksh
# usage:
filename=$(ls -l | grep -v '^total*' | sort -r | head -1)

username=$1
passwd=$2
location=$3

ftp -i -n $server <<-EOF 2>/dev/null
user $usename $passwd
put $filename $location
bye
EOF

# total message will come if we use ll or ls -l

You can use rcp too instead of ftp for this. make "+" entry in $HOME/.rhosts file on two server

Simulate this in a cron job at your required time (at 00:00 & 12:00)

Regards,
Muthukumar.
Muthukumar.


Easy to suggest when don't know about the problem!