[BASH] Using getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [BASH] Using getopts
# 1  
Old 03-24-2014
[BASH] Using getopts

Heyas

Just recently there was a thread about parsing arguments, where i read the first time about getopts.
This said, i'd like to 'provide' a list function that can be 'trigered' using an 'option'(?).

The regarding code snippets are:
Code:
	while getopts "e:(edit)l:(list)m:(mount)u:(unmount)h:(help)d:(debug)": name
	do 	case $name in
		e|edit)	server_info="$OPTARG";shift
			mode=edit ;;
		l|list)	mode=list ;;
		m|mount)server_info="$OPTARG";shift
			mode=mount ;;
		h|help)	printf "$help_text"
			exit $RET_HELP	;;
		u|unmount)
			server_info="$OPTARG";shift
			mode=unmount ;;
		d|debug)	DEBUG=true	;;
		esac
	done
	$DEBUG && set -x
	shift

... more code ...

	List() { # 
	# Prints a list of NAS' and their shares
	#
		ListNAS
	}

... more code ...

#
#	Display & Action
#
	case $mode in
	edit)	Edit
		exit $? ;;
	list)	List
		exit $? ;;
	menu)	menu="Mount Unmount Edit List New"
		tui-echo "Please choose a task:"
		select TASK in $menu;do
			$TASK
			exit $?
		done
		;;
	esac

The behaviour that irritates me is this:
on passing script -l it fails, on passing the long optionname -list it works as expected. (see attached screenshot)
However... -d|-debug is NOT affected by this.. why?

Where lies my problem, if there is one?
Is the getopts/shift thingy properly adapted in general?

Thank you in advance
[BASH] Using getopts-getoptspng
# 2  
Old 03-24-2014
Don't mix up the getopt command with bash's builtin getopts. The former allows for long options which the latter doesn't. And, colons in the OPTSTRING denote options with required arguments (which is indicated in your screenshot). So, in your case, try again with the OPTSTRING elmuhd - no long option, no req. arguments. I guess, you should forget the shift in the case statement as every call to getopts will get the next option.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-24-2014
Thank you Rudi, i'm not fully understand yet.

When i change from getopts to getopt, and run "nas3.sh -l" all i get is "-- name" printed X-times. So i change back to the builtins getopts.
getopts: Man Page for getopts (linux Section 1) - The UNIX and Linux Forums
getopt: Man Page for getopt (linux Section 1) - The UNIX and Linux Forums
A slight diffrence in name, a big one in usage....

Quote:
Originally Posted by RudiC
And, colons in the OPTSTRING denote options with required arguments (which is indicated in your screenshot).
Pardon me, what does my screenshot indicate??

There are no colons (':') in OPTSTRING (actualy the 'value to the '-option' right?) or ANY argument expected.

Issue solved, so thank you again Smilie
But i still dont feel 'comfortable', as i'd like to understand, why -d(ebug) worked without arguments, but -h(elp) and -l(ist) required some or had to be long?
# 4  
Old 03-24-2014
Your screenshot says: option requires an argument -- l when run with -l only. When supplying -list, l is the option, and ist is the argument.

This is your OPTSTRING: "e:(edit)l:(list)m:(mount)u:(unmount)h:(help)d:(debug)": It has many an option character, some listed twice or even more often, and e, l, m, u, h, and d have a colon next to them, making them require an argument. -d works without arguments as it is listed in "edit" without colon. i should work as well as an option...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-24-2014
Wierd, the article (some website i dont recall) showed some examples for multiple long-options in the like of:
Code:
e:(env)(envir)(environment)

Also it differs quite much from the example in: /usr/share/doc/util-linux/getopt-parse.bash which i found just now.
However i dont understand how getopt actualy gets involved: (snipet from the above file)
Code:
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
     -n 'example.bash' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
	case "$1" in

However, i now understand the 'matters' of the colons Smilie
# 6  
Old 03-24-2014
This might help you with long arguments using getopt:

Code:
eval set -- $(getopt -l edit:,list:,mount:,unmount:,help,debug -o e:l:m:u:hd -- "$@")

while true
do
   name="$1"
   shift
   case $name in
       -e|--edit) server_info="$1"; shift ; mode=edit ;;
       -l|--list) server_info="$1"; shift ; mode=list ;;
       -u|--unmount) server_info="$1"; shift ; mode=unmount ;;
       -d|--debug) DEBUG=true ;;
       -h|--help) printf "$help_text" ; exit $RET_HELP ;;
       --) break ;;
       *)  echo "Internal error!" ; exit 1 ;;
   esac
done
echo options left $#
echo Debug: $DEBUG Mode: $mode  Server_info: $server_info

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 03-24-2014
List should have an optional (IF any at all) argument, saying, if one provides the servername or ip, it lists just that one, otherwise (empty,missing) it lists all.
So assume to 'remove' the argument requirement, but check if anything else were supplied..

Changes:
Code:
eval set -- $(getopt -l edit:,list,mount:,unmount:,help,debug -o e:l:m:u:hd -- "$@")

And then check within the 'list' function like [ -z $server_info ] && echo 'list all' || echo 'list only $server_info'

And thanks to the local example and Chubler's adaption of my code, i finaly understood that shift thing.

Thank you guys, you're the best! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts help

Hi All, I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working. Can someone please let me know what wrong in this code. function1()... (4 Replies)
Discussion started by: sidh_arth85
4 Replies

2. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

3. UNIX for Dummies Questions & Answers

Getopts

while getopts v OPTION do case $OPTION in v) echo "Hello" ;; *) exit 1;; esac done Suppose I have script tmp.sh Whose Signature is tmp.sh <fixed_argument> When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
Discussion started by: Devendra Hupri
1 Replies

4. Shell Programming and Scripting

[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED -- Heyas, Getting used to optargs, but by far not understanding it. So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals. Passing: browser -t test -d sect $HOME Where -t should change the title, and -d... (0 Replies)
Discussion started by: sea
0 Replies

5. Shell Programming and Scripting

bash:getopts command help

How can I say one of the options is required? can I use an if statement? let say: while getopts ":c:u:fp" opt; do case $opt in c) echo "-c was triggered, Parameter: $OPTARG" >&2;; u) echo "-u was triggered, Parameter: $OPTARG" >&2;; f) echo "-u was triggered,... (2 Replies)
Discussion started by: bashily
2 Replies

6. Shell Programming and Scripting

? used in getopts

Suppose I have a code below . while getopts a: opt do case $opt in a) app_name="$OPTARG";; *) echo "$opt is an invalid option"; exit 1;; ?) echo "The value of $OPTARG is an invalid option"; exit 1;; esac done Could anyone please tell me in which case my... (1 Reply)
Discussion started by: maitree
1 Replies

7. Shell Programming and Scripting

[bash] getopts not executing when called second time.

Hi, Unexpectedly, the function below doesn't seem to work when called a second time. The output shows that when the function is called the first time, it works as expected but when it is called a second time, the loop that processes the options passed to the function, with the while loop,... (2 Replies)
Discussion started by: ASGR
2 Replies

8. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

9. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

10. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question