Shell Script on for do

Hi all,
I have a list of servers in a file test
I have used awk to print the particular column.I need to configure 100 servers but the description has a space between .Hence the configuration file is not created properly

#cat test
host1,10.0.0.1,AD server
host2,10.0.0.2,File server
host3,10.0.0.3,Print Server

The script is

# vim test.sh
for i in $(cat test);do
     h=$(grep $i test |  awk -F "," '{print $1}')
     P=$(grep $i test |  awk -F "," '{print $2}')
     S=$(grep $i test |  awk -F "," '{print $3}')
     echo $S,$P,$H >> ip
     #echo define host{ >> hosts.cfg
     #       echo -e ' \t ' "use"  ' \t '   ' \t '            hoststatus >> hosts.cfg
     #        echo -e ' \t ' "host_name"  ' \t '       $h >> hosts.cfg
     #        echo -e ' \t ' "alias"    ' \t '         $S >> hosts.cfg
     #        echo -e ' \t ' "address"   ' \t '       $P >> hosts.cfg
     #        echo -e ' \t ' "hostgroups" ' \t '    monitor  >> hosts.cfg
     #echo } >> hosts.cfg
done

the output is

#cat test
AD server
File server
Print Server,10.0.0.1
10.0.0.2
10.0.0.3,host1
host2
host3
Print Server,10.0.0.3,host3

Hence I used sed to replace the space which some character

# cat test
host1,10.0.0.1,AD;server
host2,10.0.0.2,File;server
host3,10.0.0.3,Print;Server
#sh test.sh

the output is fine .Why is the space between the two words not printed properly irrespective of me specifing the delimiter :(.Should I use LFS="/n" or some parameter to fix this issue

Need your expertise for the same :smiley:

You definitely shouldn't use neither LFS nor "/n". :slight_smile:
But you can use IFS=$'\n' or better while loop instead of for:

while read i; do
  ...
done <test 

You can easy to see what happens with:

for i in $(cat test); do
  echo $i
done

when I use IFS=$'\n'

AD server,10.0.0.1,host1
AD server File server,10.0.0.1 10.0.0.2,host1 host2
File server,10.0.0.2,host2
AD server File server,10.0.0.1 10.0.0.2,host1 host2
Print Server,10.0.0.3,host3
Print Server,10.0.0.3,host3
 

instead of this output

AD server,10.0.0.1,host1
File server,10.0.0.2,host2
Print Server,10.0.0.3,host3

Why is the space creating this issue