Link to home
Start Free TrialLog in
Avatar of 1hoschi
1hoschi

asked on

shellscript ftp filename as variable

Hi
I need a shell script to download a tar file from a server daily and extract it to a specified folder. the problem i have is that the name of the file changes unregularly and i dont know how to get the name of the downloaded file as a value in my script to extract the file. does anyone have got an idea how to handle this?
heres some code:

#!/bin/sh
HOST='ftp.url.com'
USER='user'
PASSWD='pass'
FILE='*.tar'                  //there is only one tar-file

cd /usr/local/folder
ftp -n $HOST <<END_SCRIPT
user $USER $PASSWD
binary
get $FILE
quit
END_SCRIPT
tar xvf FILENAME         <------ how can i get this in here


thanx a lot

Avatar of HamdyHassan
HamdyHassan

Add "-i" to ftp command
Use mget *.tar


ftp -i  -n $HOST <<END_SCRIPT
user $USER $PASSWD
binary
mget *.tar
Avatar of yuzh
Agreed with HamdyHassan to use mget, modify your script to:

#!/bin/sh
HOST='ftp.url.com'
USER='user'
PASSWD='pass'
FILE='*.tar'                  #there is only one tar-file

cd /usr/local/folder
ftp -inv $HOST <<END_SCRIPT
user $USER $PASSWD
binary
mget $FILE
quit
END_SCRIPT

#To untar the files, you need to do:
for f in  *.tar
do
     echo "extracting $f ...."
     tar xvf $f
done
exit
exit

 
Avatar of 1hoschi

ASKER

thanks alot, but with that you will extract all tar files in the target folder, but i only want to have the downloaded to be extracted. there is only one tar on the ftp but there may be many on the local folder.
ASKER CERTIFIED SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial