cclmotors
asked on
C Shell Read Numeric value only
Hello,
I want to write a script on Digital UNIX C Shell to read user input but only numeric value. I have created a script to do that but it does not maintain 2 special characters: * and ?.
Do you know how to write it?
-------------------------- ----------
#! /bin/csh -f
#
# On C Shell to read a value and ensure it belongs to numeric value.
#
echo -n "Enter a number: "
set ans = $<
if (($ans =~ [1-9]) || ($ans =~ [1-9]*[0-9]) && ($ans =~ [0-9])) then
echo "Right " $ans
else
echo "wrong"
endif
-------------------------- ----------
Regards,
Paulus
I want to write a script on Digital UNIX C Shell to read user input but only numeric value. I have created a script to do that but it does not maintain 2 special characters: * and ?.
Do you know how to write it?
--------------------------
#! /bin/csh -f
#
# On C Shell to read a value and ensure it belongs to numeric value.
#
echo -n "Enter a number: "
set ans = $<
if (($ans =~ [1-9]) || ($ans =~ [1-9]*[0-9]) && ($ans =~ [0-9])) then
echo "Right " $ans
else
echo "wrong"
endif
--------------------------
Regards,
Paulus
use "set noglob" to disable metacharacters and after that metacharacters represent themselves.
be careful though if you have to use them
#! /bin/csh -f
#
# On C Shell to read a value and ensure it belongs to numeric value.
#
set noglob
echo -n "Enter a number: "
set ans = $<
if (($ans =~ [1-9]) || ($ans =~ [1-9]*[0-9]) && ($ans =~ [0-9])) then
echo "Right " $ans
else
echo "wrong"
endif
be careful though if you have to use them
#! /bin/csh -f
#
# On C Shell to read a value and ensure it belongs to numeric value.
#
set noglob
echo -n "Enter a number: "
set ans = $<
if (($ans =~ [1-9]) || ($ans =~ [1-9]*[0-9]) && ($ans =~ [0-9])) then
echo "Right " $ans
else
echo "wrong"
endif
and maybe you can change your if condition to
if (($ans =~ [0-9]) || ($ans =~ [1-9][0-9]*) ) then
if (($ans =~ [0-9]) || ($ans =~ [1-9][0-9]*) ) then
ASKER
Thank you.
But it cannot maintain an input e.g. 12q
When the 3rd char is not numeric value, it always accept.
But it cannot maintain an input e.g. 12q
When the 3rd char is not numeric value, it always accept.
the * is taken to be any charcater any number of times ..
one crude way would be to use something like
if ( ($ans =~ [0-9]) || ($ans =~ [1-9][0-9]) || ($ans =~ [1-9][0-9][0-9] ) ) then
and so on depending on the acceptable length of the input
hardly a good solution ..
i am still looking into it though ..
one crude way would be to use something like
if ( ($ans =~ [0-9]) || ($ans =~ [1-9][0-9]) || ($ans =~ [1-9][0-9][0-9] ) ) then
and so on depending on the acceptable length of the input
hardly a good solution ..
i am still looking into it though ..
Why use csh? Csh Programming Considered Harmful, see:
http://www-dbs.inf.ethz.ch/~fessler/csh-whynot.html
It is a lot easier to do it with sh/ksh/bash, eg:
expr $var + 0 >/dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "$var is not a number"
else
echo "It is a number"
fi
http://www-dbs.inf.ethz.ch/~fessler/csh-whynot.html
It is a lot easier to do it with sh/ksh/bash, eg:
expr $var + 0 >/dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "$var is not a number"
else
echo "It is a number"
fi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
following will give you an demo
#! /bin/csh -f
echo -n "Enter a number: "
set ans = $<
echo $ans
if you enter a '*'
the output echoed is the list of the files in the directories.
-----
the remedy ? ( i dont know yet :)