New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Operating System General OS operation information and support

FTP Shell Script Q.


Post Reply New Thread Subscribe

 
Thread Tools
edwardlscott

 
Member Since: Oct 05, 2010
Posts: 6
edwardlscott is on a distinguished road

edwardlscott is offline
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:pswrd@webaddress.com
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
QUOTE Thanks
MacsWork

 
MacsWork's Avatar
 
Member Since: May 22, 2005
Location: Closer than you think.
Posts: 2,144
MacsWork is just really niceMacsWork is just really niceMacsWork is just really niceMacsWork is just really niceMacsWork is just really nice
Mac Specs: Performa 6116 2GBSCSI 8MB OS 7.5.3

MacsWork is offline
Try,..

get big/long/pathname/filename.zip /localpath/filename.zip
QUOTE Thanks
edwardlscott

 
Member Since: Oct 05, 2010
Posts: 6
edwardlscott is on a distinguished road

edwardlscott is offline
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.
QUOTE Thanks
ukchucktown

 
Member Since: Oct 26, 2009
Posts: 128
ukchucktown has a spectacular aura about

ukchucktown is offline
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
QUOTE Thanks
edwardlscott

 
Member Since: Oct 05, 2010
Posts: 6
edwardlscott is on a distinguished road

edwardlscott is offline
Thanks, that is the info I was looking for. (I think).

Best Regards.
QUOTE Thanks
XJ-linux

 
XJ-linux's Avatar
 
Member Since: Jul 02, 2007
Location: Going Galt...
Posts: 3,351
XJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond reputeXJ-linux has a reputation beyond repute
Mac Specs: MacBookAir5,2:10.9.4-MacMini3,1:10.9.4-iPhone6,1:7.1.2

XJ-linux is offline
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.

"Those who don't understand Unix are condemned to reinvent it, poorly." Henry Spencer
QUOTE Thanks
edwardlscott

 
Member Since: Oct 05, 2010
Posts: 6
edwardlscott is on a distinguished road

edwardlscott is offline
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.
QUOTE Thanks
ukchucktown

 
Member Since: Oct 26, 2009
Posts: 128
ukchucktown has a spectacular aura about

ukchucktown is offline
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.
QUOTE Thanks
chandan88

 
Member Since: Mar 06, 2013
Posts: 1
chandan88 is on a distinguished road

chandan88 is offline
Getting the following error on execution

ftp: Unknown port `ftp://anonymous:guest@172.16.111.78', using port 21
ftp: Can't lookup `-in': nodename nor servname provided, or not known
Not connected.
Not connected.
QUOTE Thanks

Post Reply New Thread Subscribe


« Time Machine taking a long time to prepare items | Upgrading Mac Mini OS X 10.5.8 to OS X 6 »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Shell Script Address Book Mbengi Bongi OS X - Development and Darwin 0 04-28-2009 03:02 PM
Terminal.app/bash: Creating An Executable Shell Script ebay Running Windows (or anything else) on your Mac 7 04-28-2008 06:43 PM
Shell script question Niwrad OS X - Development and Darwin 14 06-28-2005 04:58 PM
OS X.26 - FTP allows login, but then closes after cwd to folder funky Web Design and Hosting 2 01-27-2005 11:43 PM
Shell Script on Desktop starrin OS X - Operating System 2 07-28-2004 11:25 AM

All times are GMT -4. The time now is 07:52 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?