LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 11:10 AM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

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, 02: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, 02:18 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

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



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 07:59 PM
SUSE 9.2 eth0 "Waiting for mandatory devices..." DHCP problems ozric Linux - Hardware 2 03-29-2005 01:07 PM
getopts multiple parameters arnulfo Programming 2 12-23-2004 05:12 AM
Mandatory Doom 3 "GOLD" Thread - It's done! PhoenixOne Linux - Games 27 08-04-2004 11:43 PM
Help with getopts command Rezon Programming 3 10-22-2003 04:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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
Open Source Consulting | Domain Registration