In a csh script I check if a variable (one character) is not a number:
if ($myvar !~ [0-9]) then
How to do the same for say the first three characters of any variable?
I mean if any of them is not a number then...
Hi,
Can anyone tell me how to do the same in bash i.e. I don't want to use the echo|egrep mechanism. I want to know how to do pattern matching in bash.
Cheers,
Pravin.
Here is a ksh way of doing this. Adapt as needed for bash:
#! /bin/ksh
# Returns True if its argument is a valid number.
# The first pattern requires a digit before the decimal
# point, and the second after the decimal point.
function isnum
{
case $1 in
?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
*) print "\nNot a number.\n" ;;
esac
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.