1833777 Members
2433 Online
110063 Solutions
New Discussion

Validations for read?

 
SOLVED
Go to solution
Chua Wen Ching
Regular Advisor

Validations for read?

I need some help here.
script1.sh
-----------
echo "Enter a number: \c"
read num1

i want to only num1 accepts number (ex: 1, 2, 4). But if i enter (enter key, backspace key, spacebar key). The output will be different. Any sample validations to avoid this. Thank you.
wenching
17 REPLIES 17
curt larson_1
Honored Contributor

Re: Validations for read?

if the shell your using supports character classes, you can do
if [[ $num1 == [:alnum:] ]] ;then

but it probably doesn't, so you'll need to do something like this

if [[ $num1 == [a-zA-Z0-9] ]] ;then
curt larson_1
Honored Contributor

Re: Validations for read?

just numbers would be
[:digit:] or [0-9]
hex numbers
[:xdigit:] or [0-9a-fA-F]
just certain digits
[159] etc
Chua Wen Ching
Regular Advisor

Re: Validations for read?

I had some confusion with the solution you reply, can you show me a short and simple sample script. Thank you.
wenching
harry d brown jr
Honored Contributor
Solution

Re: Validations for read?

#!/usr/bin/ksh
#
found=false
while [ "${found}" = "false" ] ;do
echo "enter num:\c"
read num1
if [[ "${num1}" = [124] ]];then
found=true
fi
done


live free or die
harry
Live Free or Die
Chua Wen Ching
Regular Advisor

Re: Validations for read?

Hmm...i do not know much about korn shell. Is there any sample script on bourne shell scripting? Thank you.
wenching
Thomas M. Williams_1
Frequent Advisor

Re: Validations for read?

simply type set a variable.

typeset -i num1

It error out on anything other than a number.
I Think the Clock is Slow ...
Thomas M. Williams_1
Frequent Advisor

Re: Validations for read?

I am sure you would do this anyway but check the man page on "typeset". It is an extremely useful command.
I Think the Clock is Slow ...
Thomas M. Williams_1
Frequent Advisor

Re: Validations for read?

I just noticed you "korn" shell reply. The "korn" shell is the best one to use in most cases. It has a lot of commands. The "typeset" is one of them so you would need to insert the "#!/bin/ksh" line at the top.
I Think the Clock is Slow ...
Chua Wen Ching
Regular Advisor

Re: Validations for read?

When i man typeset, it displays no manual entry for typeset. Is it part of HP-UX 10.20. Can you show me an sample bourne shell script that utilizes typeset? Thank you.
wenching
John Poff
Honored Contributor

Re: Validations for read?

Hi,

The 'typeset' command is part of the shell. You can find out about it in the man pages for 'ksh' and 'sh-posix', just search for typeset.

JP
Chua Wen Ching
Regular Advisor

Re: Validations for read?

Okay i will make research on the typeset. Do you mind show me a sample of ksh/posix using the typeset feature? Thank you.
wenching
John Poff
Honored Contributor

Re: Validations for read?

Hi,

Here is a quick sample of using typeset that I put together. This sample fills the variable with zeros:

>cat typeset.test.ksh
#!/bin/ksh

# typeset.test.ksh


typeset -Z8 zeros=0

echo "zeros is $zeros"

zeros=123

echo "zeros is $zeros"

>./typeset.test.ksh
zeros is 00000000
zeros is 00000123



JP


John Meissner
Esteemed Contributor

Re: Validations for read?

echo " enter a number "
until [ "$answer" = [:digit:] ] ; do
echo " Answer with a number !\c"
read answer
All paths lead to destiny
Chua Wen Ching
Regular Advisor

Re: Validations for read?

Thanks for the typeset script. I think it really can help in validations. Thank you.
wenching
John Meissner
Esteemed Contributor

Re: Validations for read?

at the end of the above script you need to put one more line at the end:

done
All paths lead to destiny
Chua Wen Ching
Regular Advisor

Re: Validations for read?

Hmm....i had some confusion here when running the sample script.
The output:
-----------
enter a number
Answer with a number!1
Answer with a number!b
Answer with a number!3
endless loop

It seems like cannot work to me. Anyway thanks for your help. Do i need to replace :digit: to :4: or :1:???
wenching
John Meissner
Esteemed Contributor

Re: Validations for read?

try replacing the [:digit:] with:

[0-9]

All paths lead to destiny