Validating month value in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validating month value in shell script
# 1  
Old 09-29-2010
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 :

Code:
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

Moderator's Comments:
Mod Comment Please use CODE-Tags when posting code or terminal output. Thank you.

Last edited by bakunin; 09-29-2010 at 06:09 AM..
# 2  
Old 09-29-2010
Somrthing like this,

Code:
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"

# 3  
Old 09-29-2010
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
# 4  
Old 09-29-2010
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
Quote:
date: invalid date `20100228 12:62'
Like this:

Code:
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
# 5  
Old 09-29-2010
hi milink,

Quote:
I checked the range values for month, but its not accepting 19 or 29 etc.
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
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy the previous month files using shell script?

could you please assist the below query. i had written the below piece of code to copy the files from one directory to another. For current month files had been copied ,unfortunately the previous month files not copied. Please find the below directory structure:- ls -lrt total 1824... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

Run shell script alternate week per month

Hi All, Requirement :- I have shell script in kern shell ,have to run alternate week per month example-today's date is 27 Mar 2013 and my script will run today then for the next time when it will run it should check 1st what was the last run date(27 Mar 2013) if it is matched 15days from... (2 Replies)
Discussion started by: krupasindhu18
2 Replies

3. Shell Programming and Scripting

Help with shell script on Month Changing Logic

Hi Unix Experts, Happy Morning to all !! :) I am new to UNIX Shell Scripting and at my begineer level. To get acquainted to scripting, I am trying to create a script. The details/requirements of my script was to create a script with month changing logic in it so that on every 6th Working... (3 Replies)
Discussion started by: micky3112
3 Replies

4. Answers to Frequently Asked Questions

Finding the nth Particular Week in a Month – shell script

I see lot of request posted in internet to find out the day of nth week in a Month. example: what is the date of 3rd Sunday in October What is the date of 2nd Friday in June 2012 what is the date of 4th Saturday in January 2011..etc.. The below shell script is used to find out the... (1 Reply)
Discussion started by: itkamaraj
1 Replies

5. Shell Programming and Scripting

Moving log files based on month - help with ksh shell script

Hello, I'm trying to move the log files from the parent directory to respective monthly folder and I would be running this script on a weekly basis through cron. I'm new to this scripting and here is what i could come up and it runs without really doing anything. I even tried placing echo... (2 Replies)
Discussion started by: jusblues
2 Replies

6. Shell Programming and Scripting

validating(pingable or not) remote ip address in shell script

i need to verify whether the ip adress given as input to the shell script is pingable or not... that is whether the ip is alive and responding.. ping $ip_adress the above wont work in script because the execution is continuous... so the shell script keeps will dwell in this pinging process...... (8 Replies)
Discussion started by: vivek d r
8 Replies

7. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

8. Shell Programming and Scripting

Shell script for validating fields in a file

Hi, I have not used Unix in a very long time and I am very rusty. I would appreciate any help I can get from the more experienced and experts in Shell script. I am reading one file at a time from a folder. The file is a flat file with no delimeters or carriage return. Col1 through col6 is... (5 Replies)
Discussion started by: asemota
5 Replies

9. Shell Programming and Scripting

Validating variables in shells script

All Can you help me to validate a variable only for string and digit. That is variable should either fully alphabets or digits. Please send me result to my mail id also: REMOVED Thanx in advance Regards Deepak Xavier (1 Reply)
Discussion started by: DeepakXavier
1 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question