Skip to main content
arimakidd

5 days ago

My very first shell script: ftp

 @terrence cox
Just finished, "The System Administrator's Guide To Bash Scripting" and I am trying to build a simple, very simple shell script that will ftp into an account and download a file using get.  I already tested the creds and everything out as I can perform the process direct from the command line.  Once I get this script to work I'll graduate to using it with a 'Dialog' box.  But for now I can't understand why it sticks.  Here is the script:
# Functions - start

#ftp function
ftpLogin () {
echo "What is your username .."
read FTPUSER
echo ""
echo "What is your password.."
read FTPPASSWORD
ftp -inv ftp.knoxttdot.com
user "$FTPUSER" "$FTPPASSWORD"
lcd /home/arimakidd/Programming/myLearning/shellScripting/ftp/ftpRoot
bin
get whileLoop
bye

}


# Functions - stop

ftpLogin
it stops at the line:
ftp -inv ftp.knoxttdot.com

I know this because the output just has the ftp> prompt and if I type in "ls" it's asking me to login.  Why is it stopping?  Why is the script crashing at user "$FTPUSER" "$FTPPASSWORD"?  I have a ttached a screenshot of the output when I run it:
user_407565_5e7bc6f07050c.png



Image of
5 days ago
Hi Danon,

When the script is reaching the `ftp -inv ftp.knoxttdot.com` portion, that's exactly what is being run on the system. It will not try to run the `user "$FTPUSER" "$FTPPASSWORD"` line in your script UNTIL the FTP process ends.

Essentially the script thinks you want to:
> Run FTP from bash
> Run a command called user, from bash
> Run a command called lcd.... from bash

What you are trying to do is something like this:

> BASH: prompt for user/pass
> BASH: run FTP and use username/password
> > FTP: run a series of FTP commands
>> FTP: exit out of FTP when finished
> BASH: do whatever you need to do once FTP is complete

From the above, can you see that the bash script you make actually needs to send commands to FTP? Fortunately there is a way to do this!

Within your script what you actually need to be able to do is feed commands to FTP, and you can do so using a feature called HEREDOC (<<SCRIPT.  This can be whatever you want).

To do so, you would take steps like this:

echo "What is your username .."
read FTPUSER
echo ""
echo "What is your password.."
read FTPPASSWORD
ftp -inv ftp.knoxttdot.com <<FTPSCRIPT
quote USER $FTPUSER
quote PASS $FTPPASSWORD
lcd /home/arimakidd/Programming/myLearning/shellScripting/ftp/ftpRoot

...

quit
FTPSCRIPT

I'd recommend taking a look at these two articles which may help, as my code above is really just demonstrative (and hasn't been tested, nor does it include all the commands you want/need).

https://stackoverflow.com/questions/1894347/how-to-upload-ftp-files-to-server-in-a-bash-script/1894479


https://tldp.org/LDP/abs/html/here-docs.html


Hope this helps!

James




Image of arimakidd
arimakidd
5 days ago
Wow
 @nebnev this actually worked.  Hooray.  Was this covered in the 'System Administrator's Guide To Bash Scripting'?  I may have missed it but I don't recall it at all.  Wow.  So the modified script is:

#ftp function
ftpLogin () {
        echo "What is your username .."
        read FTPUSER
        echo ""
        echo "What is your password.."
        read FTPPASSWORD
        ftp -inv ftp.knoxttdot.com <<HEREDOC
        user $FTPUSER $FTPPASSWORD
        lcd /home/arimakidd/Programming/myLearning/shellScripting/ftp/ftpRoot
        bin
        get whileLoop
        bye
HEREDOC
}

and it works.  I'm alot more comfortable now moving on with this script to a dialog box.  Typing in a password at the console where a shoulder surfer can see it is not safe.  But thanks again.  I feel great!

Image of
4 days ago
Excellent! Glad to hear it's working for you.