Hi
how to check input is "empty" and "numeric" in ksh?
e.g:
./myscript.ksh k
output show: invalid number input
./myscript.ksh
output show: no input
./myscript.ksh 10
output show: input is numeric
Hi
how to check input is "empty" and "numeric" in ksh?
e.g:
./myscript.ksh k
output show: invalid number input
./myscript.ksh
output show: no input
./myscript.ksh 10
output show: input is numeric
checking for empty is easy
if test "$VAR" = ""
then
echo empty
fi
checking for numeric, I would use case,
case "$VAR" in
[0-9]* )
echo at least it starts with a numeric...
;;
* )
;;
esac
thanks. is working fine.
If you want to be sure the entire argument is numeric (rather than just the first character), use grep:
if echo $VAR | grep [^0-9] > /dev/null
then
echo "Some non-numeric characters detected"
else
echo "All numeric"
fi
The case statement itself can manage full numeric check (for both integers and floating point numbers):
HTH
this is the best +([0-9])*(.)*([0-9]) )
Tests for an empty value can be done using the "-z" option of test:
if [ -z "$var" ] ; then
....
bakunin