Shell-Script

(標準輸入)1:使用 bc 時出現語法錯誤

  • November 6, 2022

我有這個工作正常的命令。但是它顯示了一個 (standard_in) 1: Syntax 錯誤,我不知道如何辨識。

participants=0
while IFS=, read -r id name nat sex date height weight sport gold silver bronze info; do
if [[ $(echo "$height>=0.1 && $height<=$2 && $weight>=0.1 && $weight<=$3" | bc) -eq 1 ]] ; then
let participants++
fi
echo -e $participants
done < $1

有人可以幫助我找出我的錯誤嗎?

謝謝

您的變數之一($height$weight或)為空$2$3不是有效數字。

$ echo "6>=5" | bc
1

# First operand is not a number
$ echo "6x>=5" | bc
(standard_in) 1: syntax error

# First operand is empty
$ echo ">=5" | bc
(standard_in) 1: syntax error

出於調試目的,我建議echo在執行bc. 這樣您就可以看到哪些變數不包含有效數字。

participants=0
while IFS=, read -r id name nat sex date height weight sport gold silver bronze info; do

 echo "\$height='$height' \$weight='$weight' \$2='$2' \$3='$3'"
 # Or:
 echo "Going echo the following to bc: $height>=0.1 && $height<=$2 && $weight>=0.1 && $weight<=$3"

 if [[ $(echo "$height>=0.1 && $height<=$2 && $weight>=0.1 && $weight<=$3" | bc) -eq 1 ]] ; then
   let participants++
 fi
 echo -e $participants
done < $1

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