bash 中 for 循环中的简单数学语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1850235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:45:50  来源:igfitidea点击:

Simple math statements in bash in a for loop

bashmathsyntaxfor-loop

提问by physicsmichael

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:

我对 bash 脚本很陌生,通常会避免所有成本,但我需要编写一个 bash 脚本来在远程集群上执行一些简单的事情。我在执行以下操作的 for 循环中遇到问题:

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!

你能帮我解决这个简单的数学问题吗?我把$'s 到处扔了,但写不出来。如果您能帮助我了解如何在 bash for 循环中命名/分配变量以及 bash 数学解释的局限性(您如何计算平方根?),我将不胜感激。谢谢!

回答by CB Bailey

Arithmetic expansion needs $((...))notation, so something like:

算术扩展需要$((...))符号,所以像:

echo $((i*i + j*j))

However, bash only uses integers so you may need to use an external tool such as dc.

但是,bash 仅使用整数,因此您可能需要使用外部工具,例如 dc。

E.g.

例如

dc -e "18k $i $i * $j $j * + v p"

回答by dustmachine

Here's a decent solution:

这是一个不错的解决方案:

for i in {1..20}
do
   for j in {1..20}
   do
       echo "scale = 3; sqrt($i*$i + $j*$j)" | bc
   done
done

Output will be:

输出将是:

1.414
2.236
3.162
2.236
[...etc...]

回答by ephemient

Shell math can be done in several ways.

壳牌数学可以通过多种方式完成。

echo $(( i*i + j*j ))
echo $[ i*i + j*j ]
expr "$i" '*' "$i" '+' "$j" '*' "$j"

However, this can only handle integer arithmetic. Instead, you can use bc:

但是,这只能处理整数算术。相反,您可以使用bc

echo "scale = 5; sqrt( $i*$i + $j*$j)" | bc

Change scaleto the number of decimal places desired.

更改scale为所需的小数位数。

回答by DigitalRoss

#!/bin/bash
for i in {1..20}; do
    for j in {1..20}; do
        echo 5k$i $i\* $j $j\*+vp | dc
    done
done

回答by rerun

Use double paren to evaluate a variable.

使用双括号计算变量。

variableA=$((variableB*variableC))

变量A=$((变量B*变量C))

Only for ints though.

仅适用于整数。

回答by ghostdog74

does your remote cluster only have bash? if not, try and see if you have awk

你的远程集群只有 bash 吗?如果没有,请尝试看看您是否有 awk

awk 'BEGIN{
  for(i=1;i<=20;i++){
    for(j=1;j<=20;j++){
       print ( i*i + j*j ) ** 0.5
    }
  }
}'

回答by Nahuel Fouilleul

with zsh, this will work

使用 zsh,这将起作用

for i in {1..20};do
  for j in {1..20};do
     echo $((($i*$i + $j*$j)**.5))
  done
done

回答by Ben

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

通常您会使用 $((1*3)),但您的情况将不起作用,因为 bash 不支持浮点数。您必须使用外部工具,如 awk、bc 或 dc:http: //mywiki.wooledge.org/BashFAQ/022

回答by bta

The code

编码

echo $[(($i * $i) + ($j * $j)) ** $X]

will work if $Xis an integer. You're trying to take the square root, and I'm not sure if bash's built-in arithmetic will do that. You'll probably be better off using a more powerful calculator tool (like bc, et al.) for this.

如果$X是整数,将起作用。您正在尝试求平方根,我不确定 bash 的内置算术是否会这样做。bc为此,您最好使用功能更强大的计算器工具(如、等)。

回答by wallyk

Bash doesn't offer mathematical functions. However, you almost certainly have the korn shell installed. This should work:

Bash 不提供数学函数。但是,您几乎可以肯定已经安装了 korn shell。这应该有效:

#!/bin/ksh

for i in {1..20}
do
    for j in {1..20}
    do
        x=$((sqrt(i*i + j*j)))
        echo "sqrt($i^2 + $j^2) = $x"
    done
done

The beginning of the output is

输出的开头是

sqrt(1^2 + 1^2) = 1.41421356237309505
sqrt(1^2 + 2^2) = 2.2360679774997897
sqrt(1^2 + 3^2) = 3.16227766016837933
sqrt(1^2 + 4^2) = 4.12310562561766055
sqrt(1^2 + 5^2) = 5.09901951359278483
sqrt(1^2 + 6^2) = 6.08276253029821969
sqrt(1^2 + 7^2) = 7.07106781186547524
sqrt(1^2 + 8^2) = 8.06225774829854965
sqrt(1^2 + 9^2) = 9.05538513813741663
sqrt(1^2 + 10^2) = 10.0498756211208903