for hi in `seq 0 100`
do
new_val=1
if `expr $hi % 5` -eq 5
then
echo hello
elif `expr $hi % 5` -eq 6
then
echo bye
elif `expr $hi % 5` -eq 7
then
echo whats up
fi
echo $new_val
done
Why does this crash? The goal is to check if the loop number module 5 equals 5,6, or 7.
The script corrected shall be something like:
#!/bin/sh
for hi in $(seq 0 100)
do
if [ "$(expr $hi % 5)" -eq 5 ]
then
echo hello
elif [ "$(expr $hi % 5)" -eq 6 ]
then
echo bye
elif [ "$(expr $hi % 5)" -eq 7 ]
then
echo whats up
fi
echo "$hi"
done
But that loop will never enter any of the ifs as the remainder of a modulus 5 operation will never be 5, 6 or 7.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments