Checking variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking variables
# 8  
Old 06-28-2005
Code:
case $Date in
  [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) echo 'DATE correct' ;;
  *) echo 'DATE incorrect' ;;
esac

# 9  
Old 06-29-2005
Expanding on what Vino has already explained:

First, take a look on what your input COULD (legally) be. For instance, if your date format is "yyyymmdd":

"<year_number><1-12><1-31>"

Lets first stick with that and ignore for the moment, that February doesn't have 30+ days and that there are other dates too, which are illegal by that pattern: "20050431" for instance, because April has only 30 days.

Therefore our first shot at a regular expression matching dates could be:

Code:
 print - "$MyDateVar" | sed -n '/^[12][0-9]\{3\}[01][0-9][0123][0-9]/p'

if this prints anything it would be a date. The regexp matches:

a number "1" or "2", followed by three numbers 0-9 (the year), followed by
a number "0" or "1", followed by a number 0-9 (the month), followed by
a "0", "1", "2" or a "3", followed by a number 0-9 (the day).

Now for the year: right now we would match anything from 1000 to 2999. Could we further eliminate unwanted years? Yes! We will (presumably) have only to deal with dates of the last decade, so we could modify our regexp part for the year to:

Code:
[12][09][09][0-9]

This matches "1 or 2" followed by "0 or 9" followed by "0 or 9" followed by any digit. This still matches i.e. "2995", but it won't match "1875" or something such.

This way one could refine the regexp to even better spread the (syntactically) legal dates from the illegal ones. Upon request I will work out a complete solution.

bakunin
# 10  
Old 06-29-2005
Code:
$ Date=20050629

$ touch -d $Date file1 && echo 'DATE correct' || echo 'DATE incorrect'
DATE correct

$ Date=20050631

$ touch -d $Date file1 && echo 'DATE correct' || echo 'DATE incorrect'
touch: invalid date format `20050631'
DATE incorrect

# 11  
Old 06-29-2005
try
Code:
Date=19691231
Date=19700101
Date=20380119
Date=20380120

test on touch (coreutils) 5.0, correct date is from 1970/01/01 to 2038/01/19.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

6. Shell Programming and Scripting

checking variables + arithmetic operator help

i'm trying to make a script to simply add numbers together for example i have a file script file called add add 1 2 3 4 5 15 however i want the user to put at least 3 numbers so i did this if then echo "please enter more than two numbers" exit 1 fi but it's telling me i have... (3 Replies)
Discussion started by: el3ctr0nic47
3 Replies

7. Shell Programming and Scripting

Help with checking that 2 variables contain matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario eg ... (3 Replies)
Discussion started by: lsecer
3 Replies

8. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

9. Shell Programming and Scripting

naming variables with variables

Hello, FIRST QUESTION: I am writing a script in which a query is taken at the beginning of the script to be later used at the end. In the query, variables are generated from a loop, and I would like to assign the variable NAME (not value) with an appended 1, 2, 3, 4.....n. The number of... (2 Replies)
Discussion started by: Allasso
2 Replies

10. UNIX for Dummies Questions & Answers

Using Variables to Set Other Variables

I have a script that I'm trying to shorten (below) by removing repetitive code. if ] then commodity_ndm_done=Y fi if ] then customer_ndm_done=Y fi if ] then department_ndm_done=Y fi if ] then division_ndm_done=Y fi (3 Replies)
Discussion started by: superdelic
3 Replies
Login or Register to Ask a Question