How to test if a variable in a Bourne shell script is an integer?
April 30, 2009 1:26 PM
Subscribe
I'm being tasked with creating a simple integer calculator, which I have effectively done, up to the point of it being a calculator, however, even after hours of searching I cannot get any scripts to work correctly.
Here is my code so far:
-----------------------------------------------
#!/bin/sh
echo "Please enter your first number"
read a
echo "What operation would you like to perform? (+,-,/,x)"
read orp
echo "Please enter your second number"
read b
echo $op
if [ $opr = "+" ]
then
op=`expr $a + $b`
echo "$op"
elif [ $opr = "-" ]
then
op=`expr $a - $b`
echo "$op"
elif [ $opr = "/" ]
then
op=`expr $a / $b`
echo "$op"
elif [ $opr = "X" ]
then
op=`expr $a \* $b`
echo "$op"
fi
-----------------------------------------------
Thanks for any assistance!
posted by InsaneRhino to computers & internet (11 comments total)
Also in string comparison, you would do:
if ["$opr" == "+"]
Also, you have read orp and then comparing with $opr
so orp $orp or opr $opr
posted by so_ at 1:34 PM on April 30