Exit if date not in correct format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit if date not in correct format
# 1  
Old 07-06-2010
Exit if date not in correct format

Can somone take a look at this script for me - I'm trying to get it to exit if the format of dateToLookFor is not in the format YYYYMMDD:

Code:
function search
{    
    cd $logsloc
    echo "Enter date in format YYYYMMDD (enter to exit):"
    read dateToLookFor
    echo $dateToLookFor | grep -q  "[0-2][0-9][0-9][0-9][01][0-9][0-3][0-9]"
    echo $?
    
    #test if exit status of previous command is 0
    #if not, exit
    if [[ $? -ne 0 ]]; then
        echo "Error: date entered does not match pattern YYYYMMDD -  exiting"
        exit
    fi
}

It just conducts a search for everything in the directory no matter what I type in (unless I type in the correct date format YYYYMMDD in which case it only looks at those files which is what i want).

Thanks
# 2  
Old 07-06-2010
Code:
function search
{    
    cd $logsloc
    echo "Enter date in format YYYYMMDD (enter to exit):"
    read dateToLookFor
    echo $dateToLookFor | grep -q  "[0-2][0-9][0-9][0-9][01][0-9][0-3][0-9]"
    status=$?
    echo $status
    
    #test if exit status of previous command is 0
    #if not, exit
    if [[ $status -ne 0 ]]; then
        echo "Error: date entered does not match pattern YYYYMMDD -  exiting"
        exit
    fi
}

In If statement $? gives status of echo statement. Thats why your script ran fine even if you pass wrong date
This User Gave Thanks to anbu23 For This Post:
# 3  
Old 07-06-2010
Many thanks anbu Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date not correct so it changed to current date.

Hello, so basically i have a txt file containing "foto's" named YYYY-MM-DD_HH.mm.ss.jpeg. But since it can probably not convert it it changes the date to the current date. What am i doing wrong? #!/bin/bash inputDateFmt() { sed -e 's/_/ /g' -e 's/\./:/g' <<< "$1" } begindatum=$(date... (3 Replies)
Discussion started by: Rovester
3 Replies

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

3. Shell Programming and Scripting

How to check if date format is correct?

Hi! how do i know if the input is the same as the required date format? the date should be dd/mm/YYYY ex. 2/3/2012 or 15/11/2012 all the following conditions must return an error: *input of string *day is > 31 or < 1 *month is > 12 or < 1 *year is < 2013 suppose the date format is stored... (1 Reply)
Discussion started by: angilulu
1 Replies

4. Shell Programming and Scripting

Send correct exit code from child script back to parent

Hello all; hope someone can help me cause I am going crazy trying to find a solution for (what I think is simple) issue...looked hard up and down this forum and tried several "solutions" with no avail...so here's my issue: I have this (parent) script: copylsofdcmcadefttosftpwithmove.sh ... (3 Replies)
Discussion started by: gvolpini
3 Replies

5. Shell Programming and Scripting

Correct a pattern format using sed

hi all I have a file with many lines like aa;bb;cc;dd;ee bb;aa;dd;ee;bb cc;ee;bb;dd;aa ee;cc;bb;aa;dd . . . etc The first line is in right format,Please help me how to use "SED" to change the rest of the lines to the format like line 1 thank all! (1 Reply)
Discussion started by: yuesko
1 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

Making a KSH exit if path is not correct

I am very green to shell programming and have no training and this is my first real attempt. I am fairly versed in Unix and running the cmds I need. I tried using the search feature but most of what I found was close but not quite what I am looking for, plus most looked more advanced than I... (7 Replies)
Discussion started by: htown71
7 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

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

10. Shell Programming and Scripting

Check for the correct date format in UNIx

Hi All, I am getting two input from User for Date from the command prompt when my script is executed . The date format i am taking is : DD-MM-YYYY so is there any method in Unix to validate the two input date. There might be many cases for these two date to be invalid.... (1 Reply)
Discussion started by: rawatds
1 Replies
Login or Register to Ask a Question