sodhosdh sodhosdh - 1 year ago 82
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
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 Source

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.




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

or for better readability :

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