Check for numeric inputs

Hi All,

How do i modify the below script such that if the input is numeric, it will give the numeric digit, else it will ouput "0"

echo "xxx"  | awk '/^[0-9]+$/' 

Here is the script that will do for you..

#!/bin/ksh
echo "Digits as input"
read number

test=`echo "$number" | egrep "[1]+$"`

if [ "$test" ]
then
echo "Entered number is a digit"
else
echo "0"
fi

Instead of egrep you can use awk,grep or any command for searching patterns.

Thanks
Namish


  1. 0-9 ↩ī¸Ž

typeset -i x

will force $x to be a number

hi namishtiwari,

I'm using csh shell. Is there a csh version of your code ?

not sure this will work in csh

..
case $x in
    *[!0-9]*|"") echo "0";;
    *) echo $x;;
esac

Hi all,

I think i have some idea of converting your ksh to csh.
See below!

Thanks all for the help!!!

#!/bin/csh

echo -n "Pls input: "
set number = ($<)

test=`echo "$number" | egrep "^[0-9]+$"`

if [ "$test" ] then
echo "Entered number is a digit"
else
echo "0"
endif

c shell considered harmful