我收到错误行8:[:N:预期为整数表达式。我尝试在$ userManuf中删除“”并将=更改为==,但仍然收到错误。我该怎么办?谢谢

function manuf {
 echo -n "Manufacturer: " ; read userManuf
 varManuf=$(mysql -D snipeit -e "SELECT count(id) FROM manufacturers;")
 countManuf=$(echo $varManuf | awk '{print $2}')

 if [ $userManuf -ne 0 ] && [ $userManuf -le $countManuf ]; then
    assetModelManuf=$userManuf
 elif [ "$userManuf" = "n" ] || [ "$userManuf" = "N" ]; then
    echo -n "Type new manufacturer name: " ; read userNewManuf
    assetModelManuf=$userNewManuf
 else    echo "Try again!" && manuf
 fi
}

最佳答案

$userManuf扩展为N时发生错误,但是您尝试将其用作整数运算符-ne的操作数。最简单的解决方法是首先验证它具有数字值(然后使用算术语句简化比较):

if [[ $userManuf =~ [0-9]+ ]] && (( 0 < userManuf && userManuf <= countManuf )); then
    assetModelManuf=$userManuf
elif [[ $userManuf = [nN] ]]; then
    read -p "Type new manufacturer name: " assetModelManuf
else
    echo "Try again!" && manuf
fi


您可能还希望使用while循环,而不是递归调用manuf。我将其保留为练习(或如有必要,以后再提出问题)。

07-26 05:32