dimanche 4 janvier 2015

Why do I get "integer expression required"?


I'm trying to have a small function that I then call.


To keep it simple I just want to validate that 12 is divisible by 2,3 and 4


The code is:



divisible_by () {
under_test=12
from=2
to=4
divisible=0
for ((check=from; check<=to; check++)) {
echo "check= $check"
if [ $under_test % $check -ne 0 ]; then
divisible=1
fi
}
return $divisible
}

x=divisible_by
if [ $x -eq 0 ]; then
echo "true"
else
echo "false"
fi


Currently I get



./5_divisible_by_1_to_10.sh: line 16: [:
divisible_by: integer expression expected
false


I'm also finding it hard to pass the number in as a parameter but maybe it's related.



Aucun commentaire:

Enregistrer un commentaire