Hint needed for incrementing numbers

Hi All
Been trying to get something working but having some trouble in unix bash or ksh scripting.

Im trying to increment once a condition has been met

Say I have a file that contains:

apple 
orange
banana
grapes
dates
kiwi

What im after is once a counter has reached every second fruit it will increment and display the fruit type.

So what im looking for is a script that outputs like so

"fruit type is apple in basket1"
"fruit type is orange in basket1"
"fruit type is banana in basket2"
"fruit type is grapes in basket2"
"fruit type is dates in basket3"
"fruit type is kiwi in basket3"

I can get the counter incrementing but having difficulty getting it to do so after the second fruit. This is what i have at the moment:

#!/bin/bash
i=0
while read a
do
i=`expr $i + 1`
echo "fruit type is $a in basket$i"
done < /tmp/fruit
 
Output i get at the moment is:
fruit type is apple in basket1
fruit type is orange in basket2
fruit type is banana in basket3
fruit type is grapes in basket4
fruit type is dates in basket5
fruit type is kiwi in basket6

Anyone please able to help?

Thanks

#!/bin/bash
i=1
while read a
do
i=$((i + 1))
echo "fruit type is $a in basket$((i / 2))"
done < /tmp/fruit
1 Like

:b:
That's great, thanks nice idea
:slight_smile:

This might be a Useful Use Of Cat :

cat -n file | while read NR REST; do echo "fruit etc. $REST is in basket$(((NR+1)/2))"; done
2 Likes