dimanche 16 octobre 2016

Integer expression expected BASH scripting IF statement

I am trying to insert names and numbers in a text file. I have wrote a short script for the learning purpose.

v= expr $# % 2
echo $v
if [ "$v" -eq 0 ]; then
    i=1
    while [ "$i" -lt $# ]
    do
        echo "$i    $i+1" >> database
        i=$((i+1))
    done
echo "User(s) successfully added \n\v"
else
    echo "Arguments are not complete";
fi

When i enter two arguments, the shell output is as follows

0                     # (The value of variable v)
./myscript: line 3: [: : integer expression expected
Arguments are not complete  # (else statement is executed)

When i replace -eq to == in line 3 (if statement), error msg is gone but still the IF statement doesn't execute as i expect.

0              # (output of variable v)
Arguments are not complete # (else statement is executed)

Aucun commentaire:

Enregistrer un commentaire