Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question

closed as off topic by EEAA, Mike, mdpc, chutz, faker Dec 22 '12 at 9:47

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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 6:04

4 Answers 4

up vote 2 down vote accepted

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
share|improve this answer

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
share|improve this answer

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 
share|improve this answer
    
Sorry Mike I couldint use the while loop, its mandatory to use the for loop. –  Arn Dec 22 '12 at 5:53
    
ohh so it's a homework assignment then –  Mike Dec 22 '12 at 5: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 6:08

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
share|improve this answer
    
It is not working, I need to itrate the loop row base of property.txt... –  Arn Dec 22 '12 at 5:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.