LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-30-2004, 09:27 PM   #1
linlu
LQ Newbie
 
Registered: Nov 2001
Location: Virginia
Posts: 20

Rep: Reputation: 0
Question Fedora Core 1, won't read in file to variable


Hi all,
I am attempting to port a bash script that worked fine on RedHat 7.1 to Fedora Core 1. I am having trouble reading a file into a variable. Nothing gets read into the variable. The source file has not changed.

Sample source file:
4.40.0.0/19
12.211.156.0/22
12.221.220.0/22

It goes on for 454 lines or so.

I copied the troublesome section into a test script

#! /bin/bash
BlockedHosts="/etc/rc.d/blocked.lst"
if [ -f "$BlockedHosts" ]; then
while read ATTACKERS; do
echo "$ATTACKERS" > /dev/null
done < "$BlockedHosts";
fi
echo "$ATTACKERS"

When I run my test script, the result is a blank line.

I also tried this mod with no success based on another post in this forum.

#! /bin/bash
BlockedHosts="/etc/rc.d/blocked.lst"
if [ -f "$BlockedHosts" ]; then
cat ${BlockedHosts} | while read ATTACKERS; do
echo "$ATTACKERS" > /dev/null
done < "$BlockedHosts";
fi
echo "$ATTACKERS"

I did not come up with the first version of the code, I merely copied it from the previous script that worked on the RH7.1 that my coworker created. He can't figure it out either. He said this problem started on RedHat 8.
I am not quite a newbie, but close enough...
Thanks for any assistance
 
Old 06-30-2004, 09:34 PM   #2
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
> When I run my test script, the result is a blank line.

The result probably is the LAST line of your file.
If the last line is empty, you get an empty line.

All the other lines are sent to /dev/null (the binary black hole)

what is this script supposed to do ?
 
Old 06-30-2004, 10:34 PM   #3
linlu
LQ Newbie
 
Registered: Nov 2001
Location: Virginia
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks for attempting to answer.
The last line was a blank in blocked.lst, so I removed that and I received the same result an empty line. By your implied answer I should have seen instead:
221.144.0.0/12
But I did not. So it's not reading the file period as far as I can tell.

So if the /dev/null in this statement is incorrect,
echo "$ATTACKERS" > /dev/null
what is correct? What do I echo it into?

Thanks
 
Old 06-30-2004, 11:48 PM   #4
linlu
LQ Newbie
 
Registered: Nov 2001
Location: Virginia
Posts: 20

Original Poster
Rep: Reputation: 0
The answer is....

Here's what worked... I found it myself...

ATTACKERS=$(cat $BlockedHosts)

I had to find the advanced bash shell guide for this, command substitution is what it is called.

Also if I use:
echo $ATTACKERS
everything is run onto one line.
If I use:
echo "$ATTACKERS"
then my newlines are preserved....

Go figure....
 
Old 07-01-2004, 06:47 PM   #5
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
>#! /bin/bash
>BlockedHosts="/etc/rc.d/blocked.lst"
>if [ -f "$BlockedHosts" ]; then
>cat ${BlockedHosts} | while read ATTACKERS; do
>echo "$ATTACKERS" > /dev/null
>done < "$BlockedHosts";
>fi
>echo "$ATTACKERS"

The last line is always empty.
reason: the while loop ends when there
are no more data left (i.e. empty)

still don't understand what the script is supposed to do
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read variable from one file & update its value in another file minil Programming 1 03-22-2005 12:12 AM
Can Fedora Core 3 mount and read vmfs file system smkamene Linux - Software 2 01-24-2005 09:24 PM
How to read a variable from a file? Andrew Benton Programming 2 12-29-2004 10:50 PM
Fedora Core 2 CD read problems drcyber Linux - Newbie 10 09-07-2004 10:39 AM
can i read NTFS from fedora core 2? Napoleon_Jo Red Hat 0 07-08-2004 11:45 PM


All times are GMT -5. The time now is 10:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration