LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2009, 12:39 PM   #1
mvmacd
LQ Newbie
 
Registered: Nov 2009
Distribution: OpenSuSE 11.2
Posts: 12

Rep: Reputation: 0
Question 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!!
 
Old 12-30-2009, 01:19 PM   #2
Dave_Devnull
Member
 
Registered: May 2009
Posts: 142

Rep: Reputation: 24
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
 
Old 12-30-2009, 03:41 PM   #3
mvmacd
LQ Newbie
 
Registered: Nov 2009
Distribution: OpenSuSE 11.2
Posts: 12

Original Poster
Rep: Reputation: 0
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

Last edited by mvmacd; 12-30-2009 at 03:41 PM. Reason: typo ;)
 
Old 12-31-2009, 01:46 AM   #4
Dave_Devnull
Member
 
Registered: May 2009
Posts: 142

Rep: Reputation: 24
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
#
#
#
 
Old 12-31-2009, 12:08 PM   #5
mvmacd
LQ Newbie
 
Registered: Nov 2009
Distribution: OpenSuSE 11.2
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 12-31-2009, 12:33 PM   #6
Dave_Devnull
Member
 
Registered: May 2009
Posts: 142

Rep: Reputation: 24
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/
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script for make schedule for automatically download file from ftp site chenboly Linux - General 6 10-05-2015 04:41 AM
Cron Job - FTP Upload File script stuartornum Linux - General 3 09-23-2012 12:34 PM
Upload file to ftp server -vsftp- but can not delete or change the file once uploaded murattas6 Linux - Server 2 06-26-2009 06:00 AM
buggy ftp upload script rblampain Programming 5 05-20-2007 12:30 AM
Connection, login and upload files to ftp server via shell script Paulo Góes Linux - Networking 2 02-21-2004 01:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration