Paulo Bernardo Paulo Bernardo - 4 months ago 9
Linux Question

line 8: [: N: integer expression expected

I'm receiving the error line 8: [: N: integer expression expected. I have tried removing the " " in the $userManuf and changing = to == and I still receive the error. What should I do? Thanks

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
}

Answer

The error is occurring when $userManuf expands to N, but you try to use it as an operand with the integer operator -ne. The simplest fix would be to verify that it has a numerical value first (then use an arithmetic statement to simplify the comparison):

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

You probably also want to use a while loop instead of calling manuf recursively. I'll leave that as an exercise (or a future question, if necessary).