(标准输入)1:使用 bc 时出现语法错误

(标准输入)1:使用 bc 时出现语法错误

我有这个命令,工作正常。但是它显示了 (standard_in) 1: 语法错误,我不知道如何识别。

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

有人可以帮助我找出我的错误吗?

谢谢

答案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

相关内容