0

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?

Community
  • 1
  • 1
sodhosdh
  • 25
  • 4
  • Possible duplicate of [How to set a variable equal to the output from a command in Bash?](http://stackoverflow.com/questions/4651437/how-to-set-a-variable-equal-to-the-output-from-a-command-in-bash) – tripleee Jun 11 '16 at 07:57
  • Why did you included vim here? Are you okay with solution in vimscript? – SibiCoder Jun 11 '16 at 09:50

2 Answers2

1

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)
SLePort
  • 15,211
  • 3
  • 34
  • 44
0

If the shell is bash, there's no need for echo:

Hypot=$(bc <<< "scale=2;sqrt($CSquared)")
agc
  • 7,973
  • 2
  • 29
  • 50