PDA

View Full Version : FTP bash script



TheFlyingCorpse
10th September 2007, 08:02 PM
Hi,

I am TheFlyingCorpse, leader of Interstate Nitro, a modification for Battlefield 2, a Micro$.. program.

I am currently administering a Fedora Core 6 server which have gotten quite crowded recently, by all the extra's around the development of a game addon. One of the latest, is a script that moves the recorded demos(everything that has happened during the round), to an external server for everyone to DL if they want to review what happened in the round that was, maybe make a movie or get a few screenshots.

This is not easy, hehe, because there is already a script, in python for it, which I have tried to make work, it still wont work, so I've begun writing my own script, in BASH(.sh).

It works by defining everything up front, the parts where I am stuck is, how to get the latest filename in the directory, both transfer this filename and use it to name another file + (.desc) to the ending for logging reasons.

This is my code:

#!/bin/sh

HOST='XXXX'
USER='XXXX'
PASSWD='XXXXX'
DIR = '/demos'
FILE='$filename'

cd /home/<user>/bf2
cp bfr_cache.desc /home/<user>/temp // out of the bf2 dir!
cd /home/<user>/bf2/mods/isnitro/demos
// get the latest filename here! add it to the $file parameter
cp $filename /home/<user>/temp
rm $filename (to clean up!)
cd /home/<user>/temp
rn bfr_cache.desc $filename + .desc (somehow :P)

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put *.* (both the files)
quit
END_SCRIPT

rm -y *.* (clean up after!)
exit 0


What is missing? Am I too far out to make this script work?

giulix
10th September 2007, 09:36 PM
Hiya. If your problem is to get the latest file in a directory (latest as in 'with the most recent modification time') all you need to do is use ls with one of its many options, -t in this case, which sorts files by modification time. Then you limit the output to a single column (-1) and get the first line only:


ls -t1 |head -1

One of, I am sure, many ways of doing it...

Use `mv' (not rn) to rename a file and the following construct to attach an extension to it



fileplusext = ${file}.ext

TheFlyingCorpse
10th September 2007, 10:15 PM
For the latest change, how do I then select that filename to the $?

--

I didn't understand that last part very much, did I do it right?

--



#!/bin/sh

HOST='XXXXx'
USER='XXXXXXX'
PASSWD='XXXXXXXx'

cd /home/nitroserver/bf2
echo 'Changed dir to Bf2'
cp bfr_cache.desc /home/nitroserver/temp #// out of the bf2 dir!
echo 'Copied the bfr cache'
cd /home/nitroserver/bf2/mods/isnitro/demos
echo 'Changed dir to demos'
ls -t1 |head -1 #// get the latest filename here! add it to the $filename parameter
echo 'listing files'
cp $filename /home/nitroserver/temp
echo 'copying the demo to temp'
rm -y $filename #(to clean up!)
echo 'cleaning up'
cd /home/nitroserver/temp
echo 'changed dir to temp'
mv bfr_cache.desc ${filename}.desc #// (somehow :P)
echo 'renamed the desc file, preparing for send'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put *.* #(both the files)
quit
END_SCRIPT
rm -y *.* #(clean up after!)
echo 'cleaned up and finito'
exit 0


Am I going in the right direction?

giulix
10th September 2007, 10:39 PM
To set the value of the variable $filename to the latest file in the directory, you need to use command substitution:



filename=$(ls -t1 |head -1)


or



filename=`ls -t1 |head -1`

note that ` is the backtick and not the apostrophe (a.k.a. single quote)

P.S. The first construct works in bash only, The latter works on any Bourne, Korn and C Unix shells.

TheFlyingCorpse
10th September 2007, 10:55 PM
Thx, will test the script now;)

giulix
10th September 2007, 11:08 PM
Good, let us know. btw, the customary way of zapping a file, no questions asked, is


rm -f filename

TheFlyingCorpse
10th September 2007, 11:16 PM
This is how the script looks atm:

#!/bin/sh

HOST='XXXXX'
USER='XXXX'
PASSWD='XXXXX'
FILE='$filename'

cd /home/nitroserver/bf2/
echo 'Changed dir to Bf2'
cp bfr_cache.desc /home/nitroserver/midl/
echo 'Copied the bfr cache'
cd /home/nitroserver/bf2/mods/isnitro/demos/
echo 'Changed dir to demos'
filename=$(ls -t1 |head -1) #// get the latest filename here! add it to the $filename parameter
echo 'listing files'
cp $filename /home/nitroserver/midl/
echo 'copying the demo to temp'
rm $filename #(to clean up!)
echo 'cleaning up'
cd /home/nitroserver/midl/
echo 'changed dir to temp'
mv bfr_cache.desc ${filename}.desc #// (somehow :P)
echo 'renamed the desc file'
#// and send it

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put *.* (both the files)
quit
END_SCRIPT
rm -y *.* (clean up after!)
echo 'cleaned up and finito'
exit 0


This is the output:


: command not found
: command not found
: No such file or directory/nitroserver/bf2/
Changed dir to Bf2
cp: cannot stat `bfr_cache.desc': No such file or directory
Copied the bfr cache
: No such file or directorye/nitroserver/bf2/mods/isnitro/demos/
Changed dir to demos
listing files
copying the demo to temp
cleaning up
: Not a directory: cd: /home/nitroserver/midl/
changed dir to temp
mv: cannot stat `bfr_cache.desc': No such file or directory
renamed the desc file
: command not found
: unknown hostro.com
Not connected.
Not connected.
Not connected.
?Invalid command

All the directories DOES exist, the files too, so why does it give me these errors? Its all under the same user, the script is run as the user too, so permissions should be fine.

giulix
10th September 2007, 11:37 PM
Mmmmmhhh.... You didn't copy the script file from a windoze/DOS filesystem, by any chance ?

To find out. type



vi -b scriptname


If you see all lines ending with ^M, you need to



dos2unix scriptname


Oh, quit the vi editor by typing [Esc] and then



:q!

TheFlyingCorpse
11th September 2007, 09:02 PM
Ok, it works now, a few modifications did it!


#!/bin/sh

HOST='XXX'
USER='XXXX'
PASSWD='XXXX'

cd /home/nitroserver/bf2/
echo 'Changed dir to Bf2'
cp bfr_cache.desc /home/nitroserver/midl/
echo 'Copied the bfr cache'
cd /home/nitroserver/bf2/mods/isnitro/demos/
echo 'Changed dir to demos'
filename=$(ls -t1 |head -1) #// get the latest filename here! add it to the $filename parameter
echo 'listing files'
cp $filename /home/nitroserver/midl/
echo 'copying the demo to temp'
rm $filename #(to clean up!)
echo 'cleaning up'
cd /home/nitroserver/midl/
echo 'changed dir to temp'
mv bfr_cache.desc ${filename}.desc #// (somehow :P)
echo 'renamed the desc file'
#// and send it

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $filename
put ${filename}.desc
quit
END_SCRIPT
rm *.* #(clean up after!)
echo 'cleaned up and finito'


Thats the end result and it works ! thank you a ton!