1829596 Members
1735 Online
109992 Solutions
New Discussion

ftp script question ..

 
SOLVED
Go to solution
someone_4
Honored Contributor

ftp script question ..

I have read the past post on ftp scripts. I got one that works for what I need but I am trying to modify it a little. Here it is.

#!/usr/bin/sh
HOSTNAME_FILE=ivrs.txt
for host in $(cat $HOSTNAME_FILE)
do
ftp -n << EOF
open $host
user username password
lcd /home/richardl/english
cd netip
put *.vox
done
#
#.end


When I run it I get
./testftp[3]: Syntax error at line 5 : `<<' is not matched.

why am i getting that error?

Richard
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: ftp script question ..

Hi Richard:

The ftp -n << EOF is the 'here docs' convention.
It means take input from the script until it sees the string 'EOF' at the beginning of a line. You appear to be missing the 'EOF' which should follow your last ftp command (quit).

By the way, you can use any string 'XXX', '!!!', '!EOF!', ... just so long as the << XXX is matched with XXX.

Clay
If it ain't broke, I can fix that.
Darrell Allen
Honored Contributor

Re: ftp script question ..

Hi Richard,

Before the done put

quit
EOF

you need to "quit" from ftp and tell the shell that your redirected input (<
Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
linuxfan
Honored Contributor

Re: ftp script question ..

Hi Richard,


you need a "EOF" after the put *.vox and b4 the done line.

-Ramesh
They think they know but don't. At least I know I don't know - Socrates
linuxfan
Honored Contributor

Re: ftp script question ..

OOps,

should be
put *.vox
quit
EOF
done

Also if you are using multiple files you have to use prompt and mput rather than put so your script would look like

/Begin/

#!/usr/bin/sh
HOSTNAME_FILE=ivrs.txt
for host in $(cat $HOSTNAME_FILE)
do
ftp -n << EOF
open $host
user username password
prompt
lcd /home/richardl/english
cd netip
mput *.vox
quit
EOF
done

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Sridhar Bhaskarla
Honored Contributor

Re: ftp script question ..

Richard,

You forgot EOF just before
done.

Also you need to say quit before EOF

..........
put *.vox
quit

EOF
done

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
someone_4
Honored Contributor

Re: ftp script question ..

im trying to add a test statement.

#!/usr/bin/sh
PATH=/usr/sbin/ping:/usr/bin/:${PATH}
export PATH
cd /home/richardl
HOSTNAME_FILE=ivrs
for host in $(cat $HOSTNAME_FILE)
do
/usr/sbin/ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host down"
else
ftp -n << EOF
open $host

so it pings the host if i fails to go into a file. And if ping does not fail to proceed with the ftp. But for some reason it is bypassing the ftp statement and even if the ping is good that it tells me the host is down.
Any ideas?

Patrick Wallek
Honored Contributor

Re: ftp script question ..

I think you have your statement backwards in the your if.

Try this:

if [ $? = 0 ]
then
ftp -n << EOF
...
...
...
EOF
else
echo "$host down"
fi
linuxfan
Honored Contributor

Re: ftp script question ..

Hi Richard,

Your logic seems ok, its just that in your if statement change it to !=
because when it pings the machine the output is 0 and when it fails it 1

so change it to
#!/usr/bin/sh
PATH=/usr/sbin/ping:/usr/bin/:${PATH}
export PATH
cd /home/richardl
HOSTNAME_FILE=ivrs
for host in $(cat $HOSTNAME_FILE)
do
/usr/sbin/ping $host -n 1 | grep -q '1 packets received'
if [ $? != 0 ]
then
echo "$host down"
else
ftp -n << EOF
open $host


-Ramesh
They think they know but don't. At least I know I don't know - Socrates
James R. Ferguson
Acclaimed Contributor

Re: ftp script question ..

Hi Richard:

Change:

# if [ $? = 0 ]

...to...

# if [ $? -eq 0 ]

...and your test will work.

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: ftp script question ..

Hi (again) Richard:

Ooops! No points for me! Patrick & Ramesh are correct. You have the test backwards.

Either [ $? -ne 0 ] or [ $? != 0 ] in lieu of [ $? = 0 ] will fix your problem.

...JRF...