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 05-05-2013, 06:02 PM   #1
313
Member
 
Registered: Oct 2011
Location: Cardiff
Distribution: Debian 6.0.5
Posts: 39

Rep: Reputation: Disabled
Help writing a bash script for FTP uploading.


Hi, I'm looking to write a bash script for uploading files to my FTP server.

What I ideally want (not that I have the faintest idea of the plausability of it or where to start), is to be able to reduce what I have to type each time I upload, like so:

Instead of -
Code:
ncftpput -u USER -p PASS server.com /directory/ ~/music/up/FILE.zip
I'd have -
Code:
scriptname.sh ~/music/up/FILE.zip
What I need help with is getting the script to recognise the input.

If I'm being an overambitious noob who should go away and learn the first thing about bash before getting people on the inernet do to it then I understand, but help would be appreciated as it doesn't seem all that hard (I hope).
 
Old 05-05-2013, 06:11 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853
I wouldn't use a script, but an alias for that, like
Code:
alias upload="ncftpput -u USER -p PASS server.com /directory/"
Just put that into your ~/.bashrc. After starting a new terminal you should be able to use it the way you wanted:
Code:
upload ~/music/up/FILE.zip
 
1 members found this post helpful.
Old 05-05-2013, 06:33 PM   #3
313
Member
 
Registered: Oct 2011
Location: Cardiff
Distribution: Debian 6.0.5
Posts: 39

Original Poster
Rep: Reputation: Disabled
Perfection, thanks for the quick response. No doubt that will consistently come in handy.
 
Old 05-05-2013, 07:09 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
For bash scripts etc
http://rute.2038bug.com/index.html.gz - Linux cli tutorial
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 05-05-2013, 09:33 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
just for giggles this might help as a start, but the alias could just be one of the easier ways to handle this and im still not 100% on my little script. you can test it out and see. if you get errors add the bash -xvv under the first line of the script.

Code:
#!/bin/bash
### remove the # from next line if you have issues
#bash -xvv

### Variables

STARTDATE="`date +%F-%a-%H.%M.%S`"
DOW=`date +%A`
TAR="${DOW}"
HOMEDIR="$HOME"
LOG=${HOMEDIR}/logs/${STARTDATE}-FTP.log
ENDDATE="`date +%Y-%b-%d-%a-%H.%M.%S`"
CONFFILE="${HOMEDIR}/.ftptransfer"

### Checking for log directory

[ ! -d "${HOMEDIR}/logs" ] && mkdir -p ${HOMEDIR}/logs >> /dev/null 2>&1

### Options for FTP program

echo -n "Enter the remote pc username: "
read REMUSER
echo -en "\t${REMUSER}" >>${CONFFILE}.tmp
echo -n "Enter the remote pc password: "
read REMPASS
echo -en "\t${REMPASS}" >>${CONFILE}.tmp
echo -n "Enter the full path to place remote files ( example; /home/backupuser ) : "
read REMDIR
echo -en "\t${REMDIR}" >>${CONFILE}.tmp
echo -n "enter remote URL or ip address: "
read REMIP
echo -en "\t${REMIP}" >>${CONFILE}.tmp
echo -n "Enter path to file or directory to FTP ( example; /home/music ) : "
read FILE
echo -en "\t${FILE}" >>${CONFFILE}.tmp
echo -n "Do you wish to compress data before transmitting: "
read ANSWER
	if [[ ${ANSWER} = [Yy] ]]
	then
		echo "Compressing ${FILE}"
		tar cvjf /tmp/${TAR}.tar.bz2 ${FILE} >>${LOG}
		echo "Creating /tmp/${TAR}.tar.bz2 at ${STARTDATE}." >> ${LOG}
		NEWTAR="/tmp/${TAR}.tar.bz2"
### Starting compressed FTP transmission
		echo "Starting compressed FTP transmission at ${STARTDATE}" >> ${LOG}
		ncftpput -u ${REMUSER} -p ${REMPASS} ${REMIP} ${REMDIR} ${NEWTAR} >> ${LOG}
		echo "FTP transmission completed at ${ENDDATE}" >> ${LOG}
	else
### Starting uncompressed FTP transmission
		echo "Starting uncompressed FTP transmission at ${STARTDATE}" >> ${LOG}
		ncftpput -u ${REMUSER} -p ${REMPASS} ${REMIP} ${REMDIR} ${FILE} >> ${LOG}
		echo "FTP transmission completed at ${ENDDATE}" >> ${LOG}
	fi

exit
edit, this bit of code is just some more practice for me at coding. im still getting used to it.

edit to fix the error that Chrism01 pointed out for me.

Last edited by lleb; 05-06-2013 at 12:58 AM.
 
Old 05-05-2013, 10:00 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
With 'mkdir -p', no need to check if target exists already, it doesn't do anything if target already exists.

This
Code:
	if [[ ${ANSWER} = [Yy]
needs fixing, but I'll leave that as an exercise for the OP
 
Old 05-06-2013, 12:57 AM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by chrism01 View Post
With 'mkdir -p', no need to check if target exists already, it doesn't do anything if target already exists.

This
Code:
	if [[ ${ANSWER} = [Yy]
needs fixing, but I'll leave that as an exercise for the OP
ahh ty, yes i left out the closing ]] at the end. duh. ty for catching that. like i said im still rather new to coding.

what about the rest of the script?

as to the mkdir -p will that not give errors if the directory already exists?
 
Old 05-06-2013, 01:38 AM   #8
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,271

Rep: Reputation: 1298Reputation: 1298Reputation: 1298Reputation: 1298Reputation: 1298Reputation: 1298Reputation: 1298Reputation: 1298Reputation: 1298
Is this machine used by multiple users simultaneously? If so you might want to use cURL to do the upload as it will hide the password from anyone running a nicely timed ps command.
 
Old 05-06-2013, 02:39 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Quote:
as to the mkdir -p will that not give errors if the directory already exists?
Try it on a test dir
 
  


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
I need some help writing a bash script... trist007 Programming 8 04-15-2009 11:31 AM
Writing a bash script s_b Linux - Newbie 7 10-08-2008 01:32 PM
Writing a bash script. lebabyg Linux - General 2 03-31-2007 12:39 PM
LXer: uploading with an ftp macro script LXer Syndicated Linux News 0 02-12-2006 09:01 PM
Script for changing site name and uploading via FTP fleeced Linux - General 3 08-15-2004 05:15 PM

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

All times are GMT -5. The time now is 08:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration