【问题标题】:Invalid arithmetic operator error in bash [duplicate]bash中无效的算术运算符错误[重复]
【发布时间】:2019-04-16 15:12:17
【问题描述】:

我正在尝试在 bash 上运行以下代码,但每次我遇到错误(无效的算术运算符)时,谁能帮忙

#!/bin/bash
   for i in $(seq 20);
    do
            k=$(($i/100))
            a=$((4.18+$k))
            b=$(($a+0.01))

            siesta < BiTeI.fdf > out_file  #do the operation
            lineno=939   # set the line number where the data you want to read is in
            newline=$(sed -n "${lineno}p" "out_file") # asign a veriable for the line from the txt file note to change the file name
            echo $newline >> output.txt  # print the line extracted to an out put file note to generate file before adding to it
            sed -i "30s/$a/${b}/" BiTeI.fdf  # update the input file for new parameters for a specific raw


    done  

【问题讨论】:

  • 我试过这个,但每次都说 bc: command not found
  • 很难相信您的系统上没有安装bcuname -srv 的输出是什么?您更有可能覆盖了正常的 $PATH 变量。启动一个新终端,并发出以下 cmd echo $PATH。你看到像/bin:/usr/bin:/sbin:/usr/sbin 这样的名字吗?你应该。如果您这样做了,现在查看bc 是否可用,即。 which bc。如果它现在在那里,那么查看你的脚本,看看你在哪里重置 PATH,你需要export PATH="/new/dir:$PATH"。祝你好运。

标签: bash floating-point date-arithmetic


【解决方案1】:

尽管我不喜欢awk,但它可以让你免去一些麻烦。

$: cat x
for i in $(seq 20)
do read k a b <<< "$( echo "$i" | awk '{
      k = $1 / 100;
      a = 4.18 + k;
      b = a + 0.01;
      printf "%.2f %.2f %.2f\n", k, a, b;
   }' )"
   echo "i=$i k=$k a=$a b=$b"
done

$: ./x
i=1 k=0.01 a=4.19 b=4.20
i=2 k=0.02 a=4.20 b=4.21
i=3 k=0.03 a=4.21 b=4.22
i=4 k=0.04 a=4.22 b=4.23
i=5 k=0.05 a=4.23 b=4.24
i=6 k=0.06 a=4.24 b=4.25
i=7 k=0.07 a=4.25 b=4.26
i=8 k=0.08 a=4.26 b=4.27
i=9 k=0.09 a=4.27 b=4.28
i=10 k=0.10 a=4.28 b=4.29
i=11 k=0.11 a=4.29 b=4.30
i=12 k=0.12 a=4.30 b=4.31
i=13 k=0.13 a=4.31 b=4.32
i=14 k=0.14 a=4.32 b=4.33
i=15 k=0.15 a=4.33 b=4.34
i=16 k=0.16 a=4.34 b=4.35
i=17 k=0.17 a=4.35 b=4.36
i=18 k=0.18 a=4.36 b=4.37
i=19 k=0.19 a=4.37 b=4.38
i=20 k=0.20 a=4.38 b=4.39

【讨论】:

    【解决方案2】:

    我已经尝试过这个解决方案,它对我有用

    #!/bin/bash
    

    回显 $result

       for i in $(seq 20);
        do
                k=$(python -c "print($i/100)")
                a=$(python -c "print(4.18+$k)")
                b=$(python -c "print($a+0.01)")
                echo $k
                echo $a
                echo $b
                siesta < BiTeI.fdf > out_file  #do the operation
                lineno=939   # set the line number where the data you want to read is in
                newline=$(sed -n "${lineno}p" "out_file") # asign a veriable for the line from the txt file note to change the file name
               echo $newline >> output.txt  # print the line extracted to an out put file note to generate file before adding to it
                sed -i "30s/$a/${b}/" BiTeI.fdf  # update the input file for new parameters for a specific raw
    
    
        done  
    

    谢谢

    【讨论】:

    • 如果您可以从单个 python 调用中读取它们,将会更快。 :)
    【解决方案3】:

    当您在 bash 中遇到错误时,是时候回退到打印行调试了。在脚本顶部的 shebang 行之后添加 set -x。然后重新运行它。 Bash 会告诉你它在做什么。

    line 8: 4.18 + 0: syntax error: invalid arithmetic operator (error token is ".18 + 0")
    

    所以它说它不知道如何解析4 之后的部分:.18....。那是因为 bash 实际上不支持小数:Bash C style arithmetic with floating point value

    【讨论】:

    • 我理解错误,但每次我使用 bc 解决方案时,我都会得到 bc: command not found
    • 嗯,你需要选择一个提供数学和十进制支持的程序,安装它然后使用它。如果您需要bc,请安装它。如果您选择python,请安装它。如果您无法安装任何程序,请找到支持小数的已安装工具。你的机器上必须有某种编程语言(perlpythonphpruby...),如果找不到其他东西,就使用它。跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-08-17
    • 2020-02-27
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    相关资源
    最近更新 更多