LinuxQuestions.org
Help answer threads with 0 replies.
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-26-2011, 01:03 PM   #1
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Rep: Reputation: 31
Need Help with getopts


Hi,

I am trying to use getopts on bash. Operating System is Mac 10.6.7

The aim is to test if an argument is provided with an option then it should take that or else if should take the default value defined in the script.

Currently if I provide the options with arguments it works but if I don't provide an argument, it takes next option as the argument for first option.

Here is the script:

$cat npc
Quote:
#!/bin/bash
user=parag
rh=redhat

while getopts "r:u:" os
do
case "$os" in
r) if [ $OPTARG ]; then oss=$OPTARG; else oss=$rh; fi ;;
u) if [ $OPTARG ]; then usr=$OPTARG; else usr=$user ; fi ;;
esac
done

echo $oss
echo $usr
Quote:
$ ./npc -r redhat -u pk
redhat
pk

Quote:
$./npc -r -u
-u
In last example can we make it print "redhat" and "parag"
 
Old 03-26-2011, 08:32 PM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
Consider your logic. If someone uses "-r something", there
will always be an $OPTARG. So the test with the $OPTARG
value will always be true. If someone doesn't use the -r
option, that test will never run.

So you'd want to set your default values before reading
any input. Then if someone selects an option, assign the
new value to the variable.

Does this make sense?
 
Old 03-26-2011, 08:42 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...or maybe just parse args, throw an error and exit when OPTARG starts with a dash?:
Code:
[ "${OPTARG:0:1}" = "-" ] && { echo "A chair is still a chair
Even when there's no one sitting there
But a chair is not a house
And a house is not a home
(exiting)." >/dev/stderr; exit 127; }
 
Old 03-26-2011, 08:57 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by unSpawn View Post
...or maybe just parse args, throw an error and exit when OPTARG starts with a dash?:
Code:
[ "${OPTARG:0:1}" = "-" ] && { echo "A chair is still a chair
Even when there's no one sitting there
But a chair is not a house
And a house is not a home
(exiting)." >/dev/stderr; exit 127; }
No! Use carltm's advice. The point of getopt is that it will decide if there is an error on input. Otherwise, what is the point of using getopt if one must "parse" the input themselves?
 
Old 03-26-2011, 09:17 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by dwhitney67 View Post
No! Use carltm's advice.
Ah, a connoisseur...

Code:
~]$ sh script.sh -r -u 
-u
..and with check
Code:
+check_opts() { [ "${OPTARG:0:1}" = "-" ] && { echo "Invalid arg, exiting." >/dev/stderr; exit 127; }; }
 while getopts "r:u:" os
 do
 case "$os" in
-r) if [ $OPTARG ]; then oss=$OPTARG; else oss=$rh; fi ;;
-u) if [ $OPTARG ]; then usr=$OPTARG; else usr=$user ; fi ;;
+r) if [ $OPTARG ]; then check_opts; oss=$OPTARG; else oss=$rh; fi ;;
+u) if [ $OPTARG ]; then check_opts; usr=$OPTARG; else usr=$user ; fi ;;
this becomes:
Code:
~]$ sh script.sh -r -u 
Invalid arg, exiting.
 
  


Reply

Tags
getopts option arguments



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
getopts script m4rtin Programming 12 12-14-2009 08:47 AM
getopts kj6loh Programming 4 09-10-2009 03:53 PM
question on getopts tostay2003 Programming 1 12-25-2007 04:39 AM
struggling with getopts mike9287 Programming 6 07-14-2006 12:42 AM
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 02:25 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