Unix Bourne script

I would like to write a script that uses the arithmetic operations to calculate whatever numbers we type in.  For example, when I type: 5 + 6, it will add up to 11.  Also, I would like to check the errors that whenever we type the characters, it will reply an error message. Thanks.
nhvo73Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
bedotConnect With a Mentor Commented:
read a b c
export a b c
case $a in
        [!0-9]) echo $a "is not a digit\n"; exit ;;
esac
case $c in
        [!0-9]) echo $b "is not a digit\n"; exit ;;
esac

case $b in
        [+-/%]) echo "$a $b $c" = `expr $a $b $c` ;;
        \*) echo "$a $b $c" = `expr $a \* $c` ;;
        *) echo $b "is not an operator\n";;
esac



but bourne shell is not designed to do easily that, because variables are ASCII coded (not numeric)

shell posix can use numeric variable (typeset -i )
0
 
ozoCommented:
read a b
expr $a + $b
0
 
ozoCommented:
expr will do it's own error checking.
(unless you wanted to customize the error message)
0
 
chris_calabreseCommented:
#!/usr/bin/sh
exec /usr/bin/bc
0
 
nhvo73Author Commented:
Very short script.  I am impressed.  Thanks a lot!
0
All Courses

From novice to tech pro — start learning today.