net help with getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting net help with getopts
# 1  
Old 03-02-2010
net help with getopts

Hi

I have written the following script.
But the variable sid is not getting set why is that.

When i run the script i get the output as

ORATAB FOR is

Instead if i run the script like

test.sh xiamin

i am expecting the output as

ORATAB FOR xiamin is


Code:
#!/bin/ksh
while getopts s: opt;
do
  case ${opt} in
    s) sid=${OPTARG} ;;
    \?)echo "Usage : get_ora_home  [-s ORACLE_SID]"
       return 1;;
  esac
done


if [ $# -ne 1 ] ; then
  echo "Usage: $(basename $0) [-s ] ORACLE_SID "
  return 1
fi


echo "ORATAB FOR $sid is"

# 2  
Old 03-02-2010
See the changes highlighted below:

Code:
#!/bin/sh
while getopts s: opt;
do
  case ${opt} in
    s) sid=${OPTARG} ;;
    \?)echo "Usage : get_ora_home  [-s ORACLE_SID]"
       exit 1 ;;
  esac
done
if [ $# -ne 2 ] ; then
echo "Usage: $(basename $0) [-s ] ORACLE_SID "
exit 2
fi
echo "ORATAB FOR $sid is"


cheers,
Devaraj Takhellambam

Last edited by pludi; 03-02-2010 at 08:12 AM.. Reason: added code tags
# 3  
Old 03-02-2010
Try this,

Code:
#!/bin/sh
while getopts "s:" opt
do
        case ${opt} in
                s) sid=${OPTARG} ;;
                *) echo "Usage : get_ora_home  [-s ORACLE_SID]"
                    exit 2 ;;
        esac
done

if [ $# -le 1 ] ; then                   # or it may be  [ $# -ne 2 ]
        echo "Usage: $(basename $0) [-s ] ORACLE_SID "
        exit 1
fi

echo "ORATAB FOR  $sid is"


Last edited by Nila; 03-12-2010 at 07:51 AM..
# 4  
Old 03-02-2010
Hi

Cannot use exit 1 as this is a fucntion and not a program in itself

If i set the line to [ $# -ne 2 ] or to [ $# -ne 1 ] how do i call this function from command line


Code:
#!/bin/sh
while getopts s: opt;
do
case ${opt} in
s) sid=${OPTARG} ;;
\?)echo "Usage : get_ora_home [-s ORACLE_SID]"
exit 1 ;;
esac
done
if [ $# -ne 2 ] ; then
echo "Usage: $(basename $0) [-s ] ORACLE_SID "
exit 2
fi
echo "ORATAB FOR $sid is"

regards
# 5  
Old 03-02-2010
It checks if the no of arguments passed in the script is 2 or not with
Code:
$# -ne 2

If the number of arguments is not 2, then the script will exit with the exit message specified.

You can execute the script as "script.sh -s xiamin"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Getopts

while getopts v OPTION do case $OPTION in v) echo "Hello" ;; *) exit 1;; esac done Suppose I have script tmp.sh Whose Signature is tmp.sh <fixed_argument> When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
Discussion started by: Devendra Hupri
1 Replies

2. Shell Programming and Scripting

Using getopts. Need help

Hi all... I have been looking on here for the past few days for an answer and Im gonna have to break down and ask. I just learned about the getopts command last week so have been trying to utilize it in my scripts. Below, I am trying to set up a case structure for options using getopts.... (1 Reply)
Discussion started by: losingit
1 Replies

3. UNIX for Dummies Questions & Answers

Getopts

Hey, i need help with the use of getopts in shell script. tried reading a lot online, but found incomplete examples (maybe complete but cudn't make out). PLzz help...explain in deatil plzzz, i am a newbie:confused: (3 Replies)
Discussion started by: SasankaBITS
3 Replies

4. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

5. Shell Programming and Scripting

Using getopts

I am having some trouble/questions with getopts that I can't find any solid info on with google I need it to parse things of the syntax of: -r # # # -f -c with as many repeats as possible, and it should catch erroneous commands also, but continue going... my first question is, -r... (3 Replies)
Discussion started by: TurboArkhan
3 Replies

6. HP-UX

using getopts

Is there a restriction on levels of using 'getopts' ? I have several scripts, each of which requires an option as the first parameter . If I call one prg separately it works fine, but when one prg calls another prg and passes the option on the called prg, then the called prg seems not to process... (3 Replies)
Discussion started by: vslewis
3 Replies

7. Shell Programming and Scripting

Net::SSLeay or Net::FTPSSL

Hello, I ran into an issue in one of my monitoring scripts. If I use the public ip address in my connection string everything works, but if I switch the connection string ip to 127.0.0.1 or the internal ip I get, " Connection refused at... (1 Reply)
Discussion started by: Styles
1 Replies

8. Shell Programming and Scripting

Help in getopts

Hi, My script will take a input file as a parameter(which is not mandatory) and also an option. ksh my_script.sh <inputfile> The option -n I have given is no way related to the input file. Now the problem here is when i execute the script specifying the input file and the option(the way... (4 Replies)
Discussion started by: chella
4 Replies

9. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

10. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question