LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-03-2009, 03:28 PM   #1
jmcejuela
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, 03:42 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
As the thread title implies, getopt is the way to go. Here's a simple example:
Code:
#!/bin/bash

set -e

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

eval set -- "$OPTS"

while true
do
        case "$1" in
                -p|--port)  echo "port = $2"; shift 2 ;;
                -h|--host)  echo "host = $2"; shift 2 ;;
                --)         shift ; break ;;
        esac
done
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 03:47 PM.
 
Old 04-03-2009, 03:54 PM   #3
jmcejuela
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?

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

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

Thank you so much.
 
  


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
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
getopts - displaying warnings/errors when extraneous arguments are provided retrovertigo Programming 5 07-18-2007 03:44 PM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 06:38 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 02:18 PM


All times are GMT -5. The time now is 09:28 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration