(standard_in) 1:应该工作的脚本语法错误

(standard_in) 1: syntax error on script that should work

我从 github 下载了一个脚本,它似乎可以工作,但在 运行 时出现错误。

https://github.com/chentda/curl-average-timings

脚本从 URL

计算平均加载时间
#!/bin/bash

#This is a simple bash script to get the averages of various website response timings using cURL

#Formatted cURL output that contains the various response timings separated by '-'
curl_format="%{time_namelookup}-%{time_connect}-%{time_appconnect}-%{time_pretransfer}-%{time_redirect}-%{time_starttransfer}-%{time_total}"
#Website you want to send the request to
URL="https://www.google.com/"
#How many requests you want to hit website with to get averages
iterations=5
decimal_rounding=5

#Initialising total variables for the various timings
total_time_namelookup="0"
total_time_connect="0"
total_time_appconnect="0"
total_time_pretransfer="0"
total_time_redirect="0"
total_time_starttransfer="0"
total_time_total="0"

for i in `seq 1 $iterations`;
do
    response=$(curl -o /dev/null -s -w $curl_format $URL)

    #Splits response string by the delimiter of "-"
    response_times=($(echo "$response" | tr "-" "\n"))

    #Assigning each type of response time to a variable
    time_namelookup=${response_times[0]}
    time_connect=${response_times[1]}
    time_appconnect=${response_times[2]}
    time_pretransfer=${response_times[3]}
    time_redirect=${response_times[4]}
    time_starttransfer=${response_times[5]}
    time_total=${response_times[6]}

    #Adding variables assigned above in loop to the respective total variables that are set at start of script
    total_time_namelookup=$(echo "$total_time_namelookup + $time_namelookup" | bc)
    total_time_connect=$(echo "$total_time_connect + $time_connect" | bc)
    total_time_appconnect=$(echo "$total_time_appconnect + $time_appconnect" | bc)
    total_time_pretransfer=$(echo "$total_time_pretransfer + $time_pretransfer" | bc)
    total_time_redirect=$(echo "$total_time_redirect + $time_redirect" | bc)
    total_time_starttransfer=$(echo "$total_time_starttransfer + $time_starttransfer" | bc)
    total_time_total=$(echo "$total_time_total + $time_total" | bc)
done

#Calculating the average for each type of response time
average_time_namelookup=$(echo "scale=$decimal_rounding; $total_time_namelookup / $iterations" | bc)
average_time_connect=$(echo "scale=$decimal_rounding; $total_time_connect / $iterations" | bc)
average_time_appconnect=$(echo "scale=$decimal_rounding; $total_time_appconnect / $iterations" | bc)
average_time_pretransfer=$(echo "scale=$decimal_rounding; $total_time_pretransfer / $iterations" | bc)
average_time_redirect=$(echo "scale=$decimal_rounding; $total_time_redirect / $iterations" | bc)
average_time_starttransfer=$(echo "scale=$decimal_rounding; $total_time_starttransfer / $iterations" | bc)
average_time_total=$(echo "scale=$decimal_rounding; $total_time_total / $iterations" | bc)

echo "Averages of response timings:"
echo ""
echo "   time_namelookup: $average_time_namelookup"
echo "      time_connect: $average_time_connect"
echo "   time_appconnect: $average_time_appconnect"
echo "  time_pretransfer: $average_time_pretransfer"
echo "     time_redirect: $average_time_redirect"
echo "time_starttransfer: $average_time_starttransfer"
echo "                    --------"
echo "        time_total: $average_time_total"

经过几次debuggin,发现问题出在这一行,用bc求和的时候

total_time_namelookup=$(echo "$total_time_namelookup + $time_namelookup" | bc)

但是它抛出

(standard_in) 1: syntax error 

但我不知道我做错了什么。

我正在 ubuntu 运行 从终端使用 sh

您的数值包含 locale 特定的小数点分隔符 (,)。 bc只认识.。将包含数值的字符串通过 运行 转换为 tr , '.',然后再将它们发送到 bc.

或者,按照 tripleee 的建议,在脚本顶部明确设置 C 语言环境:

export LC_ALL=C

对于更复杂的 locale 操作,您可能需要库支持: https://github.com/leagris/lcnumconv.sh