Sponsored Content
Top Forums Shell Programming and Scripting Validate long date format in If statement Post 302272575 by Batsies on Wednesday 31st of December 2008 02:38:10 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
FUNCTION::CTIME(3stap)					      Time utility functions					    FUNCTION::CTIME(3stap)

NAME
function::ctime - Convert seconds since epoch into human readable date/time string SYNOPSIS
ctime:string(epochsecs:long) ARGUMENTS
epochsecs Number of seconds since epoch (as returned by gettimeofday_s) DESCRIPTION
Takes an argument of seconds since the epoch as returned by gettimeofday_s. Returns a string of the form "Wed Jun 30 21:49:08 1993" The string will always be exactly 24 characters. If the time would be unreasonable far in the past (before what can be represented with a 32 bit offset in seconds from the epoch) an error will occur (which can be avoided with try/catch). If the time would be unreasonable far in the future, an error will also occur. Note that the epoch (zero) corresponds to "Thu Jan 1 00:00:00 1970" The earliest full date given by ctime, corresponding to epochsecs -2147483648 is "Fri Dec 13 20:45:52 1901". The latest full date given by ctime, corresponding to epochsecs 2147483647 is "Tue Jan 19 03:14:07 2038". The abbreviations for the days of the week are 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', and 'Sat'. The abbreviations for the months are 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', and 'Dec'. Note that the real C library ctime function puts a newline (' ') character at the end of the string that this function does not. Also note that since the kernel has no concept of timezones, the returned time is always in GMT. SystemTap Tapset Reference June 2014 FUNCTION::CTIME(3stap)
All times are GMT -4. The time now is 02:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy