LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-09-2005, 12:10 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
Mandatory arguments with getopts?


Hi all,
I'm trying to create a shell script which has some optional parameters and ideally some that are mandatory, but I can't seem to figure out how to tell getopts that.
I'd like to have the potential for 6 parameters. The first is mandatory and takes an argument, the second mandatory, the third optional, the fourth optional and takes an argument, the fifth takes an argument and is optional on its own, but if 4 is there, 5 has to be also. The sixth is optional and takes an argument.

I'm having trouble with the "fifth is unnecessary on its own, but if 4 is there, 5 has to be also. "...is there any way to do this?
What I've got now is this (variable names changed to protect the innocent ):
Code:
while getopts ":s:dvu:p:n:" options; do
  case $options in
    s ) a=$OPTARG;;
    d ) b=$OPTARG;;
    v ) c=$OPTARG;;
    u ) d=$OPTARG;;
    p ) e=$OPTARG;;
    n ) f=$OPTARG;;
    \? ) echo $usage
         exit 1;;
    * ) echo $usage
          exit 1;;
  esac
done
Thanks!
 
Old 03-09-2005, 03:19 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
My understanding is that getopts won't do what you're trying to do.. you can easily use if..then's after the while..loop to determine if the user has entered the required information.

Firstly, according to the code getopts argument list (:s:dvu:p:n:), -s, -u, -p, and -n take a mandatory argument. In the case statement, the code assigns the value of OPTARG to b and c, even though these options are set to not recieve an argument. As such, these items will never hold a value. It might be better to assign them an arbitrary number, like 1 for true, or something:
Code:
while getopts ":s:dvu:p:n:" options; do
  case $options in
    s ) a=$OPTARG;;
    d ) b="1";;
    v ) c="1";;
    u ) d=$OPTARG;;
    p ) e=$OPTARG;;
    n ) f=$OPTARG;;
    \? ) echo $usage
         exit 1;;
    * ) echo $usage
          exit 1;;
  esac
done
This will at minimum, provide the script with a way to test for these.

And, as to the required options bit, here's a bit of incomplete sample code to show you how I might go about this:
Code:
#  test to see if they gave the -s option
#
if [ "x" == "x$a" ]; then
  echo "-s [option] is required"
  exit
fi

#  now, test for the -u option.  if they gave it, the
# -p option must also exist
#
if [ "x" != "x$d" ]; then
  if [ "x" == "x$e" ]; then
    echo "-u [option] requires -p [option]"
    exit
  fi
fi
That should do the job.

Happy coding!
 
Old 03-10-2005, 03:18 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
I played around with it a bit, and in the end used something very like what you did here.
Thanks very 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
Mandatory Service Pack 2 coming tuesday ?? BajaNick General 6 04-17-2005 08:59 PM
SUSE 9.2 eth0 "Waiting for mandatory devices..." DHCP problems ozric Linux - Hardware 2 03-29-2005 02:07 PM
getopts multiple parameters arnulfo Programming 2 12-23-2004 06:12 AM
Mandatory Doom 3 "GOLD" Thread - It's done! PhoenixOne Linux - Games 27 08-05-2004 12:43 AM
Help with getopts command Rezon Programming 3 10-22-2003 05:12 PM


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