2

I am trying to build a very basic 4 function arithmetic script in UNIX and it doesn't like my arithmetic statement. I was trying to use 'bash arithmetic' syntax

from this source

http://faculty.salina.k-state.edu/tim/unix_sg/bash/math.html

also as a side note when you reference a variable in UNIX when do you need to use the "$" symbol, and when do you not need to?

#!/bin/bash str1="add" str2="sub" str3="div" str4="mult" ((int3=0)) ((int2=0)) ((int1=0)) clear read -p "please enter the first integer" $int1 clear read -p "Please enter mathmatical operator'" input clear read -p "Please enter the second integer" $int2 if [ "$input" = "$str1" ]; then ((int3 = int1+int2)) echo "$int3" else echo "sadly, it does not work" fi; 
1

5 Answers 5

1

I think this is what you want:

#!/bin/bash str1="add" str2="sub" str3="div" str4="mult" ((int3=0)) # maybe you can explain me in comments why you need a arithmetic expression here to perform an simple assignment? ((int2=0)) ((int1=0)) echo -n "please enter the first integer > " read int1 echo -n "Please enter mathmatical operator > " read input echo -n "Please enter the second integer > " read int2 if [ $input = $str1 ] then ((int3=int1 + int2)) echo "$int3" else echo "sadly, it does not work" fi exec $SHELL 

You should definitly checkout man bash. It is documented there in which command you need to specify $ or not to reference a variable. But aside from that:

var=123 # variable assignment. no spaces in between echo $var # fetches/references the value of var. Or in other words $var gets substituted by it's value. 
Sign up to request clarification or add additional context in comments.

Comments

0

Use bc command

something like this

echo "9/3+12" | bc

Comments

0

You use $ when you want the value of the variable. read, though, expects the name of a variable:

read -p "..." int1 

(Technically, you could do something like

name=int1 read -p "..." "$name" 

to set the value of int1, because the shell expands name to the string int1, which read then uses as the name.)

2 Comments

Good feedback, I have removed teh bash symbol from my variables, however I still get an error in execution
@JonathanDeal: which error? What do you mean by the "bash symbol". The $ should be considered to be a unary operator to give the value of a variable.
0

Here's a quick once over:

op=( add sub div mult ) int1=0 int2=0 ans=0 clear read -p "please enter the first integer > " int1 clear IFS='/' read -p "Please enter mathmatical operator (${op[*]})> " input unset IFS clear read -p "Please enter the second integer > " int2 case "$input" in add) (( ans = int1 + int2 ));; sub) (( ans = int1 - int2 ));; div) (( ans = int1 / int2 ));; # returns truncated value and might want to check int2 != 0 mult) (( ans = int1 * int2 ));; *) echo "Invalid choice" exit 1;; esac echo "Answer is: $ans" 

You will also want to check that the user enters numbers to :)

Comments

0

Another one

declare -A oper oper=([add]='+' [sub]='-' [div]='/' [mul]='*') read -r -p 'Num1? > ' num1 read -r -p "oper? (${!oper[*]}) > " op read -r -p 'Num2? > ' num2 [[ -n "${oper[$op]}" ]] || { echo "Err: unknown operation $op" >&2 ; exit 1; } res=$(bc -l <<< "$num1 ${oper[$op]} $num2") echo "$num1 ${oper[$op]} $num2 = $res" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.