-1

Shell script for loop is behaving unexpected ! The requirement is need to iterate the loop line by line of the property file. Its mandatory to use the for loop.

hostnamefl=/tmp/propperty.txt

for property  in $(cat $hostnamefl)
do
hostname=`echo $property | awk '{print $1}'`
echo $hostname
user=`echo $property | awk '{print $2}'`
echo $user
pas=`echo $property | awk '{print $3}'`
echo $pas
echo "---------------------------------------------------------------------------------"
done

propperty.txt

host1 host1user host1pas
host2 host2user host2pas
host3 host3user host3pas

I am getting the result as given below :

host1
---------------------------------------------------------------------------------
host1user
---------------------------------------------------------------------------------
host1pas
---------------------------------------------------------------------------------
host2
---------------------------------------------------------------------------------
host2user
---------------------------------------------------------------------------------
host2pas
---------------------------------------------------------------------------------
host3
---------------------------------------------------------------------------------
host3user
---------------------------------------------------------------------------------
host3pas
---------------------------------------------------------------------------------

Expected result:

host1
host1user
host1pas
---------------------------------------------------------------------------------
host2
host2user
host2pas
---------------------------------------------------------------------------------
host3
host3user
host3pas
---------------------------------------------------------------------------------

For loop is iterating word by word rather than line by line, how should I resolve it ?

200_success
  • 4,771
  • 1
  • 25
  • 42
Arn
  • 13
  • 5
  • This question would be best addressed on the UNIX SE and certainly this is not a place to handle homework problems. – mdpc Dec 22 '12 at 06:04

4 Answers4

2

That's how for loops work — they treat all input as whitespace-delimited words. What you want is the read builtin command, which reads a line at a time.

while read line ; do
    for word in $line; do
        echo $word
    done
    echo ---------------------------------------------------------------------------------
done < /tmp/propperty.txt
200_success
  • 4,771
  • 1
  • 25
  • 42
0

If cat /tmp/propperty.txt does output setting the IFS variable might help

host1 host1user host1pas  
host2 host2user host2pas  
host3 host3user host3pas  

See Sepcial Paramenters in bash man page

IFS=:
hostnamefl=/tmp/propperty.txt

for property  in $(cat $hostnamefl)
do
hostname=`echo $property | awk '{print $1}'`
echo $hostname
user=`echo $property | awk '{print $2}'`
echo $user
pas=`echo $property | awk '{print $3}'`
echo $pas
echo "---------------------------------------------------------------------------------"
done
rhasti
  • 497
  • 3
  • 9
0

If rhasti's answer doesn't work for you. You can do this

while read property           
do           
    hostname=`echo $property | awk '{print $1}'`
    echo $hostname
    user=`echo $property | awk '{print $2}'`
    echo $user
    pas=`echo $property | awk '{print $3}'`
    echo $pas
    echo "---------------------------------------------------------------------------------"

done < /tmp/propperty.txt 
Mike
  • 22,310
  • 7
  • 56
  • 79
  • Sorry Mike I couldint use the while loop, its mandatory to use the for loop. – Arn Dec 22 '12 at 05:53
  • ohh so it's a homework assignment then – Mike Dec 22 '12 at 05:58
  • Thanks Mike, Do to some application behavior we are in need of only to user the "for loop", so Its mandatory to use the for loop. The requirement is, need to iterate the loop line by line of the property file. I dont know why the for loop is behavior is like this, I think it is taking column ... – Arn Dec 22 '12 at 06:08
0

read can parse the line for you: there is no need for awk

while read hostname user pas
do
   echo $hostname
   echo $user
   echo $pas
   echo -------------
done < /tmp/property.txt
ramruma
  • 2,740
  • 1
  • 15
  • 8