I'm quite new to bash scripting and usually avoid it all costs but I need to write a bash script to execute some simple things on a remote cluster. I'm having problems with a for loop that does the following:
for i in {1..20}
do
for j in {1..20}
do
echo (i*i + j*j ) **.5 <--- Pseudo code!
done
done
Can you help me with this simple math? I've thrown $
's everywhere and can't write it properly. If you could help me understand how variables are named/assigned in bash for loops and the limitations of bash math interpretation (how do you do the square root?) I'd be very grateful. Thanks!
Typically you would use $((1*3)), but your case won't work as bash doesn't support floating point numbers. You'll have to use an external tool like awk, bc or dc: http://mywiki.wooledge.org/BashFAQ/022
with zsh, this will work
Arithmetic expansion needs
$((...))
notation, so something like:However, bash only uses integers so you may need to use an external tool such as dc.
E.g.
Bash doesn't offer mathematical functions. However, you almost certainly have the korn shell installed. This should work:
The beginning of the output is
does your remote cluster only have bash? if not, try and see if you have awk