LinuxQuestions.org
Help answer threads with 0 replies.
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 07-13-2012, 05:44 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
getopts question in bash script.


I am using getopts in a script as it can perform either FTP or RSYNC backup. when running the FTP portion it will create a tarball and as long as it is not run in -n (no kill) it will also verify and encrypt the tarball before FTP across the LAN.

I want to set the script so that if there is no option provided it does not run, but will echo a warning that -f, -r, -n, -o, -s must be called with the script.

Code:
### This file MUST be called with one of the following options.
### -n will start the script in test mode not terminating any interfaces.
### -r will start the rsync vs of the script
### -f will start the ftp vs of the sctipt
### -x will start the script in debug mode bash -xvv
### -s will run the setup portion of the script and must be run first.
### -o will print out the options

### Options
###################################

while getopts ":nrfxso" opt
do
        case $opt in
                n )     NO_KILL=true
                        ;;
		r )	DO_FTP=false
			DO_RSYNC=true
			DO_TAR=false
			;;
		f )	DO_FTP=true
			DO_RSYNC=false
			DO_TAR=true
			;;
		x )	set -xvv
			;;
		s )	DO_SETUP=true
			;;
		o )	DO_USE=true
			;;
        esac
done

shift $(( $OPTIND -1 ))

USAGE()
{
cat <<-usage

This is a test to evaluate this function.  To run this script you must first run it
with the -s option.  During the setup you will be able to choose FTP or RSYNC for backups.

You may run the setup portion more then one time if you wish to use both FTP & RSYNC.

Once the script is setup you must call with either -n, -r, -f, or any combination.
	ex: BACKUP -f -r
		this will run the FTP portion of the script first, then RSYNC after the 
		FTP is complete.  It will terminate all Rx30 screens and interfaces then
		restart them after RSYNC has completed.  It will also remove any tarballs
		left in the /tmp directory on the server.
	ex: BACKUP -f -n
		this will run the FTP portion of the script in TEST mode not terminating
		any rx30 screen or interface.  It will also not encrypt the tarball.
	ex: BACKUP -f -r -x
		Now you will run just like the first example, but it will be run in full 
		debug mode. $ bash -xvv BACKUP -f -r
usage
}
Not sure i explained that correctly. hope i did.

Last edited by lleb; 07-13-2012 at 05:49 PM.
 
Old 07-13-2012, 06:09 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608
Quote:
Originally Posted by lleb View Post
I want to set the script so that if there is no option provided it does not run, but will echo a warning that -f, -r, -n, -o, -s must be called with the script.
I often include a "help" function which basically echoes the options and exits 1. This avoids having to echo help-like text throughout a script.
- Your case statement should have a "*|h)" option which points to help and safeguards against supplying the wrong set of options.
- Before you parse getopts check if $# is empty or does not have the required minimum amount of elements and point to help.

I'm sure any of the
Code:
function howto() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls"; }
would show a better method but that's what I use.
 
1 members found this post helpful.
Old 07-16-2012, 07:55 AM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
thank you much. ill read over those links today while at work.
 
Old 07-16-2012, 08:06 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,992

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
- Your case statement should have a "*|h)" option which points to help and safeguards against supplying the wrong set of options.
where? is that part of the start of the getopts or is that in lower down???

Code:
while getopts "*h|:n" opt
or

Code:
case *h| $opt in
or lower still...

Code:
	case $opt in
		*h )	help=true
			;;
also does the *h force the script to mandate some type of - argument to run?
 
Old 07-20-2012, 05:37 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608
Quote:
Originally Posted by lleb View Post
or lower still...

Code:
	case $opt in
		*h )	help=true
			;;
Yes, that's the place, after all it's just a case statement option.


Quote:
Originally Posted by lleb View Post
does the *h force the script to mandate some type of - argument to run?
Getopts itself catches any unknown options with an error but if you want to check for anything yourself you should put it before the script does anything significant. In the example below you only expect one argument, the arg starts with a dash and shoudln't exist of just a dash:
Code:
#!/bin/bash
_help() { echo "programname: [options]" >/dev/stderr; exit 1; }
[ $# -ne 1 -o "${1:0:1}" -ne "-" -o ${#1} -ne 2 ] && _help
while getopts "nrfxsoh" OPTION; do
        case "${OPTION}" in
                # other options here before "h|*"
                h|*) _help;;
 
  


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
[SOLVED] bash getopts - accept a quoted string with spaces action_owl Programming 3 04-18-2010 09:49 PM
getopts script m4rtin Programming 12 12-14-2009 08:47 AM
getopts and mandatory arguments in BASH jmcejuela Programming 3 04-03-2009 04:00 PM
BASH - problem with subsequent calls to function with getopts dkrysak Programming 3 03-25-2009 03:28 PM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM

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

All times are GMT -5. The time now is 03: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