Hi Guys,
I am relatively new to scripting at the moment and am struggling to get the following function to work. For some reason it does not recognise the arithmatic symbol when i select option1. Any help would be greatly appreciated.
menu ()
{
echo "=============="
echo "Calculator"
echo "=============="
echo "1. Divide"
echo "2. Multiply and Subtract"
echo "3. Quit"
read choice
case $choice in
1) option1 ;;
2) option2 ;;
3) exit 0 ;;
*) echo "invalid option" ;;
esac
}
option1 ()
{
a=$1
b=$2
c=$(($a/$b))
echo "**********"
echo "****"$c"****"
echo "**********"
}
menu
Ygor
September 14, 2012, 9:37am
2
Syntax is fine for bash...
$ a=10 b=2 c=$(($a/$b))
$ echo $c
5
What shell are you using?
Ygor
September 14, 2012, 9:42am
3
Oh I see the problem. $1 and $2 are local to functions but you are not passing parameters to the function. Perhaps define a and b outside of the functions, e.g...
$ cat sh.1
menu ()
{
echo "=============="
echo "Calculator"
echo "=============="
echo "1. Divide"
echo "2. Multiply and Subtract"
echo "3. Quit"
read choice
case $choice in
1) option1 ;;
2) option2 ;;
3) exit 0 ;;
*) echo "invalid option" ;;
esac
}
option1 ()
{
c=$(($a/$b))
echo "**********"
echo "****"$c"****"
echo "**********"
}
a=$1
b=$2
menu
$ sh sh.1 50 5
==============
Calculator
==============
1. Divide
2. Multiply and Subtract
3. Quit
1
**********
****10****
**********
$
1 Like
I am using a bash shell. This is the error message i keep getting:
ex2: line 23: /: syntax error: operand expected (error token is "/")
any ideas?
---------- Post updated at 02:46 PM ---------- Previous update was at 02:42 PM ----------
Thank you that makes perfect sense.