Compare bash variable to see if divisible by 5
Solution 1
Conclusion of the various comments seems to be that the simplest answer to the original question is
if ! (( $COUNTER % 5 )) ; then
Solution 2
You can do this using the modulo arithmetic operator:
#!/bin/sh
counter="$1"
remainder=$(( counter % 5 ))
echo "Counter is $counter"
if [ "$remainder" -eq 0 ]; then
echo 'its a multiple of 5'
else
echo 'its not a multiple of 5'
fi
In use:
$ ./modulo.sh 10
Counter is 10
its a multiple of 5
$ ./modulo.sh 12
Counter is 12
its not a multiple of 5
$ ./modulo.sh 300
Counter is 300
its a multiple of 5
I've also written a loop that may be what you are looking for? This will loop through every number from 1 to 600 and check if they are multiples of 5:
loop.sh
#!/bin/sh
i=1
while [ "$i" -le 600 ]; do
remainder=$(( i % 5 ))
[ "$remainder" -eq 0 ] && echo "$i is a multiple of 5"
i=$(( i + 1 ))
done
output (shortened)
$ ./loop.sh
5 is a multiple of 5
10 is a multiple of 5
15 is a multiple of 5
20 is a multiple of 5
25 is a multiple of 5
30 is a multiple of 5
...
555 is a multiple of 5
560 is a multiple of 5
565 is a multiple of 5
570 is a multiple of 5
575 is a multiple of 5
580 is a multiple of 5
585 is a multiple of 5
590 is a multiple of 5
595 is a multiple of 5
600 is a multiple of 5
Solution 3
Answering the question exactly as it's currently written, disregarding the title (which was edited).
To compare an integer in a variable to a number of other integer values, where the other values are determined ahead of time (it is unclear what "dynamically" actually means in the question):
case "$value" in
5|10|15|200|400|600)
echo 'The value is one of those numbers' ;;
*)
echo 'The value is not one of those numbers'
esac
This may also be done in a loop, of course,
for i in 5 10 15 200 400 600; do
if [ "$value" -eq "$i" ]; then
echo 'The value is one of those numbers'
break
fi
done
But this makes it harder to handle the case when $value
is not found among the given numbers without using some sort of flag:
found=0
for i in 5 10 15 200 400 600; do
if [ "$value" -eq "$i" ]; then
echo 'The value is one of those numbers'
found=1
break
fi
done
if [ "$found" -eq 0 ]; then
echo 'The value is not one of those numbers'
fi
Or, cleaner,
found=0
for i in 5 10 15 200 400 600; do
if [ "$value" -eq "$i" ]; then
found=1
break
fi
done
if [ "$found" -eq 1 ]; then
echo 'The value is one of those numbers'
else
echo 'The value is not one of those numbers'
fi
I would personally go for the case ... esac
implementation.
Related videos on Youtube
Mehran Goudarzi
Updated on September 18, 2022Comments
-
Mehran Goudarzi almost 2 years
Here is my code; I want to compare
$COUNTER
to various multiple times.if [ "$COUNTER" = "5" ]; then
It's okay, but I want it do it for dynamic times like
5,10,15,20
etc. -
Mehran Goudarzi over 6 yearsand for 200 , 400 , 600 ?
-
Jeff Schaller over 6 years@MehranGoudarzi 200, 400, and 600 are also multiples of 5...
-
Arkadiusz Drabczyk over 6 yearsAre you sure it's a good idea to enclose a number with
""
? -
Mehran Goudarzi over 6 yearsi mean $COUNTER=200 & 300 & 400 etc ..
-
smw over 6 yearsIn bash, can't this be simplified to just
if ((counter%5 == 0)); then ...
? -
jesse_b over 6 years@steeldriver: Yes.
-
don_crissti over 6 yearsor
if ! ((counter%5)); then...
-
jesse_b over 6 yearsI have made my answer in
sh
because op is using[
rather than[[
-
Kusalananda over 6 years@ArkadiuszDrabczyk Yes. In the (unlikely) event that
$IFS
contains a digit. -
Arkadiusz Drabczyk over 6 years@Kusalananda: also in
arithmetic expansions
? MyBash 4.3.46
returns error when number is enclosed in""
:./modulo.sh: line 5: "7"%5: syntax error: operand expected (error token is ""7"%5")
-
Kusalananda over 6 years@ArkadiuszDrabczyk In an arithmetic context (inside
$(( ... ))
) quotes and$
are not needed. I thought you were talking about the[ ... ]
test`. -
Arkadiusz Drabczyk over 6 years@Kusalananda: not needed implies that they are optional, are they legal at all? OP changed his answer, he used
""
in$((...))
before. -
Kusalananda over 6 years@ArkadiuszDrabczyk Not the quotes.
-
Arkadiusz Drabczyk over 6 years@Kusalananda: I don't understand. What do you mean by
not the quotes
? -
Kusalananda over 6 years@ArkadiuszDrabczyk The quotes are not legal inside
$(( ... ))
, as far as I can tell. The$
on variable names is optional. -
Arkadiusz Drabczyk over 6 years@Kusalananda: ok, that's what I meant in my comment.
-
roaima over 6 years@Kusalananda in which case something like
if (( (k/5)*5 == k ))
or evenif [ $k -ne `expr $k / 5 \* 5` ]
-
Kusalananda over 6 years@roaima It's the double parentheses that is the issue, not the modulus operator.
-
zyy over 2 yearsWhy is there an exclamation mark in front? It makes the expression look like finding non-divisible numbers...
-
steve over 2 yearsBecause it's examining the remainder of counter divided by 5. If result is 0, then it's divisible by 5. If the result is anything else, it's not divisible by 5. Hence the need for
!
.