sodhosdh sodhosdh - 5 months ago 16
Linux Question

Hypotenuse From Shell Script Linux

This is related to my previous question:
Making shapes with linux shell script

I'm trying to write a shell script program with VIM that given sides A and B of the Pythagorean Theorem, it will provide C. Here is my code:

echo -n "Enter A: "
read A
echo -n "Enter B: "
read B
BSquared=$(($B**2))
ASquared=$((A**2))
CSquared=$(($B+$A))
Hypot='echo"scale=2;sqrt($CSquared)"|bc'
echo '
+
|\
| \ C
A | \
| \
+----
B '

echo "A = $A"
echo "B = $B"
echo "C = $Hypot"


The triangle part is just for fun. The only thing wrong with my script is that on the line:

echo "C = $Hypot"


The output is as follows:

C = "scale=2;sqrt($CSquared)"|bc


In other words, the code from the script. Can anyone tell me what I'm doing wrong?

Answer

For command substitution, you must use backticks or $() syntax, not single quotes.

You must also separate the echo command from its argument by adding a space after it.

Replace:

Hypot='echo"scale=2;sqrt($CSquared)"|bc'

with:

Hypot=`echo "scale=2;sqrt($CSquared)"|bc`

or for better readability :

Hypot=$(echo "scale=2;sqrt($CSquared)"|bc)
Comments