The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




Thread: Help in getopts
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 11-01-2007
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
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> [-n]
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 as mentioned above), the option part of the code is not working resulting me with no action.
But when i execute the script in this way, I am getting my desired output
ksh my_script.sh -n <inputfile>
But this is not the way i should mention them.

Why the option given at the end is not performing its action.? Is it a rule for getopts or problem with my code?

Please help me regarding this.

Sample code:

#! /bin/ksh
while getopts 'n' option
do
case "$option" in
n)echo "option is n"
;;

?) echo " Bad option specified...."
;;
esac
done

shift `expr $OPTIND - 1`
INPUTFILE=$1
echo $INPUTFILE

Thanks in advance,
Chella