Validating month value in shell script

Hi,

the below script is to get & check the correct values for minutes (0-59) that is inputed by user :

printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
        read MIN

        case "$MIN" in
        [0-9]|[0-5][0-9]) break 2;;
          *) 
        echo ""
     echo "Invalid minutes, please try again.";;
        esac

How to give the same range for day of month (1-31) and for month (1-12) and hour (0-23) ???

Also, after getting user input, how to get it displayed as below :

Cronjob scheduled on "month day_of_month day_of_week hour minute "

With Regards

Somrthing like this,

printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
read MIN

case "$MIN" in
[0-9]|[0-5][0-9]) echo "valid min $MIN"; break 2;;
*)
echo ""
echo "Invalid minutes, please try again.";;
esac

printf "$FBOLD\nPlease enter the month (0-12): $FREG"
read MON

case "$MON" in
0[1-9]|[0-9]|1[012]) echo "valid month $MON";break 2;;
*)
echo ""
echo "Invalid month, please try again.";;
esac

printf "$FBOLD\nPlease enter the day (0-31): $FREG"
read DAY

case "$DAY" in
0[1-9]|[0-9]|[12][0-9]|3[01]) echo "valid Day $DAY";break 2;;
*)
echo ""
echo "Invalid day, please try again.";;
esac

printf "$FBOLD\nPlease enter the hr (0-23): $FREG"
read HR

case "$HR" in
0[1-9]|[0-9]|1[0-9]|2[0123]) echo "valid hour $HR";break 2;;
*)
echo ""
echo "Invalid hour, please try again.";;
esac

y=`date '+%Y'`
day_of_week=`date -d $MON/$DAY/$y '+%u'`

echo "Cronjob scheduled on $MON $DAY $day_of_week $HR $MIN"

Hi,

I checked the range values for month, but its not accepting 19 or 29 etc.

actually it should accept the values from 1 to 31 only.

With Regards

Hi, I think You can let the date command do the work for You. If it's a valid date, it will echo the "Cronjob... " line, if it's an invalid date, You will get an error, like

Like this:

echo -n Please enter MONTH DAY HOUR MINUTE :
read MONTH DAY HOUR MINUTE
date -d"$(date -d+%Y)${MONTH}${DAY} ${HOUR}:${MINUTE}" > /dev/null && echo "Cronjob scheduled on $MONTH $DAY $HOUR $MINUTE"

But maybe You would wish to do something more than just echo the text, then create a block of code. And You may have to adjust for the date and time format used in Your LOCALE.

Best regards,
Lakris

hi milink,

Slightly change in code for month and day (should not between 0-12 / 0-31)
month value should be between 1-12 and day 1-31

case "$MON" in
0[1-9]|[1-9]|1[012]) echo "valid month $MON";break 2;;
*)
echo ""
echo "Invalid month, please try again.";;
esac

printf "$FBOLD\nPlease enter the day (1-31): $FREG"
read DAY

case "$DAY" in
0[1-9]|[1-9]|[12][0-9]|3[01]) echo "valid Day $DAY";break 2;;
*)
echo ""
echo "Invalid day, please try again.";;
esac