LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Make FTP script to upload file to server? (https://www.linuxquestions.org/questions/linux-newbie-8/make-ftp-script-to-upload-file-to-server-778921/)

mvmacd 12-30-2009 12:39 PM

Make FTP script to upload file to server?
 
I have OpenSuSE 11.2, and I have a script to download the latest version of our family's schedule [we sing and travel around the country part of the year]:
Code:

cd /home/matthew/Desktop/local/
rm schedule.html
wget www.macdonaldfamilysingers.com/schedule.html
kompozer ~/Desktop/local/schedule.html

After downloading, it opens KompoZer, so I can update it, after saving I have to open FileZilla, and manually tell it upload schedule.html to a certain directory. Lots of times it is quicker from the terminal rather than GUI! So I figured I'd post this here.
If possible I would like a little prompt after closing CompoZer,
Code:

upload schedule.html? y/n
[In case I accidentally save it wrong, I don't want it to wipe out the real schedule]
So, can you show me what I need to paste at the end of my script, to log in to ftp and upload schedule.html, after closing KompoZer?? Thanks so much!!

Dave_Devnull 12-30-2009 01:19 PM

Not tested, but something like this:

Quote:

#!/bin/bash
filename="/path/to/schedule.html"
serverdir="/path/to/your/webspace/dir"
hostname="your_ftp_hostname"
username="your_ftp_username"
password="your_ftp_password"
ftp -n $hostname <<EOF
quote USER $username
quote PASS $password
cd $serverdir
put $filename
quit
EOF
echo ended

mvmacd 12-30-2009 03:41 PM

I tried it, but it said something like it could not find the file/folder. I suspect because it was looking for "/home/matthew/Desktop/local/" under my /htdocs dir. So I took out the $username, $password, etc, and just put it in directly, and I used lcd to change the local dir to ~/Desktop/local/, and then it was able to find schedule.html and upload it!! Thanks for your help! I now have an alias to the script [sh ~/Desktop/local/upload_schedule] so I can quickly download it and upload it [I have a download alias too]. But, it would be cooler, and quicker, if at the end of my download script, I could have a prompt: "Upload also? Y/N" where, "y" would in turn run the upload script. Do you know if it's possible? Thanks :)

~Matt

Dave_Devnull 12-31-2009 01:46 AM

Try this;


Code:

#!/bin/bash
#
#
#DOWNLOAD CODE GOES HERE
#
#
while true
do
echo -n "Upload the file back to the FTP server <y/n> "
read CONFIRM
case $CONFIRM in
y|Y|YES|yes|Yes) break ;;
n|N|no|NO|No)
echo Upload bypassed - exiting script
exit
;;
*) echo Please enter only y or n
esac
done
echo running upload
#
#
#


mvmacd 12-31-2009 12:08 PM

Thanks Dave!! It works! Updating the schedule will be a lot easier now. =)
One more thing, could you sort of explain a little about, what it means? Obviously I can get that if I say "y,Y,YES,yes,Yes" then it will jump to ";;" and run everything after that, bypassing the 'exit.' and what does 'esac' mean? 'read'? 'case'? If you don't have time, that's perfectly fine. I was just wondering.

~Matt

Dave_Devnull 12-31-2009 12:33 PM

What we are saying;
We keep doing everything between
while true
do
....
done

WHILST true is true. True will always be true, so this would loop forever. We are saying 'keep doing this until we get an answer we want'.
Whilst in this loop we read the stdin (keyboard) into the variable CONFIRM and then check to see if it matches any of these patterns:
y or Y or YES or yes or Yes
- if it does we 'break' out of the while/do and the script continues after the 'done' statement (which closes the while do).
If not we check to see if it matches any of the (default) patterns:
n or N or no or NO or No
- if it does we exit the program with the 'exit' command.
If nothing matches we go back to reading the the stdin (keyboard) as true is still (and will always be) true.

The y or Y or YES or yes or Yes and n or N or no or NO or No is just to cover the various bases that a user may type instead of y or n. Users are odd like that.

We've covered the block:
while true
do
....
done

Your other question is about the CASE or 'switch' statement, basically this checks a variable against a list of cases. Here there are two - but one is the default 'catch all' (hence no break;; terminating it). The closing of case is easc (it's almost case backward).

Truth is, I'm no whizz with bash scripting. I keep a copy of everything useful I do, and the FTP and Y/N are common for me.

Take a look at this - it has some more meat ;-)

http://www.linuxquestions.org/questi...script-637221/


All times are GMT -5. The time now is 05:11 PM.