Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-03-2009, 04:28 PM   #1
LQ Newbie
Registered: Apr 2009
Posts: 6

Rep: Reputation: 0
getopts and mandatory arguments in BASH

Hi there,

I am writting a script and I want some optional arguments for what I use getopts, and a mandatory argument, so the usage of the script is:

script [OPTIONS] mandatory

How to reference the mandatory argument? I mean, in which variable is stored, $n ? After running getopts can I have an index or something like that?

Thanks for your time.
Old 04-03-2009, 04:42 PM   #2
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
As the thread title implies, getopt is the way to go. Here's a simple example:

set -e

OPTS=`getopt -n $0 -o p:h: --long port:,host: -- $@`

eval set -- "$OPTS"

while true
        case "$1" in
                -p|--port)  echo "port = $2"; shift 2 ;;
                -h|--host)  echo "host = $2"; shift 2 ;;
                --)         shift ; break ;;
Here, both -p (or --port) and -h (or --host) require one to input values; the ':' that is placed after the options p and h above, imply this. If an arg is not required, then don't use the ':' after the option.

Last edited by dwhitney67; 04-03-2009 at 04:47 PM.
Old 04-03-2009, 04:54 PM   #3
LQ Newbie
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
hmm I want it more easy. I have discovered that it is the OPTIND variable which is the index I was looking for, how to do something like ${$OPTIND} to reference to the bash variable that reference to the $OPTIND th argument?

Old 04-03-2009, 05:00 PM   #4
LQ Newbie
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
oh yeah, itīs simple done with shift.

Thank you so much.


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
BASH - problem with subsequent calls to function with getopts dkrysak Programming 3 03-25-2009 04:28 PM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 11:53 AM
getopts - displaying warnings/errors when extraneous arguments are provided retrovertigo Programming 5 07-18-2007 04:44 PM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 07:38 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 03:18 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:21 PM.

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