Another validate input Question.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Another validate input Question.
# 1  
Old 08-03-2011
Error Another validate input Question.

I'm writing a bash shell script to 'help' me post to susepaste (I can NEVER remember the time options).
Here's the code:
Code:
#!/bin/bash

##########
# 
# Project     : personal script.
# Started     : Wed Aug 03, 2011 
# Author      : Habitual
# Description : susepaste c-li script with user input.
# Todo        : I need to validate "$EXPIRATION".
# 
##########

clear
echo -n "Post as: "
read NAME

echo -n "Post Title: "
read TITLE

echo -n "File to post: "
read FILE

echo -e "Expiration:"
echo -e "30m (Minutes)"
echo -e "1h (Hour)"
echo -e "6h (Hours)"
echo -e "12h (Hours)"
echo -e "1d (1 Day)"
echo -e "1w (1 Week)"
echo -e "1m (1 Month)"
echo -e "3m (3 Months)"
echo -e "1y (1 Year)"
echo -e "2y (2 Years)"
echo -e "3y (3 Years)"
echo -e "n (Never)"
read EXPIRATION

# An Array to validate $EXPIRATION?
# expiry=("30" "60" "360" "720" "1440" "10080" "40320" "151200" "604800" "1209600" "1814400" "0")

case $EXPIRATION in
    '30m') EXPIRATION="30" ;;
    '1h') EXPIRATION="60" ;; 
    '6h') EXPIRATION="360" ;; 
    '12h') EXPIRATION="720" ;; 
    '1d') EXPIRATION="1440" ;; 
    '1w') EXPIRATION="10080" ;; 
    '1m') EXPIRATION="40320" ;; 
    '3m') EXPIRATION="151200" ;; 
    '1y') EXPIRATION="604800" ;; 
    '2y') EXPIRATION="1209600" ;; 
    '3y') EXPIRATION="1814400" ;; 
    'n')  EXPIRATION="0" ;; 
esac

# test output during coding:
echo susepaste -t "$TITLE" -e "$EXPIRATION" -f bash -n "$NAME" "$FILE"
exit 0

# EOF

That works, but wanting to be a better coder/scripter, I want to 'validate' the $EXPIRATION, and re-prompt for a "valid" selection (choices in case statement)

Here's what I have tried:
Code:
...
if [ $? != $EXPIRATION ]; then
echo "Invalid entry: "
read answer
else
echo susepaste -t "$TITLE" -e "$EXPIRATION" -f bash -n "$NAME" "$FILE"
exit 0
fi
# EOF

and
Code:
...
case $EXPIRATION in
    '30m') EXPIRATION="30" ;;
    '1h') EXPIRATION="60" ;; 
    '6h') EXPIRATION="360" ;; 
    '12h') EXPIRATION="720" ;; 
    '1d') EXPIRATION="1440" ;; 
    '1w') EXPIRATION="10080" ;; 
    '1m') EXPIRATION="40320" ;; 
    '3m') EXPIRATION="151200" ;; 
    '1y') EXPIRATION="604800" ;; 
    '2y') EXPIRATION="1209600" ;; 
    '3y') EXPIRATION="1814400" ;; 
    'n')  EXPIRATION="0" ;; 
      *) echo "Try again "
      read $EXPIRATION
     ...
esac

I thought to validate the answer against an array, but I think that may not be the most efficient way to do this.

Thank you for your time.
# 2  
Old 08-03-2011
Add a case (*) to every case before esac to catch invalid data. Use both parens to keep vi % working for you.
# 3  
Old 08-03-2011
DGPickett:

so...
Code:
case $EXPIRATION in
    '30m') EXPIRATION="30" ;;
    (*) ;; 
    '1h') EXPIRATION="60" ;; 
    (*) ;; 
    '6h') EXPIRATION="360" ;; 
    (*) ;; 
...

?
# 4  
Old 08-03-2011
How about something like this:

Code:
#!/bin/bash
menu_items=(
    "30 Minutes" "1 Hour" "6 Hours" "12 Hours" "1 Day"
    "1 Week" "1 Month" "3 Months" "1 Year" "2 Years"
    "3 Years" "Never" )
 
items_period=(
    30 60 360 720 1440
    10080 40320 151200 604800 1209600
    1814400 0 )
 
PS3="Expiration:"
select s in "${menu_items[@]}"; do
    if [[ -z "$s" ]]
    then
        echo "Option $REPLY is invalid - Please select a number from 1 to ${#menu_items[@]}"
    else
        period=${items_period[$REPLY-1]}
        echo Period selected is $period
        break
    fi
done

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 08-04-2011
Chubler_XL:

That rocks.

http://susepaste.org/26599207

Last edited by Habitual; 08-04-2011 at 01:49 PM..
# 6  
Old 08-04-2011
Code:
case x in
(a) ... ;;
(b) ... ;;
(*)
     echo Not a or b ! >&2
     exit 1
     ;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Validate input files and update

We have a job which we need to run on daily bases, before loading data in a table we need to validate whether the input file is received or not.Inputfile formatsrc_sps_d_Call_Center_Reporting_yyyymmdd_01.dat SPS-Service nameYYYY-yearMM-MonthDD-dayLike above we will get n number of files for... (1 Reply)
Discussion started by: katakamvivek
1 Replies

2. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

3. Shell Programming and Scripting

Epic - Validate input size

Is there an easy way to validate an input field size. Let us say a script is asking to enter 10 digits mobile number, how do I write a script to validate it is numeric and is 10 digits in length? I just need an easy way w/o using looks ...etc. Is there such a away ? Here is what I have so far... (6 Replies)
Discussion started by: mrn6430
6 Replies

4. Shell Programming and Scripting

How to validate input parameters?

Hi, I wonder how I can know if the input parameters to the script are numbers or text Thanks (11 Replies)
Discussion started by: Gengis-Kahn
11 Replies

5. Shell Programming and Scripting

Validate and sort input

Hi, This will most likely be a simple answer. Currently I have a situation where my script will be sent various options: -o1 -o2 -oe3@somthing.com Now, if I want to run a certain command based on the option I am sent, I am doing the following. for o in $(echo $options) do if ... (3 Replies)
Discussion started by: stuaz
3 Replies

6. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

7. Shell Programming and Scripting

validate input

the user inputs names that have to be inside square brackets I want to check if the user puts the brackets and if not ask him to re-enter the names (9 Replies)
Discussion started by: DDoS
9 Replies

8. Shell Programming and Scripting

Need to validate a date input format

Hi all, I have a shell script(K shell) which takes a date as input. i want the input to be in DD-MM-YYYY format. Can i enforce such a format of input string using just one line of code? OR do i need to parse the input date into different components and test them using Case statements... (2 Replies)
Discussion started by: rajugp1
2 Replies

9. Shell Programming and Scripting

How to validate input values

Hi How would i validate value of a variable whether it is number,date or string Thanks in advance Sas (5 Replies)
Discussion started by: SasDutta
5 Replies

10. Shell Programming and Scripting

validate input from user for file name

Hello, This may have been addressed already somewhere, however I am looking for the easiest/shortest way to validate a response from a user for a file name. The file name should not have any of the following characters ~`!@#$%^&*()_-+={|\:;"'<,>.?/ Further the response should not have any... (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question