Bc

(standard_in) 1: 使用 bc 時出現語法錯誤

  • January 5, 2019

我寫了一個.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: syntax error

具體來說,我正在討論這段程式碼:

           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

我很難理解為什麼它會出錯。

完整程式碼連結:

.tcl 文件生成 .tr 文件 .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

您的腳本似乎沒有初始化變數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

引用自:https://unix.stackexchange.com/questions/492609