Try this:
#!/bin/bash
typeset -i count=0;
for ((i=1;i<=1232;i++));
do
if [[ "$i" =~ .*3.* ]]; then
count=count+1
fi
done
echo $count
This code performs a regex searching for 3
in i
as the for
loop progresses.
Incidentally, the answer is 315.
If you need to use grep
:
#!/bin/bash
typeset -i count=0;
for ((i=1;i<=1232;i++));
do
if grep -q 3 <<<$i; then
count=count+1
fi
done
echo $count
It's a lot slower, though.