- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Unix Shell Script to check the character input by ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 01:57 AM
07-04-2002 01:57 AM
Unix Shell Script to check the character input by user !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 02:03 AM
07-04-2002 02:03 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 02:04 AM
07-04-2002 02:04 AM
Re: Unix Shell Script to check the character input by user !!
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 02:04 AM
07-04-2002 02:04 AM
Re: Unix Shell Script to check the character input by user !!
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 02:04 AM
07-04-2002 02:04 AM
Re: Unix Shell Script to check the character input by user !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 02:10 AM
07-04-2002 02:10 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 02:50 AM
07-04-2002 02:50 AM
Re: Unix Shell Script to check the character input by user !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 04:11 AM
07-04-2002 04:11 AM
Re: Unix Shell Script to check the character input by user !!
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 07:57 AM
07-04-2002 07:57 AM
Re: Unix Shell Script to check the character input by user !!
Thanks for all your suggestions !! I just solved the problem.
Cheers,
Chris,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2002 08:03 AM
07-04-2002 08:03 AM
Re: Unix Shell Script to check the character input by user !!
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.