Solved

shellscript ftp filename as variable

Posted on 2004-03-23
4
574 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:1hoschi
  • 2
4 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 10658286
Add "-i" to ftp command
Use mget *.tar


ftp -i  -n $HOST <<END_SCRIPT
user $USER $PASSWD
binary
mget *.tar
0
 
LVL 38

Expert Comment

by:yuzh
ID: 10664134
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

 
0
 

Author Comment

by:1hoschi
ID: 10664744
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.
0
 
LVL 38

Accepted Solution

by:
yuzh earned 100 total points
ID: 10665155
To play safe, you can download them to a tmp dir and then extract them, here's the new
version of the script:

#!/bin/sh
HOST='ftp.url.com'
USER='user'
PASSWD='pass'
FILE='*.tar'                  #there is only one tar-file
TMPDIR=/pathto/mytmp
DESDIR=/usr/local/folder

cd $TMPDIR
rm *.tar                  # make sure no tar files in your tmp dir
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 ...."
     cd $DESDIR
     tar xvf ${TMPDIR}/${f}
     cd ${TMPDIR}
     #If you want to mv the *.tar file to $DESDIR, uncomment the following:
     # mv $f $DESDIR
done
   
exit
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 85
how do i create updater to My Activex application? 3 79
Expand data scrubbing tool 13 32
JQuery serialize and unserialize 8 49
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now