(standard_in) 1: 使用 bc 时语法错误

(standard_in) 1: 使用 bc 时语法错误

我编写了一个.awk.tr文件执行某些操作并将输出写入文件的程序。END文件部分打印.awk如下:

printf("%15.2f\n%15.5f\n%15.2f\n%15.2f\n%15.2f\n%10.2f\n%10.2f\n%10.5f\n", rThroughput, rAverageDelay, nSentPackets, nReceivedPackets, nDropPackets, rPacketDeliveryRatio, rPacketDropRatio,rTime) ;

printf("%15.5f\n%15.5f\n%15.5f\n%15.5f\n%15.0f\n%15.9f\n", total_energy_consumption, avg_energy_per_bit, avg_energy_per_byte, avg_energy_per_packet, total_retransmit,rEnergyEfficiency);

我从一个文件中调用这个.awk文件.sh。执行运行该文件的命令后.awk,我迭代该.awk文件生成的值。

        awk -f Wireless_udp.awk 802_11.tr > "TEMP"

        while read val
        do
            l=$(($l + 1))

            if [ "$l" == "1" ]; then
                thr=$(echo "scale=5; $thr+$val/$iteration_float" | bc)
#               echo -ne "throughput: $val " 
            elif [ "$l" == "2" ]; then
                del=$(echo "scale=5; $del+$val/$iteration_float" | bc)
#               echo -ne "delay: $val " 
            elif [ "$l" == "3" ]; then
                s_packet=$(echo "scale=5; $s_packet+$val/$iteration_float" | bc)
#               echo -ne "send packet: $val " 
            elif [ "$l" == "4" ]; then
                r_packet=$(echo "scale=5; $r_packet+$val/$iteration_float" | bc)
#               echo -ne "received packet: $val " 
            elif [ "$l" == "5" ]; then
                d_packet=$(echo "scale=5; $d_packet+$val/$iteration_float" | bc)
#               echo -ne ;"drop packet: $val " 
            elif [ "$l" == "6" ]; then
                del_ratio=$(echo "scale=5; $del_ratio+$val/$iteration_float" | bc)
#               echo -ne "delivery ratio: $val " 
            elif [ "$l" == "7" ]; then
                dr_ratio=$(echo "scale=5; $dr_ratio+$val/$iteration_float" | bc)
#               echo -ne "drop ratio: $val "
            elif [ "$l" == "8" ]; then
                time=$(echo "scale=5; $time+$val/$iteration_float" | bc)
#               echo -ne "time: $val " 
            elif [ "$l" == "9" ]; then
                t_energy=$(echo "scale=5; $t_energy+$val/$iteration_float" | bc)
#               echo -ne "total_energy: $val "
            elif [ "$l" == "10" ]; then
                energy_bit=$(echo "scale=5; $energy_bit+$val/$iteration_float" | bc)
#               echo -ne "energy_bit: $val "
            elif [ "$l" == "11" ]; then
                energy_byte=$(echo "scale=5; $energy_byte+$val/$iteration_float" | bc)
#               echo -ne "energy_byte: $val " 
            elif [ "$l" == "12" ]; then
                energy_packet=$(echo "scale=5; $energy_packet+$val/$iteration_float" | bc)
#               echo -ne "energy_packet: $val " 
            elif [ "$l" == "13" ]; then
                total_retransmit=$(echo "scale=5; $total_retransmit+$val/$iteration_float" | bc)
#               echo -ne "total_retrnsmit: $val \n"
            elif [ "$l" == "14" ]; then
                energy_efficiency=$(echo "scale=9; $energy_efficiency+$val/$iteration_float" | bc)
                #       echo -ne "energy_efficiency: "
            fi
#           echo "$val"
        done < "TEMP"

一切都运行良好,但是当我添加最后一个if-else条件并执行脚本时,它给出了 (standard_in) 1: 语法错误

具体来说,我正在考虑这一段代码:

            elif [ "$l" == "14" ]; then
                energy_efficiency=$(echo "scale=9; $energy_efficiency+$val/$iteration_float" | bc)
#               echo -ne "energy_efficiency: "

TEMP文件包含:

      197645.74
        0.32776
       25000.00
        7350.00
       17348.00
     29.40
     69.39
  24.99826
      720.13300
        0.00015
        0.00117
        0.09798
              0
    0.001166018

我很难理解为什么它会出错。

完整代码链接:

生成 .tr 文件的 .tcl 文件 .awk 文件: .sh 文件:

bash --version给出:

GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

操作系统:Ubuntu 16.04 LTS

答案1

您的脚本似乎没有初始化变量energy_efficiency,因此第一次循环

echo "scale=9; $energy_efficiency+$val/$iteration_float"

产生

scale=9; +197645.74/1.0

这是一个语法错误(bc显然允许一元-,但不允许一元+

$ echo "scale=9; +197645.74/1.0" | bc
(standard_in) 1: syntax error

相关内容