Having a for loop read in lines with spaces?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Having a for loop read in lines with spaces?
# 1  
Old 10-29-2010
Having a for loop read in lines with spaces?

Is this possible?

I have a for loop in a shell script reading a list, but I want each line to be a loop, not each thing with a space.

Here is the example:
Code:
HOSTLIST="\
1.2.3.4 serverA
1.2.3.5 serverB"

for NBUHOST in `echo $HOSTLIST`
do
    ssh ${SERVERNAME} "echo "${NBUHOST}" >>/etc/hosts"
    echo "${NBUHOST}"
done

With the way that is now it takes 1.2.3.4 and puts it in the hosts file, then takes serverA, then loops with 1.2.3.5, then loops again with serverB.

How can I make it so a whole line, space included is considered an NBUHOST and not each entry with a space delimited?

Last edited by Scott; 10-29-2010 at 02:05 PM.. Reason: Please use code tags
# 2  
Old 10-29-2010
Hi.

for treats its arguments as words (that is, each whitespace-separated argument is a word).

It's better to use a while-loop:

Code:
HOSTLIST="\
1.2.3.4 serverA
1.2.3.5 serverB"

echo "$HOSTLIST" | while read NBUHOST
do
    ssh ${SERVERNAME} "echo ${NBUHOST} >>/etc/hosts"
    echo "${NBUHOST}"
done

# 3  
Old 10-29-2010
try:
Code:
#1.2.3.4 serverA
#1.2.3.5 serverB

for NBUHOST in   '1.2.3.4 serverA'  '1.2.3.5 serverB'
do
    ssh ${SERVERNAME} "echo "${NBUHOST}" >>/etc/hosts"
    echo "${NBUHOST}"
done

# 4  
Old 10-29-2010
Code:
while read myIP myHost
do
echo "$myHost ......"
eval ssh ${myHost} "echo \"${myIP}\" >>/etc/hosts"
done<(echo $HOSTLIST)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Re: Read lines and compare in a loop

I have a file which has following content: NAME=ora.DG1.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG2.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG3.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG4.dg... (7 Replies)
Discussion started by: rcc50886
7 Replies

2. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

3. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

4. Shell Programming and Scripting

how to read blank spaces

hi i have a file which store some data.the contents of my file is data1:data2 data3:data4 i have a script which read this file correct="$(cat /root/sh | cut -d: -f1)" i used this syntax..please help me which syntax is used to read blank spaces.and then remove it and after that how to read... (1 Reply)
Discussion started by: shubhig15
1 Replies

5. Shell Programming and Scripting

for loop ( string having spaces )

Dear All, i facing problem to use string having spaces in for loop.. file used for FOR LOOP command.txt rpm -t -v ttm -D -r RJLL -h YELP rpm -t -v ttm -D -r RJLL -h ERRT rpm -t -v ttm -D -r RJLL -h TYYE rpm -t -v ttm -D -r RJLL -h POOL CODE using for execute above command... (3 Replies)
Discussion started by: arvindng
3 Replies

6. UNIX for Dummies Questions & Answers

How to read files with spaces

Hi I am a newbie to unix. I have a current script that reads a directory for excel files and renames the files. There is a problem now because some of the files have spaces. If I put quotes on the file, it will work but I dont know how to read all the files with quotes. Variables $1 =... (6 Replies)
Discussion started by: Lillyt
6 Replies

7. Shell Programming and Scripting

Read lines with different lengths in while loop

Hi there ! I need to treat files with variable line length, and process the tab-delimited words of each line. The tools I know are some basic bash scripting and sed ... I haven't got to python or perl yet. So my file looks like this obj1 0.01953 0.34576 0.04418 0.01249 obj2 0.78140... (7 Replies)
Discussion started by: jossojjos
7 Replies

8. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

9. Shell Programming and Scripting

Keep spaces with read command

Hi The following command read a string from the keyboard & echo it back. $ read S;echo "$S" ABCD ABCD As you see, the input has space. while it echo back the spaces are removed. Is there a way to keep the input intact and not removing any spaces? Also, the number of spaces may vary. ... (3 Replies)
Discussion started by: azmathshaikh
3 Replies

10. UNIX for Dummies Questions & Answers

delete blank lines or lines with spaces only

hi there i'm trying to delete blank lines and or lines with spaces only from a series of files in an directory. to do so, i'm using this: for files in `ls /users/myname/pesop* 2>/dev/null` do grep -v ^$ $files > newfile mv newfile $files done now, this works great for blank lines but... (3 Replies)
Discussion started by: vascobrito
3 Replies
Login or Register to Ask a Question