FTP Shell Script Q.

Joined
Oct 5, 2010
Messages
6
Reaction score
0
Points
1
I am new to Shell scripting. I found an example script to do an FTP file transfer and modified it to my circumstances. It seems straightforward:

#!/bin/bash
ftp -i ftp://username:[email protected]
cd big/long/pathname/
get filename.zip /localpath/filename.zip
bye
EOF

Invoked from Terminal it connects successfully but the cd command does not execute and I simply get the ftp> prompt within terminal like this:

Connected to theserver.com.
220 theserver.com FTP server ready.
331 Password required for username.
230 User username logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
200 Type set to I.
ftp>

I can now execute ftp commands. If I type exit, the last two lines of the script (cd and get lines) then execute out-of-context within terminal and generate "command not found" errors.

Any suggestions on why the cd and get commands won't execute from inside the script after ftp connects?

Thanks in advance. ~Ed
 
Joined
May 22, 2005
Messages
2,159
Reaction score
67
Points
48
Location
Closer than you think.
Your Mac's Specs
Performa 6116 2GBSCSI 8MB OS 7.5.3
Try,..

get big/long/pathname/filename.zip /localpath/filename.zip
 
OP
E
Joined
Oct 5, 2010
Messages
6
Reaction score
0
Points
1
Thanks for your response.

Tried it. Eliminated cd command, put path into get command. Same behavior, successful connection after about 10 seconds, Terminal branches to the >ftp prompt, but "get big/long/pathname/filename.zip /localpath/filename.zip" does not execute until after I do ">ftp exit" at which time an error occurs (for obvious reasons).

Any other ideas? It is as if the script pauses during connection then doesn't continue executing after connection.
 
Joined
Oct 26, 2009
Messages
128
Reaction score
8
Points
18
You are misunderstanding how shell scripting works. It's doing exactly what you are telling it to do. FTP reads its commands from standard input. Your script is not the standard input of the FTP command. After you execute the ftp line in your script you will get an FTP prompt and once you type exit the next two lines in your script execute. The cd is a relative path so the operating system will look for that path relative to your current directory. The last line will generate a command not found. You need to redirect the standard input of the FTP command. I included a link. Read through the thread. It might help you understand. You need to understand file descriptors, standard input and output and how to redirect them.

Automated FTP task - The UNIX and Linux Forums
 
Joined
Jul 2, 2007
Messages
3,494
Reaction score
204
Points
63
Location
Going Galt...
Your Mac's Specs
MacBookAir5,2:10.13.6-iMac18,3:10.13.6-iPhone9,3:11.4.1
In ftp you need to use lcd not cd to do a local change directory. Try to lcd to the local directory you want downloaded to, then doing a plain old get of the file.
 
OP
E
Joined
Oct 5, 2010
Messages
6
Reaction score
0
Points
1
Thanks for all the help so far!

ukchucktown, I followed your advice and learned some about file redirection from your link (although it a little mysterious why it isn't called i/o redirection). The technique of creating the co-process and feeding the ftp commands into it using "print -p" commands makes sense, so I borrowed the code example and tried it. Stuck on line three code where sub-process should create. Script now looks like this:

#!/bin/bash
exec 4>&1
ftp -nv >&4 2>&4 |&
print -p ftp -in ftp://username:pwd@URL
print -p cd big/long/pathname
print -p get filename.zip localpath/filename.zip
print -p bye
wait
exit 0

Here is the error:

-bash: syntax error near unexpected token `&'

Further web searching leads me to believe the example code I borrowed was written for Korn shell, not Bash. Now "over my head" is an understatement, I can't even see the coastline.

Any further guidance much appreciated as sorting out syntax errors requires a trained eye and Googling doesn't help much unless you are lucky.
 
Joined
Oct 26, 2009
Messages
128
Reaction score
8
Points
18
There are lots of shells for Unix and OS X at its core is Unix. Open a terminal and cd /bin and then ls -al bash or ls -al ksh. They are both there so just change line 1 from #!/bin/bash to #!/bin/ksh and give it a go with your script.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top