Hi,
I wonder how I can know if the input parameters to the script are numbers or text
Thanks
Hi,
I wonder how I can know if the input parameters to the script are numbers or text
Thanks
use tr, run the input parameter though a filter, and see if the output is equal to the input.
---------- Post updated at 01:47 PM ---------- Previous update was at 01:41 PM ----------
use tr, run the input parameter though a filter, and see if the output is equal to the input.
out=`echo $input|tr blah blah`
if [ $input != $out ]
What I do is the following:
if [ $1 is a number ] then...
else...
fi
This is a very good question! I have a ksh function dealing with exactly this im my library.
Lets start with an integer: an integer is a sequence of digits 0-9 preceeded optionally with a plus or a minus sign:
# i assume the string in question was passed as argument $1 here
if [ -n "$(print - $1 | sed 's/[+-]*[0-9][0-9]*//')" ] ; then
print - "string is not an integer"
else
print - "string is an integer"
fi
The sed statement will delete out a possible integer and if the remaining string still contains characters ("-n") it can't be an integer. This mechanism is the core of my f_CheckInteger() function.
General numerics are a bit more complicated. I didn't bother to implement localized decimal separators (americans write "123.4", germans write "123,4", etc.) or something such, so i ended with the quite simple definition:
A numerical value is a string consisting of an optional plus/minus sign, followed by one or more digits 0-9, optionally followed by a full stop "." followed by one or more digits 0-9.
These are valid numbers according to this definition: 0.123 123 123.0, -123.4
These are not valid numbers: 0..123 0.123.456 123. +-12
# i assume the string in question was passed as argument $1 here
if [ -n "$(print - $1 | sed 's/[+-]*[0-9][0-9]*\(\.[0-9][0-9]*\)*//')" ] ; then
print - "string is not a numeric"
else
print - "string is a numeric"
fi
I hope this helps.
bakunin
Shell only
#!/bin/sh
while [ $# -gt 0 ]; do
case "$1" in
*[!1-9]*) echo "Text: $1";;
*) echo "Number: $1"
esac
shift
done
Hi Gengis-Kahn
I write a script I hope that you want to this like..
[root@rhnserver ~]# cat script.sh
if [[ "$1" = *[[:alpha:]]* && "$1" = *[[:digit:]]* ]] ; then
echo "Your parameter is alpha numeric"
else
if [[ "$1" = *[[:digit:]]* ]] ; then
echo "Your parameter is numeric"
else
if [[ "$1" = *[[:alpha:]]* ]] ; then
echo "Your parameter is a character"
else
echo "Your paramter is different type(except number or character)"
fi
fi
fi
A few examples :
[root@rhnserver ~]# ./script.sh 12121asa
Your parameter is alpha numeric
[root@rhnserver ~]# ./script.sh 12121
Your parameter is numeric
[root@rhnserver ~]# ./script.sh sasasasqsq
Your parameter is a character
Thank you very much for the help! Now I have to study the code
With tr, delete all acceptable chars, if result is more than "", then not acceptable.
Or replace acceptable chars with nothing (=remove), rest of chars are not acceptable.
#!/bin/ksh or bash or
# example numbers, remove numbers
otherchars=$(echo "$input" | tr -d "[0-9]")
[ "$otherchars" != "" ] && echo not ok $input && exit 1 # not num
# or using builtin properties - more speed less cpu
# replace numbers with nothing, rest of chars are something else
otherchars="${input//[0-9]/}"
[ "$otherchars" != "" ] && exit 1 # not num
There have been several suggestions following this line, but they are not always giving the correct result. If a numerical value is not an integer the "tr -d'[0-9]'" will say its not numerical if a comma is included: "123.4" for instance.
If you would include the comma into the expression, like this: "tr -d'[0-9.]'", then this: "123.456.789" would be considered a numeric value, but it is not.
A numerical value is - see my definition above - a bit mor complex than just searching for digits would cover.
bakunin
Question was numbers, not numeric. Numeric is much more then some sign+dot. You need take care of locales, +/- sign, different kind of formats, delimeters, ...
But here is something more, not enough for full numeric testing:
#!/bin/ksh or bash or ...
check()
{
case "$1" in
?(+|-|)+([0-9])) echo "int" ; return 1 ;;
?(+|-|)+([0-9])(,|.)+([0-9])) echo "not int, but it's numeric" ; return 2 ;;
*) echo "something else" ; return 0 ;;
esac
}
for i in 12 +12 -12 12+ 1.2 +1.2 -12.22 -12.2.22 2.3.4 A1 1A 2A.45 23,45
do
echo -n "$i :"
check $i
done
The question was actually "numbers". I interpreted it to mean "numerical values", you seem to have interpreted it differently. Compare now what you have written to the solution i gave one day ago (see my first post in this thread) and the limitations i gave for my code.
bakunin