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
Help in getopts chella Shell Programming and Scripting 4 11-02-2007 01:09 AM
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 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 Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-25-2005
yerra yerra is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 13
getopts

I have a script which fires a command based on certain parameters. I am posting the code below..

The options needs be given such that
-u option goes along with -d and -s, -f goes with -d and -t goes with -s and -d.
1) How do I ensure that user misses any of the option then he should be prompted . Is it simple if then else where I check the length of string of all these options.

USAGE="Usage: `basename $0` [-s schemaname] [-d databasename] [-u username] -f -t -h"
while getopts :s:d:u:fth params
do
case $params in

s) SOURCESCHEMA="$OPTARG" ;;

f) FULL='y' ;;

t) TABLE='y' ;;

d) DBNAME="$OPTARG" ;;

u) USERNAME="$OPTARG" ;;

h)
help_doc
exit 0
;;

?|
echo "Invalid Option Specified"
echo "$USAGE" 1>&2 ; exit 1
;;

esac

done


Thanks..
  #2 (permalink)  
Old 03-25-2005
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
You could do it a couple ways, one - by adding a checksum count to each case. At the end of the getsopts statement and before any other code is executed, check to see if the checksum value is what is expected. If you dont like this approach, take the easy way and evaluate $#. $# holds a count of the number of command line arguments passed to the script.
  #3 (permalink)  
Old 03-26-2005
yerra yerra is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 13
I added this line at the end of the script (after getopts), maybe the approach is wrong..

if [ $# -eq "3" ]; then
"Option 1 specified -s -d -u"
elif [ $# -eq "2" ]; then
"Option 2 specified -d and -f"
fi

However $# always evaluates to 6 if provide the option
scriptname -d databasename -u username -s schemaname. How did it arrive at 6 ?
  #4 (permalink)  
Old 03-26-2005
yerra yerra is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 13
I think I got it now.
If i provide scriptname -s schema -u user -d database, $# evaluates to 6 based on no of options and its values. (-s schema) 2, (-u user) 2 and (d database ) 2.

So if it is -d database -f it evaluates to 3 (-d database)2 and (-f) 1.
If he specifies -d database and -t ( That too evaluates to 3) .

How do i check if he has specified -t or -f then ?
  #5 (permalink)  
Old 03-26-2005
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
I didnt test this at all, just a thought I had. Im sure there are a million ways to do this.

Code:
USAGE="Usage: `basename $0` [-s schemaname] [-d databasename] [-u username] -f -t -h"
while getopts :s:d:u:fth params
do
   case $params in
     s) SOURCESCHEMA="$OPTARG"
        let COUNT= $COUNT+10000
         ;;
     f) FULL='y'
         ;;

     t) TABLE='y'
         ;;

     d) DBNAME="$OPTARG"
         let COUNT= $COUNT+1
        ;;

     u) USERNAME="$OPTARG"
         let COUNT= $COUNT+1000
         ;;
     h) 
         help_doc
         exit 0 
         ;;
     ?) 
         echo "Invalid Option Specified"
         echo "$USAGE" 1>&2 ; exit 1 
         ;;
   esac

if [ "$COUNT" -ne 10101 ]
  then
    help_doc
    exit 0
elif [ ! -z $FULL ]
   then
      echo "Paramater FULL is Set"
elif [ ! -z $TABLE ]
   then
      echo "Paramater TABLE is Set"
else
    echo "Neither TABLE or FULL Are Set"
fi
  #6 (permalink)  
Old 03-26-2005
yerra yerra is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 13
I was able to do it with the $#. Thanks for the help..
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 04:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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