0
ftp ipaddress
bin
hash
cd path
get filename
quit

i want these line to be executed in a shell script only first line is executing after entering username and password the rest line are not executing and the control is stuck at ftp> prompt

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Nair Ajit
  • 19
  • 1
  • 8

3 Answers3

0

Once your script invokes the program ftp, the shell loses control until the ftp program finishes. So you need to use some new technique. One way is with the program expect, which you can get a hint about here: https://stackoverflow.com/a/12598169/4323 . Another way is to use a more "scriptable" FTP client, such as lftp on Linux, which has specific features to enable the sort of scripted use you're going for.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

You need to use a non-interactive FTP client if you want to do this in a script rather than an interactive shell. ncftp is one you could check out.

jam
  • 3,640
  • 5
  • 34
  • 50
0

Or you can use a shell "here" document:

#!/bin/bash 

ftp -in <<EOS
user pswd
bin
hash
cd path
get filename
quit
EOS

The here document sends everything via the open program's stdin, just as if you typed it in at the command line. Note that ftp clients can be finicky and each one seems to have it's own set of gotchas, so some experimentation and use of man ftp will likely be required.

Looks like this has come up before on Stackoverflow, you might want to read through how to ftp multiple file using shell script

IHTH

Community
  • 1
  • 1
shellter
  • 36,525
  • 7
  • 83
  • 90
  • I think John is correct as once it invokes ftp the shell loses control untill ftp program finishes – Nair Ajit Feb 27 '13 at 14:25
  • solving ftp client scripting issues is something you can spend days on. I won't have the time to debug this in detail. I highly recommend that you read your ftp client's man page AND that your review any of the 401 questions here on stackoverflow that can be found by searching for `[unix] ftp`. I have included a good S.O. in my answer that should be a big help. **Try changing invocation line to `ftp -in < – shellter Feb 27 '13 at 14:29