LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 06-09-2011, 10:15 AM   #1
nano2
Member
 
Registered: May 2007
Posts: 100

Rep: Reputation: 15
mandatory and optional arguments


Hi ,
I am using getopts to take parse optional arguments but i also pass in mandatory arguments first
[code]
#!/bin/bash


print_usage()
{
perror "$@"
}
arg1="$1"
arg2="$2"
shift 2
while getopts f:d: opt
do
case $opt in
f) file="${OPTARG}";;
d) dir="${OPTARG}";;
?) print_usage $0
exit 1;;
esac
done
echo "Argument 1 = $arg1"
echo "Argument 2 = $arg2"
echo "File=$file"
echo "Dir=$dir"

[code]

./script arg1 arg2 -f file -d dir this works fine

But when i pass in the wrong number of mandatory arguments and pass in the optional argument the command line gets all messed up like so

./script arg1 -f BLAH -d DIR
Argument 1 = arg1
Argument 2 = -f
File=
Dir=

Any ideas how to stop this ???
 
Old 06-09-2011, 11:01 AM   #2
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
Do it the other way round which is also more common: first the optional arguments, and when they run out the mandatory ones follow. After the getopt you can use:
Code:
shift $((OPTIND-1))
to have $1 set to the first mandatory argument. You can also check $# then for the number of mandatory arguments.
 
Old 06-10-2011, 02:35 AM   #3
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
I would prefer to have the mandatory first and then followed by the options . IF so how can i get rid of the options getting missed up as mandatory ?
 
Old 06-10-2011, 03:34 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
Then you will have to read your mandatory arguments in a loop one by one (and shift only once after you handled the argument). If it starts with a "-" (or there is no additonal one), exit the loop and call the getopts for the remaining ones.
Code:
INDEX=0
while [ -n "$1" -a "${1:0:1}" != "-" ]; do
    ARGUMENT[$((++INDEX))]="$1"
    shift
done
if [ "$INDEX" -ne 2 ]; then
    echo "Error"
    exit 1
fi
echo "${ARGUMENT[1]}"
echo "${ARGUMENT[2]}"
 
Old 06-10-2011, 08:10 AM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,203
Blog Entries: 3

Rep: Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432Reputation: 1432
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 06-10-2011, 08:39 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by nano2 View Post
Hi ,
I am using getopts to take parse optional arguments but i also pass in mandatory arguments first
Code:
#!/bin/bash


print_usage()
{
    perror "$@"
}
arg1="$1"
arg2="$2"
shift 2
while getopts f:d: opt
do
   case $opt in
               f) file="${OPTARG}";;
               d) dir="${OPTARG}";;
               ?) print_usage $0
            exit 1;;
   esac
done
echo "Argument 1 = $arg1"
echo "Argument 2 = $arg2"
echo "File=$file"
echo "Dir=$dir"
./script arg1 arg2 -f file -d dir this works fine

But when i pass in the wrong number of mandatory arguments and pass in the optional argument the command line gets all messed up like so

./script arg1 -f BLAH -d DIR
Argument 1 = arg1
Argument 2 = -f
File=
Dir=

Any ideas how to stop this ???
I do not use getopts much but perhaps you might get an idea from this example:
Code:
#!/bin/bash

ARGS=()

while [[ $# -gt 0 ]]; do
	case "$1" in
	-f)
		FILE=$2
		shift
		;;
	-d)
		DIR=$2
		shift
		;;
	*)
		ARGS[${#ARGS[@]}]=$1
		;;
	esac
	shift
done

echo "Argument 1: ${ARGS[0]}"
echo "Argument 2: ${ARGS[1]}"
echo "Extra Arguments:" "${ARGS[@]:2}"

echo "File: $FILE"
echo "Directory: $DIR"
 
Old 06-10-2011, 11:22 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
@konsolebox: Yes, you can parse the (optional) argumemnts on your own, this works too. One advantage of using getopts is, that you can just concatenate several options like ls -lhrt instead of ls -l -h -r -t.
 
Old 06-10-2011, 05:20 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Reuti View Post
@konsolebox: Yes, you can parse the (optional) argumemnts on your own, this works too. One advantage of using getopts is, that you can just concatenate several options like ls -lhrt instead of ls -l -h -r -t.
yeah i know
i had used getopts .. 6 years ago but there was a reason why i stopped using it.. i already forgot.
 
  


Reply

Tags
bash scripting


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 script using getopt to parse optional arguments nano2 General 6 04-28-2011 08:09 AM
getopts and mandatory arguments in BASH jmcejuela Programming 3 04-03-2009 04:00 PM
Optional arguments in Python forsaken_pariah Programming 1 03-10-2007 09:48 AM
Mandatory Locking Kernel programmer Linux - Software 3 07-28-2006 05:21 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 02:18 PM


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