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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File checking using shell script

Status
Not open for further replies.

charmingcann

Programmer
Jul 17, 2001
13
SG
I have this shell script that gets a file from a particular server :

HOST_IP=111.111.111.11
E_DEST=$SAMPLE_TOP/in
E_USERID='mis'
cd $E_DEST
ftp -n -v -i << EOF
open $FTP_HOST_IP
user $FTP_USERID
ascii
lcd $E_DEST
get file1
bye
EOF

I need to check whether the file is existing or not. If not, a message will be printed &quot;file not found&quot; and then exit from the script.

Thanks


 
if [ -e yourfile ] blablabla Greetz,
muppeteer.gif

NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

Don't eat yellow snow...and don't mess with your fstab!
 
perhaps a bit more specific is:

if [ ! -e &quot;/home/me/myfile&quot; ]; then
echo &quot;file not found&quot; Greetz,
muppeteer.gif

NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

Don't eat yellow snow...and don't mess with your fstab!
 
Hi, im getting error &quot;invalid command&quot; . the host_ip is a HP3K server and from that i will be getting a file and tranfer to an oracle server.

im not sure if the code and the place to put this code is correct :

HOST_IP=111.111.111.11
E_DEST=$SAMPLE_TOP/in
E_USERID='mis'
cd $E_DEST
ftp -n -v -i << EOF
open $FTP_HOST_IP
user $FTP_USERID
if [ ! -e file1]; then
echo 'file not found'
else
echo 'file found'
fi;
bye
EOF

 
its just aplain script ?

try putting

#!/bin/sh

in the beginning of the file. Greetz,
muppeteer.gif

NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

Don't eat yellow snow...and don't mess with your fstab!
 
Unfortunately, I don't think the if construct can be used within an ftp session. If your file, isn't found, you should get an error to that effect - perhaps you could use that in some way by redirecting the error to a log file, something like:

ftp -n IP_Address <<! > grotfile

HTH.
 
still the same invalid command and there should be a message saying file exists
 
Thats what I use in a normal script. But I'm no expert in scripts. So I'd better leave it to KenCunningham.
Good luck. Greetz,
muppeteer.gif

NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

Don't eat yellow snow...and don't mess with your fstab!
 
thanks Themuppeteer.

Hi Ken,
Im not sure how to do this in my script? can you help me to include this code using the shell script above?


Thanks..
 
This might work (I use a similar arrangement in one of my scripts):

HOST_IP=111.111.111.11
E_DEST=$SAMPLE_TOP/in
E_USERID='mis'
cd $E_DEST
ftp -n -v -i << EOF > ftp.log
open $FTP_HOST_IP
user $FTP_USERID
bye
EOF

Then take a look at what's written to ftp.log when the file is found and when it isn't - there should be some kind of distinct message which will indicate either case. You can then write a grep for this like:

grep <success_text_in_ftp.log> ftp.log
if [ $? = 0 ]
then
echo 'File found'
else
echo 'File not found'
fi

Hope this helps - sorry if it's a little confusing.
 
Hello again. Don't know how I missed this, but it is possible to run commands on the remote machine from within ftp by preceding them with a !

Thus, you could do an ls for your file and test for it's existence using the if [ $? = 0 ] construct above, then echo the result to your log file.

I've never used the ! within ftp myself, but I might well start now! Cheers.
 
! is for running commands on the local host so won't do what you require. Annihilannic.
 
wget might be a better solution.

wget &quot;ftp://user:password@hostname.example.com/filename&quot;

If the file doesn't exist, it returns with an exit status of 1, otherwise it grabs the file.

 
you cannot use test in a ftp script
use ls|dir in ftp cmd pipe the output to grep|sed or
what ever you want, then re-do a ftp

ftp .....<<QQ >output
user ...
cd ..
ls..
quit
QQ
grep -s XXX output >/dev/null || exit
ftp .....<<QQ
user ...
cd ..
get XXX
quit
QQ
don't use the '-i' option


-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Jamisar got it right!

I used to use a similar method to generate subsequent generation numbers for my outgoing filenames

get the listing first....

FILE=/tmp/RESFILE$$

echo &quot;
cd $DIR
ls -l $FILE
bye&quot;|ftp IP

if [ -z &quot;`grep filename $FILE`&quot; ]
then
echo &quot;file not found on remote server!&quot;
else
# go get it
echo &quot;
cd $DIR
get filename
bye&quot;|ftp IP
fi

The above was using netrc entries, so you'll need to modify if not using this method! i.e. username and password with ftp -i -n

ART
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top