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 (10 answers total)
 
= is assignment, == is comparison

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, 2009


And as for numerical comparison:

http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions

arg1 OP arg2
OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
posted by so_ at 1:38 PM on April 30, 2009 [1 favorite]


Is this a homework assignment? Such questions are frowned upon.
posted by procrastination at 1:47 PM on April 30, 2009


#!/bin/sh
python -c "print input('enter expression: ')"
posted by cj_ at 1:57 PM on April 30, 2009 [1 favorite]


There is a difference between "Please provide a shell script that fulfills the following requirements" and "I'm having trouble with my program, here's what I have so far." Even if this is a homework question, it's not crossing the line into "do my homework for me."
posted by The Pusher Robot at 2:00 PM on April 30, 2009


I don't think the question has a really easy answer. If you want to keep the full generality of bash's input methods (choice of base etc), then I'd suggest

themel@sophokles:~$ export NUM=1293
themel@sophokles:~$ export NONUM=23a9
themel@sophokles:~$ eval 'true $(($NUM))' || echo bad
themel@sophokles:~$ eval 'true $(($NONUM))' || echo bad
bash: 23a9: value too great for base (error token is "23a9")
bad

If you can explain that, you've earner your homework score, I guess :)
posted by themel at 2:14 PM on April 30, 2009


The Pusher Robot: Even if this is a homework question, it's not crossing the line into "do my homework for me."

No, but it is suspicious a member of 4 days with only 2 AskMe comments asks a very home-worky question...

OP: More info required; this isn't the psychic advice hotline. What do you mean by "I cannot get any scripts to work correctly"? That could be anything from "they cause my PC to explode in flames each time I run them", through "it crashes" or "nothing happens", right down to "I just don't like the answers it gives me".
posted by Pinback at 2:23 PM on April 30, 2009


Response by poster: I got it guys. Yes it is a homework assignment, more or less, but I wasnt intending on getting everyone to do it for me, the issue I was having was more of "why cant I get this script to work" than "what script should I use". I had the code that I knew should work, but it wouldn't and thats where my issue was. As for the code I was using it was simply:

if [ $a -eq $a 2> /dev/null ]; then

However I was getting expr: non-numeric argument when a number with a decimal was entered. Regardless, as is often the case with these things I pretty much solved it right after posting the question, turns out it was because I was using #!/bin/sh when I should have been using bash, for whatever reason.

Thanks for all your help anyone guys.

Oh and Pinback, I joined on March 27th its April now
posted by InsaneRhino at 2:54 PM on April 30, 2009


You can use awk, if all else fails.

example:
echo 123 | awk '{ exit ! /^[0-9]+$/ }' && echo "True" || echo "False"
posted by devilsbrigade at 2:57 PM on April 30, 2009


bc gives you basic calculations from the shell:

$ echo "5+5" | bc
10
posted by chrisamiller at 3:08 PM on April 30, 2009


« Older Satin latex paint on my bookshelves?   |   Any moving tips for a cross-country move? Newer »
This thread is closed to new comments.