validate case statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting validate case statement
# 1  
Old 03-07-2012
validate case statement

Hello,

I need to take the input from user on below format and validate it.

ddd:hr:mm where ddd=mon-sun(days) , hr:hour (0-23) , mm=min(0-59).
Please assist me for getting input from user.

Thanks
Qamar
# 2  
Old 03-07-2012
Code:
OLDIFS="$IFS"
IFS=":"
while true
do
        printf "Enter time: "
        read DDD HH MM

        case "$DDD" in
                sun|mon|tue|wed|thu|fri|sat|sun)  ;;
                *) continue ;;
        esac

        case "$MM" in
                [0-5][0-9]) ;;
                *) continue;;
        esac

        case "$HH" in
                [0-1][0-9]) ;;
                2[0-3]) ;;
                *) continue;;
        esac

        break
done

IFS="$OLDIFS"


Last edited by Corona688; 03-07-2012 at 05:52 PM.. Reason: typo
These 3 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 03-07-2012
Nice solution corona688.

@qamar.alam, you can also print error/help messages before calling continue eg:

Code:
    *) echo "Hour must be between 0 and 23 - please reenter" ; continue ;;

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 03-07-2012
Thank you so much corona688... It's really good and straight forward solution.

Last edited by qamar.alam; 03-08-2012 at 02:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 Replies

3. Shell Programming and Scripting

case statement

Hi, I am writing case statement to execute some finction, my requirement is once one of the case statement is executed again it has to prompt for the option. for script in `echo "$Script_Selected"` do case $script in 1) getNoOFActUsers ;; 2) moveServerrOORotation ;; ... (2 Replies)
Discussion started by: Satyak
2 Replies

4. Shell Programming and Scripting

Validate long date format in If statement

Hi, I want to validate if the given input is a valid month (written in long month format) Jan / Feb / Mar / Apr / May / Jun etc etc This is what I've got with || (or statements): #!/usr/bin/ksh INPUT_DATE=$1 FORMATTED_DATE=`date | cut -f2 -d' '` #If there's no input use the... (1 Reply)
Discussion started by: Batsies
1 Replies

5. UNIX for Dummies Questions & Answers

CASE statement

Hi, I am writing a bash shell script. My script has a few user defined parameters. When the script runs the first thing it does is make sure that these parameters are valid. One of the parameters is called YEAR. A valid input for YEAR can be 1997-2000. One way I have come up with to ensure... (3 Replies)
Discussion started by: msb65
3 Replies

6. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 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