Validate long date format in If statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validate long date format in If statement
# 1  
Old 12-31-2008
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):

Code:
 
#!/usr/bin/ksh
INPUT_DATE=$1
FORMATTED_DATE=`date | cut -f2 -d' '`
#If there's no input use the sysdate otherwise use the input
if [[ -z "$INPUT_DATE" ]] ; then
   MONTH_TO_PROCESS=$FORMATTED_DATE
else
   MONTH_TO_PROCESS=$1
fi
echo $MONTH_TO_PROCESS
 
if [[ "$MONTH_TO_PROCESS" = "Feb" || "$MONTH_TO_PROCESS" = "Jan" || "$MONTH_TO_PROCESS" = "Mar"]]; then
   echo "$MONTH_TO_PROCESS is Valid"
else
   echo "$MONTH_TO_PROCESS is Invalid"
fi

But when I use this for every month (jan - dec), the list is too long and I get an error.

This is what I want (but this is too long), how can i validate the month or cut all the or statements over multiple lines, I tried // but that doesn't work:

Code:
if [[ "$MONTH_TO_PROCESS" = "Feb" || "$MONTH_TO_PROCESS" = "Jan" || "$MONTH_TO_PROCESS" = "Mar" || "$MONTH_TO_PROCESS" = "Apr" || "$MONTH_TO_PROCESS" = "May" || "$MONTH_TO_PROCESS" = "Jun" || "$MONTH_TO_PROCESS" = "Jul" || "$MONTH_TO_PROCESS" = "Aug" || "$MONTH_TO_PROCESS" = "Sep" || "$MONTH_TO_PROCESS" = "Oct" || "$MONTH_TO_PROCESS" = "Nov" || "$MONTH_TO_PROCESS" = "Dec"]]; then

# 2  
Old 12-31-2008
Solved my problem, with a Case:

case $MONTH_TO_PROCESS in
Jan) echo "Valid" ;;
Feb) echo "Valid" ;;
Mar) echo "Valid" ;;
Apr) echo "Valid" ;;
May) echo "Valid" ;;
Jun) echo "Valid" ;;
Jul) echo "Valid" ;;
Aug) echo "Valid" ;;
Sep) echo "Valid" ;;
Oct) echo "Valid" ;;
Nov) echo "Valid" ;;
Dec) echo "Valid" ;;
*) echo "$MONTH_TO_PROCESS is not a valid month, this should be one of Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec"
exit;;
esac
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

2. Shell Programming and Scripting

Validate date file format using awk

Here is my sample data Test.txt column 1|columne 2|start Date|end Date test|test|03/24/2014|03/24/2014 test|test|03/24-2014|03/24/2014 test|test|03/24/2014|03/24/2014 test|test|03/24/2014|03/24-2014 test|test|03/24/2014|03/24/2014 Now in the file i am expecting the date fields should be... (4 Replies)
Discussion started by: krish2014
4 Replies

3. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

4. Shell Programming and Scripting

Need script to validate file according to date

Hi All, I am very new to unix and just started to work with unix and shell scripting.I have a query anyone help would be much appreciated I am using sun solaris OS I want to validate a file according to its date and if validate successful then it would write the file name,size,date and... (3 Replies)
Discussion started by: sv0081493
3 Replies

5. Shell Programming and Scripting

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 (3 Replies)
Discussion started by: qamar.alam
3 Replies

6. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

7. Shell Programming and Scripting

Validate date format in argument

Hi.. I'm a newbie in KSH. Need help. How can I validate input date format in argument using format mm/dd/yy. Eg. > findlog.sh 12/30/09. Any info/help is highly appreciated. (3 Replies)
Discussion started by: harry0013
3 Replies

8. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

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

10. Shell Programming and Scripting

Validate date

Hi I am beginner to UNIX. I wanted to help for how to write script to validate date. Please help me. (2 Replies)
Discussion started by: giridher2000
2 Replies
Login or Register to Ask a Question