|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Hi all,
Do you have any pointers how to validate numbers (not to contain alphabets and special characters) and date(MM/DD/YYYY) format. I used following regular expression to validate integer, which is not working in the default shell: nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')" if [ ! -z $nodigits ] ; then echo "Invalid number format! Only digits, no commas, spaces, etc." >&2 return 1 fi Alok |
|
#2
|
|||
|
|||
|
why not simply use (old and) powerfull tools ?
case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac do the rest exit 0 NOTA: this works w/ ALL shells (only csh needs switch instead of case, but csh never was a good scripting shell, so don't use csh for scripts) |
|
#3
|
|||
|
|||
|
regular expressions don't do much for dates. You can either write a simple C module
that calls getdate() which does validate dates, or use another module that already does it. For example, touch validates dates. Here is a primitive example that validates a full date and time in YYYYMMDDhhmmss format. Code:
#!/bin/ksh # validate date/time MMDD=$(expr substr "$*" 1 4) YYYY=$(expr substr "$*" 5 4) hh=$(expr substr "$*" 10 2) mm=$(expr substr "$*" 13 2) ss=$(expr substr "$*" 16 2) if touch -c -t $YYYY$MMDD$hh$mm.$ss dummyfile then echo valid else echo invalid fi |
|
#4
|
|||
|
|||
|
Try this to check the date string:
: [ $# -eq 1 ] || { echo "$0: usage: $0 MM/DD/YYYY" >&2 exit 2 } case $1 in [0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]) : ;; *) echo invalid exit 1 ;; esac IFS=/ # redefine internal field separators set $1 # separate the fields into $1, $2, $3 # touch does not check for 31st of February. # If necessary, write a better check if touch -ct $3$2$10000 x then echo valid else echo invalid exit 1 fi Regads |
|
#5
|
|||
|
|||
|
The test for integer from guggach
" case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac do the rest exit 0 " is not OK, because it evaluates "1XXX2" as valid. In Korn shell you can use: case $1 in +([0-9])) echo valid ;; *) echo invalid ;; esac In Bourne shell you can count all characters and all digits. If the counts are equal, the input is an integer: C=`expr $1 : ".*"` D=`expr $1 : "[0-9][0-9]*"` [ $C -eq $D ] && { echo valid } Regards |
|
#6
|
|||
|
|||
|
A slick one liner
None of those methods does much for validating that the date is a valid date. Feb 32 is validated as Mar 3. I took it and used cal, sed , and grep to validate a recovery date in CCYY/MM/DD:
#Validate that the cycle date is a real date MM=$(expr substr "$recovery" 6 2) DD=$(expr substr "$recovery" 9 4) YYYY=$(expr substr "$recovery" 1 4) D=`echo $DD|sed s/^0//` cal $MM $YYYY|tail +3|grep $D>/dev/null if [ ${?} -ne 0 ]; then echo "ERROR: Invalid recovery date. Process end." exit 1 fi The heart of this is a one-liner: cal $MM $YYYY|tail +3|grep $D>/dev/null Everything else is window dressing. |
|
#7
|
|||
|
|||
|
so so
Quote:
prouve it |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Number & Date Validation Query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|