Tuesday, March 21, 2006

script: determine variable type

i found this useful script on how to determine the variable type.
$ cat mad.ksh
#! /bin/ksh

[[ -z "$1" ]] && echo "I cant work without an input" && exit 1

INPUT="$@"

[[ "$INPUT" == ?(+|-)+([0-9]) ]] && echo "$INPUT is numeric" && exit 0

[[ "$INPUT" == +([a-zA-Z]) ]] && echo "$INPUT is character" && exit 0

[[ "$INPUT" == *([0-9]|[a-zA-Z])* ]] && echo "$INPUT is alpha-numeric" && exit 0

$ ./mad.ksh 123
123 is numeric
$ ./mad.ksh abc
abc is character
$ ./mad.ksh abc123
abc123 is alpha-numeric

i hope they don't mind i paste it here.
source: http://www.unix.com/showthread.php?t=21630&p=83968

No comments: