The UNIX and Linux Forums  


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



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Aug 2002
Location: London, England
Posts: 85
getopts case statement failure

Hi,

I have a getopts that works fine if I provide an option but I want it to exit if nothing is given, but for some reason it drops out and continues with the script. Any ideas?




Code:
while getopts d:m:y: o
do
        case $o in
                d) day=$OPTARG
                ;;
                m) month=$OPTARG
                ;;
                y) year=$OPTARG
                ;;
                :) echo "Option $OPTARG needs an argument"; usage
                ;;
                *) usage
                ;;
        esac

done


script_name.sh -d 25 -m Nov -y 09

Cheers and beers,
Neil
  #2 (permalink)  
Old 11-27-2009
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,434
Test the argument count :
Code:
if [ $# -eq 0 ]
then
        echo "No option!"
        usage      # Usage function must exit the script
fi

]while getopts d:m:y: o
do
        case $o in
                d) day=$OPTARG
                ;;
                m) month=$OPTARG
                ;;
                y) year=$OPTARG
                ;;
                :) echo "Option $OPTARG needs an argument"; usage
                ;;
                *) usage
                ;;
        esac

done

Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help with case statement rkruck Shell Programming and Scripting 1 06-03-2009 04:06 PM
ksh case statement dgilc Shell Programming and Scripting 2 12-16-2008 04:34 PM
case statement c00kie88 Shell Programming and Scripting 2 10-12-2008 06:08 PM
If or Case Statement Ernst UNIX for Dummies Questions & Answers 1 09-24-2008 06:00 PM
Case Statement Zeta_Acosta Shell Programming and Scripting 19 04-06-2004 05:16 PM



All times are GMT -4. The time now is 10:30 AM.


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