getopts case statement failure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts case statement failure
# 1  
Old 11-27-2009
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  
Old 11-27-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Restarting shell script in case of a failure

I have a shell script which sqoops data from one place to hive and it does in 2 groups list. If one group is completed it waits for the second to get completed then goto to orc load, but it it fails it kills the other groups and sends a fail email. What I was looking for is if 1 group fails, it... (1 Reply)
Discussion started by: Kunalcurious
1 Replies

2. UNIX for Dummies Questions & Answers

Case statement help

Hi I am new to shell scripting, I wanted to make a shell script that has a case statement asking the user to select their city 1)london 2)tokyo 3) etc., I then want the users input to be stored in a variable and echoed out in another script; so for example if the user selects tokyo, tokyo city code... (2 Replies)
Discussion started by: scriptnewbie
2 Replies

3. Shell Programming and Scripting

Using default value with getopts and case

Hey Guys I have a program in shell which is reading default values from a file filename: default MAN=Value1 MANPD=997 REPPD=P1G6 Now the code calling is #!/bin/sh . /home/default while getopts t:D: name do (4 Replies)
Discussion started by: its
4 Replies

4. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

5. Shell Programming and Scripting

IF statement failure

Hi Guys, i have a variable which hold a string value of "in" when i try to compare this value against another value the if statement is not working. $a=in if then echo "Not a Walid match" else echo "Walid Match" fi Thank You. (9 Replies)
Discussion started by: nitinrp1
9 Replies

6. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

7. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

8. Shell Programming and Scripting

case statement

hi all i'm writing a script and in it i need to prompt the user if the entered value is correct or not ,i wrote the following and its not working ,its executing the script even if i enter Y/N pls any help is appreciated echo "\nAre you sure you entered the right Destination Environment? y :... (5 Replies)
Discussion started by: bkan77
5 Replies

9. Shell Programming and Scripting

Case Statement

Can anyone please tell me why this wont work! Thanks so much! #!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target (19 Replies)
Discussion started by: Zeta_Acosta
19 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question