LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Closed Thread
 
Search this Thread
Old 06-09-2011, 11:22 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"
./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:39 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mandatory and optional arguments nano2 Programming 7 06-10-2011 06:20 PM
BASH script using getopt to parse optional arguments nano2 General 6 04-28-2011 09:09 AM
getopts and mandatory arguments in BASH jmcejuela Programming 3 04-03-2009 05:00 PM
Optional arguments in Python forsaken_pariah Programming 1 03-10-2007 10:48 AM
Mandatory arguments with getopts? rose_bud4201 Programming 2 03-10-2005 03:18 PM


All times are GMT -5. The time now is 03:54 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration