Paulo Bernardo Paulo Bernardo - 1 year ago 117
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
elif [ "$userManuf" = "n" ] || [ "$userManuf" = "N" ]; then
echo -n "Type new manufacturer name: " ; read userNewManuf
else echo "Try again!" && manuf

Answer Source

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
elif [[ $userManuf = [nN] ]]; then
    read -p "Type new manufacturer name: " assetModelManuf
    echo "Try again!" && manuf

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download