For loop and awk print

Hi,

i am using a variable tmpVar, using variable data i need implement for loop

tmpVar="abc bbc cbc nbc mbc"    # valiable having total number of words= 5 so i need to loop 5 times. 


tmpVar="abc bbc cbc nbc mbc" 
tmpcnt=`echo $tmpVar|wc -w`
for cnt in 1..$tmpcnt
do
t1=`echo $tmpVar|awk -F" " '{print $cnt}'`      # this is not working. it should take the value first word in the string then process 
echo $t1
#checking if t1 = abc then we are implementing some logic and so on....
-
-
-
-
done


 


If you are using bash or ksh93 , try:

for cnt in {1..$tmpcnt}

But this is not a very efficient way of doing this..

Why do you use that difficult loop construct?

for t1 in "$tmpvar"; do ...; done

will loop through all five substrings in your tmpvar. If you need a count inside the loop, set and use one, like (( cnt++ )) .

Oh dear...and ' $cnt ' does not substitute the shell variable.
Better a simple for loop

tmpVar="abc bbc cbc nbc mbc"
for t1 in $tmpVar
do
 echo "$t1"
done

Or just printf "%s\n" $tmpVar