Operating System - HP-UX
1833569 Members
4509 Online
110061 Solutions
New Discussion

Unix Shell Script to check the character input by user !!

 
Chris Fung
Frequent Advisor

Unix Shell Script to check the character input by user !!

Hi all,

I am writing a script to read one character input from user. I actually intent to check the numeric number input by user i.e. 1 or 2 or 3....etc. However, when someone input strings or alpha character, my program will fail....!! Within my script I using "if [[ a -le b ]] to check the condition !! How to prevent such kind of error ? Is there any Unix shell script syntax can help me to check whether the character is a numeric first ?

Please help,

Chris
9 REPLIES 9
Olav Baadsvik
Esteemed Contributor

Re: Unix Shell Script to check the character input by user !!


Hello,

Here is a way to do it:#!/usr/bin/ksh
echo "Enter something :"
read my_value

# the next statement will return a value zero providing its numeric

let new_value=$my_value

if [[ $? -eq 0 ]] ; then
echo "ok its an integer"
else
echo "failed the test"
fi

Regards
Olav

Justo Exposito
Esteemed Contributor

Re: Unix Shell Script to check the character input by user !!

Hi Chris,

First I'll check if is null by:
-z s1 True if the length of string s1 is zero.
-n s1 True if the length of the string s1 is non-zero.
s1 True if s1 is not the null string.

Then check if it is alpha and then check if numeric.

Regards,

Justo.
Help is a Beatiful word
Steven Sim Kok Leong
Honored Contributor

Re: Unix Shell Script to check the character input by user !!

Hi,

#!/sbin/sh

mychar=3

if echo $mychar | grep [0-9]
then
echo $mychar is a digit
else
echo $mychar is NOT a digit
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Wodisch
Honored Contributor

Re: Unix Shell Script to check the character input by user !!

Hi Chris,

that's two questions, actually...

1: How to input exactly one character:

mode=$(stty -g)
stty raw
key=$(dd bs=1 2>/dev/null)
stty $mode

2: how to check the value you've got:

case "$key" in
[0-9]) number=$key ;;
*) echo "wrong input..."; exit 1;;
esac

HTH,
Wodisch
Olav Baadsvik
Esteemed Contributor

Re: Unix Shell Script to check the character input by user !!


Hi again,

Cleand up the sample to avoid the
error-message:
#!/usr/bin/ksh
echo "Enter something :"
read my_value

# the next statement will return a value zero providing its numeric

let new_value=$my_value 2> /dev/null

if [ $? -eq 0 ]
then
echo "ok its an integer"
else
echo "failed the test"
fi

Olav

Nick Wickens
Respected Contributor

Re: Unix Shell Script to check the character input by user !!

I find its always better to use the case statement for such queries ie

echo "Please enter a number between 1 & 9 -> \c"
read ANSWER
case $ANSWER in
1) echo "you entered 1";;
2) echo "you entered 2";;
3) echo "you entered 3";;
4) echo "you entered 4";;
5) echo "you entered 5";;
6) echo "you entered 6";;
7) echo "you entered 7";;
8) echo "you entered 8";;
9) echo "you entered 9";;
*) echo "You are the weakest link - goodbye !";exit;;
esac
Hats ? We don't need no stinkin' hats !!
Peter Kloetgen
Esteemed Contributor

Re: Unix Shell Script to check the character input by user !!

Hi Chris,

you can easily check this with a trick:

echo "please enter an integer: \c"
read number

if (( number + 1 )) 1>/dev/null 2>/dev/null
then
commands_of_your_script
exit 0
else
echo "Please give only integer values!"
exit 1
fi

The if- construction goes to "then" if the return code of the (( ... )) command is zero, which is allways correct, if the user input is an integer, then your script commands will be run. If anything else is given, the command fails and the script will be terminated with an error message. Standard output and standard error output are redirected because we don't need them.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Chris Fung
Frequent Advisor

Re: Unix Shell Script to check the character input by user !!

Hi all,

Thanks for all your suggestions !! I just solved the problem.

Cheers,

Chris,
Justo Exposito
Esteemed Contributor

Re: Unix Shell Script to check the character input by user !!

Hi Chris,

You are welcome, but you can assign points to the answers in order to define wich is the best answer for you and for all the people that can read this issue in the future.

Then please, assign points it's good for the forum and for all the forumers.

Regards,

Justo.
Help is a Beatiful word