Results 1 to 9 of 9
  1. #1

    Member Since
    Oct 05, 2010
    FTP Shell Script Q.
    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:

    ftp -i
    cd big/long/pathname/
    get /localpath/

    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
    220 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.

    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

  2. #2

    MacsWork's Avatar
    Member Since
    May 22, 2005
    Closer than you think.
    Performa 6116 2GBSCSI 8MB OS 7.5.3

    get big/long/pathname/ /localpath/

  3. #3

    Member Since
    Oct 05, 2010
    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/ /localpath/" 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.

  4. #4

    Member Since
    Oct 26, 2009
    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

  5. #5

    Member Since
    Oct 05, 2010
    Thanks, that is the info I was looking for. (I think).

    Best Regards.

  6. #6

    XJ-linux's Avatar
    Member Since
    Jul 02, 2007
    Going Galt...
    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.
    Never judge a man, untill you have walked a mile in his shoes...
    That way you'll be a mile away from him, and you'll have his shoes.

  7. #7

    Member Since
    Oct 05, 2010
    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:

    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 localpath/
    print -p bye
    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.

  8. #8

    Member Since
    Oct 26, 2009
    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.

  9. #9

    Member Since
    Mar 06, 2013
    Getting the following error on execution

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Shell script help
    By Hyper in forum macOS - Development and Darwin
    Replies: 12
    Last Post: 05-22-2011, 11:36 AM
  2. Terminal -- shell script -- script will only read two lines of a file
    By Straitsfan in forum macOS - Apps and Games
    Replies: 0
    Last Post: 01-30-2011, 09:04 PM
  3. shell script xml
    By MrGecko in forum macOS - Development and Darwin
    Replies: 4
    Last Post: 10-03-2006, 07:44 AM
  4. Shell script question
    By Niwrad in forum macOS - Development and Darwin
    Replies: 14
    Last Post: 06-28-2005, 04:58 PM
  5. Shell Script on Desktop
    By starrin in forum macOS - Operating System
    Replies: 2
    Last Post: 07-28-2004, 11:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts