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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using getopts TurboArkhan Shell Programming and Scripting 3 06-22-2009 04:31 PM
getopts help GrepMe Shell Programming and Scripting 3 06-20-2007 11:47 AM
help in getopts problems Shell Programming and Scripting 1 05-04-2006 11:07 PM
getopts yerra Shell Programming and Scripting 5 03-26-2005 10:43 AM
getopts google Shell Programming and Scripting 3 12-05-2002 07:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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
  #2 (permalink)  
Old 11-01-2007
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
For getopts the options must always be specified before the arguments.
The syntax for your script must be : ksh my_script.sh [-n] <inputfile>

Jean-Pierre.
  #3 (permalink)  
Old 11-01-2007
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
Thank you very much for the reply.

Can you explain me wat happens wen the option is given at the last?

ksh my_script.sh -n

In this case also I have given the option at the last. This works fine. Am I in the rite track?

where is the problem actually?

Regards,
Chella
  #4 (permalink)  
Old 11-01-2007
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
If the option is specified after a normal argument, it is considered also as an argument.

In your case, you haven't specified the inputfile, the string "-n" is immediately after the command name so it is taken as an option.

Jean-Pierre.
  #5 (permalink)  
Old 11-02-2007
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
Quote:
Originally Posted by aigles View Post
If the option is specified after a normal argument, it is considered also as an argument.

In your case, you haven't specified the inputfile, the string "-n" is immediately after the command name so it is taken as an option.

Jean-Pierre.
Thank You for the reply.

Regards,
Chella
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0