LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-11-2012, 09:49 PM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 76

Rep: Reputation: 0
Question Using getopts in Shell properly??


I have a program which is basically written in sh (has to be) and it is trying to read inputs from the command line and if no input is given then it takes the default value.

Here is the code

Code:
#!/bin/sh

if [ -f /.default ]
then
. /.default
fi

#Set Default Values from the file
DN=$DEFAULTDN
PD=$DEFAULTPD
RPD=$DEFAULTRPD
HOSTNAME=`hostname`

while getopts D:w:r:h:p:i:t: name
do
  case $name in
    t)  TYPE=$OPTARG;;
    D)  DN=$OPTARG;;
    w)  PD=$OPTARG;;
    r)  RPD=$OPTARG;;
    h)  HOSTNAME=$OPTARG;;
    p)  if [ $TYPE = option1 ]
        then
        NUM=747
        elif [ $TYPE = option2 ]
        then
        NUM=777
        fi
        ;;
    i)  if [ $TYPE = option1 ] ; then
                IPATH='/net/install/$HOSTNAME'
        elif [ $TYPE = option2 ] ; then
                IPATH='/net/install2/$HOSTNAME-opt2'
        fi
        ;;

    ?)  usage
        exit 1
        ;;
  esac
done

echo $TYPE
echo $DN
echo $PDN
echo $HOSTNAME
echo $NUM
echo $IPATH
Basically if the file is called test.sh I want the user to run it with mininum commands

Code:
./test -t (option1|option2)
Once he specifies that, and if all the other are not specified (i.e. D w r h etc) then it will take the default.. If even -D -w -r etc are mentioned then it overrides the default value

This script is not working. It is sourcing correctly from the file and assigning the default value and echo out is correct. But I think because I am not mentioning -D -w -r etc , the getopts does not work

Can anyone suggest how to get this working??

Ta
 
Old 01-12-2012, 01:15 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
You will have to define further, "does not work"? Seems to provide the output I would expect based on script and input.
 
Old 01-12-2012, 07:15 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
If I get it right, the options p and i shouldn’t get a colon behind them as you don’t use $OPTARG in these cases. If an option like -p isn’t specified, its case won’t get executed. Do you referring to a missing set $NUM and $IPATH? Maybe these two should just go behind the case statement as they should always be executed.
 
  


Reply

Tags
getopts, sh, shell


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
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
How to properly exit functions in Shell Script? Jude Terror Programming 5 01-27-2009 03:15 PM
login shell not started properly powah Linux - Software 2 07-13-2007 03:21 PM
Backup shell script not working properly jamespetts Linux - Networking 2 07-15-2006 11:03 AM
Shell script not functioning properly jordanmc31 Programming 21 04-17-2006 06:35 AM


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