LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: Read entire file line in for loop (https://www.linuxquestions.org/questions/programming-9/bash-read-entire-file-line-in-for-loop-240016/)

clinton 10-07-2004 07:44 PM

BASH: Read entire file line in for loop
 
Hi there,

Does anyone know of a way to read an entire file line as an argument to a for loop?

For example: we have my.file:
My name is boo.
I like to foo.
Isn't that coo?

Then, the BASH script:
[code]
for entry in $(cat my.file)
do
....
done
[end code]

What I want is for each _line_ of the file to be $entry. That is:
$entry = My name is boo.
$entry = I like to foo.
$entry = Isn't that coo?

But, in the code above, $entry will be each word. That is:
$entry = My
$entry = name
$entry = is
...
etc.


I have tried using double quotes aroung the my.file or the entire command substitution, but nothing seems to work.

Any ideas?

Thanks very much for your help

Tinkster 10-07-2004 08:04 PM

IFS=$'\n'



Cheers,
Tink

Hko 10-08-2004 04:07 AM

Re: BASH: Read entire file line in for loop
 
Quote:

Originally posted by clinton
Does anyone know of a way to read an entire file line as an argument to a for loop?
Instead of the "IFS-way" above, I think it's better to use a while loop. Because changing IFS has side-effects you may have to take care of (depending on the rest of your script).
Code:

#!/bin/bash

N=0
cat my.file | while read LINE ; do
        N=$((N+1))
        echo "Line $N = $LINE"
done

or:

Code:

#!/bin/bash

N=0
while read LINE ; do
        N=$((N+1))
        echo "Line $N = $LINE"
done < my.file

P.S. Instead of [ end code ] use [ /code ]
(without the spaces)

clinton 10-08-2004 11:32 AM

Thanks you two, for your help.

I used Hko's suggestion: works like a charm!

As to Tinkster's, this may be a foolish question, but were you suggesting I change $IFS for cat? I wasn't aware that cat had such a variable.

If $IFS is an environment variable, I don't have that set in my environment. Perhaps it is known as something else.....?

I'm using RH Enterprise 3, btw.

Thanks again for your help!

Hko 10-08-2004 12:16 PM

Quote:

Originally posted by clinton
If $IFS is an environment variable, I don't have that set in my environment. Perhaps it is known as something else.....?
$IFS is a special environment variable, and you do have it set, because it alwasy exists. You only (almost) don't see it, because by default is only contains a line-feed.

It think Tinkster means something like this:
Code:

#!/bin/bash

IFS=$'\n'
set $(cat my.file)

# Now the lines are stored in $1, $2, $3, ...

echo $1
echo $2
echo $3
echo $4


clinton 10-08-2004 12:48 PM

Of course I couldn't see a newline.... yeesh

Brain fart on my part there.

Thanks :)

Dragineez 07-22-2006 03:31 PM

Thanx!
 
Yet another example of how a thread from the dead can help with a current problem.

SeekingWisdom 07-27-2006 01:58 AM

I would've done it Tkinsters way:

OLDIFS="$IFS"
IFS=
for entry in `cat foo`; do [ code here ]; done

SeekingWisdom 07-27-2006 02:01 AM

I would've done it Tkinsters way:
Code:

OLDIFS="$IFS"
IFS=
for entry in `cat foo`; do [ code here ]; done
IFS="$OLDIFS"

You can save IFS, and later change it back if necessary.
Quote:

Originally Posted by Hko
You only (almost) don't see it, because by default is only contains a line-feed.

And a space and a tab, if memory serves.

ifeatu 05-25-2010 11:13 AM

Syntax Help
 
I'm trying to read a file in using the syntax above to read in a list of users and create a user.bat for them for some reason this doesn't seem to work:

Code:

#!/bin/bash

FS=$'\n'
set $(cat users.txt)

for i in $FS; do

`cp logon.bat ./$1.bat`


# Now the lines are stored in $1, $2, $3, ...

echo $1
echo $2
echo $3
echo $4
done

Can anyone help?

ifeatu 05-25-2010 11:29 AM

Update...
 
Okay I've made some changes to the file and now it only prints the last line of the file, its not iterating thru the loop:

Code:

#!/bin/bash
i=0;
FS=$'\n'
set $(cat users.txt)

for i in $_; do

#cp ~/scripts/logon.bat ./$i.bat


# Now the lines are stored in $1, $2, $3, ...

echo $i
done


catkin 05-25-2010 12:33 PM

Quote:

Originally Posted by Hko (Post 1222433)
$IFS is a special environment variable, and you do have it set, because it alwasy exists.

It is so important that if you do unset it, bash behaves in all ways as if it had its default value. This means you never need to save the existing value (unless it has been set to something different from the default) as suggested in replies above. It is simpler and works just as well to do
Code:

IFS=whatever_you_need_now
<do stuff using the special $IFS>
unset IFS


tuxdev 05-25-2010 12:39 PM

Also, strongly prefer the while-read loop over IFS tricks. It's far more intuitive and reliable. See 'Why you don't use "for" for this' in http://mywiki.wooledge.org/BashFAQ/001

pixellany 05-25-2010 02:24 PM

ifeatu;

Next time, please do not jump into an old thread like this. It's almost always better to start a new thread, especially when an existing one is 4 years old.

cioraneanu 06-01-2010 02:51 AM

Problems
 
Having problems with reading from file. This is my script.

Code:

nr=0
cat lista_ip.txt | while read a
do

if [ xxx]
then

echo "yyyyyyyy"
nr=$(($nr+1))
echo "NR=$nr"
fi

done

echo "NR=$nr"

It does what i needed, counts the number of lines but after it exits the while no values were saved. My $nr variable is back to 0 in the end. Any ideas?


All times are GMT -5. The time now is 02:03 AM.