Problem reading file in while/read loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem reading file in while/read loop
# 1  
Old 09-10-2010
Problem reading file in while/read loop

I know I should be able to see a way of doing this easily, but my brain just won't engage.

I have a script working on an embedded device that checks to see if an item is in a blacklist before performing some actions.

At the moment the code reads thus....

Code:
while read BLACKLIST ; do

execute some code
execute some code

done < /path/to/BLACKLIST.txt

The items in BLACKLIST.txt are single words separated by a \n

I'm trying to replace the BLACKLIST.txt file by putting the list directly into the script as a variable (INBLACKLIST) at the top of the file with each item separated by a space and then using tr " " "\n" to put them on separate lines. This is to try to make the script as portable as possible without relying on any outside dependencies.

What I was hoping to do in the new code was

Code:
echo "$INBLACKLIST" | while read BLACKLIST ; do

execute some code
execute some code

done

This isn't working, the first value gets passed to BLACKLIST but it looks like the while read isn't working.

I'm sure there's a better way to do this but to be honest I'm so tired my brain has frozen up.......can anyone suggest another way.

Thanks in advance and I'm going to put on a fresh pot of coffee in the hope someone out there can help me!!

Last edited by zaxxon; 09-10-2010 at 05:13 AM.. Reason: subject change
# 2  
Old 09-10-2010
Change the subject of your thread please.

Please post a snippet of this blacklist.
# 3  
Old 09-10-2010
Something like :

Code:
while read BLACKLIST ; do

execute some code
execute some code

done < ${INBLACKLIST}

# 4  
Old 09-10-2010
If the items are separated by a space in the variable $INBLACKLIST you can do something like:
Code:
for BLACKLIST in "$INBLACKLIST"
do
  # Do something with $BLACKLIST
  # execute some code
done

# 5  
Old 09-10-2010
Hi Guys

Klashxx , that's still piping from a file surely? It doesn't work I'm afraid.


Franklin.

I tried with the variables on INBLACKLIST being either space separated or newline separated and it doesn't work either way, this could be because it's an ASH rather then BASH shell? (embedded device using busybox).

either way the `for BLACKLIST in "$INBLACKLIST"` is reading in all the values in $INBLACKLIST (either on one line or multiple lines as separated).

I have put an echo "$BLACKLIST" immediately after the `for BLACKLIST....` to check.

Any other ideas please chaps?
# 6  
Old 09-10-2010
Quote:
Originally Posted by Bashingaway
Franklin.

I tried with the variables on INBLACKLIST being either space separated or newline separated and it doesn't work either way, this could be because it's an ASH rather then BASH shell? (embedded device using busybox).

either way the `for BLACKLIST in "$INBLACKLIST"` is reading in all the values in $INBLACKLIST (either on one line or multiple lines as separated).

I have put an echo "$BLACKLIST" immediately after the `for BLACKLIST....` to check.

Any other ideas please chaps?
Oops, try it without the double quotes around $INBLACKLIST in the loop:
Code:
for BLACKLIST in $INBLACKLIST
do
  # Do something with $BLACKLIST
  # execute some code
done

# 7  
Old 09-10-2010
Thanks Franklin

That did it, I can get some sleep for a couple of hours now.

Have a good weekend.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sequential Reading from two file in a loop

Hello All, I have two files with me file1.txt and file2.txt file1.txt has: 333 222 111 file2.txt has ccc bbb aaa ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only. I have to get the values from each of the file and pass them in the URL... (3 Replies)
Discussion started by: ankur328
3 Replies

2. Shell Programming and Scripting

Problem with while loop reading every line of a text file

Hello, I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below: # cat /tmp/passwd_sample CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash... (4 Replies)
Discussion started by: reddyr
4 Replies

3. Shell Programming and Scripting

Removing \r and \n during reading file through while loop

Hi, I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt There might be very large number of this path in same file like say 140 when I try to run while command: while read -r file do //command... (8 Replies)
Discussion started by: Pulkit Lall
8 Replies

4. Shell Programming and Scripting

Loop is not reading tabs from the file

Hi, I am on HP-UX and K shell. When I am using while/for loop for reading a file. It is working fine but not reading tabs: Suppose, if the line is: ; ;COMP; ; ; ; then loop is reading as ; ;COMP; ;... (5 Replies)
Discussion started by: ezee
5 Replies

5. 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

6. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

7. AIX

How to pause a while loop while reading from a file

Hi, I am building a script to grep for a string in all the files from a folder and display the results. I am reading the files one by one by placing the names in other file using while loop my code is as below while read inp do chk=`grep -c "$str" $pth/$inp` ... (2 Replies)
Discussion started by: sekhar gajjala
2 Replies

8. UNIX for Advanced & Expert Users

"while read ..." loop exiting after reading only one record

Greeting, The following script completes after reading only one record from the input file that contains many records. I commented out the "ssh" and get what I expect, an echo of all the records in the input.txt file. Is ssh killing the file handle? On the box "uname -a" gives "SunOS... (2 Replies)
Discussion started by: twk
2 Replies

9. UNIX for Dummies Questions & Answers

problem in reading inside a while loop

I am not able to read inside a while though i get the message "inside read" the cursor doesnt prompt from the console cat file | while read ln_new_engine_dirs do echo "inside $ln_new_engine_dirs" if then read nn echo "inside read" fi done Thanks in advance (3 Replies)
Discussion started by: ssuresh1999
3 Replies

10. Shell Programming and Scripting

reading from within the loop --std i/p problem

i have a teat file having data like one 12/3 two 23/09 three 12/12 now from another script i want to read one line at a time ,cut field one and two in two separate variable ,compare field1 with another variable but outside the loop .If i found a match i want to take from user the value... (2 Replies)
Discussion started by: mobydick
2 Replies
Login or Register to Ask a Question