Computing.Net > Forums > Unix > bash scripting

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

bash scripting

Reply to Message Icon

Name: mindman04
Date: October 25, 2004 at 22:16:58 Pacific
OS: Windows XP SR2
CPU/Ram: Athlon 900
Comment:

I need to write a script in the Bash shell that asks for users to input number. The script needs to check to see if a non number was entered. Please help.



Sponsored Link
Ads by Google

Response Number 1
Name: thepubba
Date: October 26, 2004 at 09:12:44 Pacific
Reply:

I don't do much bash. However, try this:

echo "Enter a number:"
read NUM
if [ "$(expr $NUM + 0 2> /dev/null)" = "$NUM" ]
then
echo "It's a Number"
else
echo "It's not a Number"
fi


0

Response Number 2
Name: thepubba
Date: October 26, 2004 at 09:35:58 Pacific
Reply:

Here is a better way to do it (it avoids external calls to expr and echo), but it doesn't work in bash (perhaps someone can explain why) but works fine in ksh:

#!/bin/ksh

print "Enter a number:"

read NUM

case $NUM in

+([0-9]) ) print "It's a number"
;;
*) print "It's not a number"
;;
esac


0

Response Number 3
Name: nails
Date: October 27, 2004 at 10:31:37 Pacific
Reply:

Jerry:

In the bash shell, for a script like yours to work, the extended pattern matching has to be turned on. Place the following line in your script, and it works fine with bash:

shopt -s extglob

BTW, my take on this handles a sign:

shopt -s extglob

i=-99
function is_integer {
[[ $1 = ?([+-])+([0-9]) ]]
}
is_integer $i && echo "yes" || echo "no"

Regards,

Nails


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: bash scripting

Verify maildelivery in bash script www.computing.net/answers/unix/verify-maildelivery-in-bash-script/5565.html

Converted BASH script acting strang www.computing.net/answers/unix/converted-bash-script-acting-strang/4385.html

howto add ctrl chars to bash script www.computing.net/answers/unix/howto-add-ctrl-chars-to-bash-script/4994.html