I'm writing a bash shell script to 'help' me post to susepaste (I can NEVER remember the time options).
Here's the 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:
...
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
...
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.