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 |
IFS=$'\n'
Cheers, Tink |
Re: BASH: Read entire file line in for loop
Quote:
Code:
#!/bin/bash Code:
#!/bin/bash (without the spaces) |
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! |
Quote:
It think Tinkster means something like this: Code:
#!/bin/bash |
Of course I couldn't see a newline.... yeesh
Brain fart on my part there. Thanks :) |
Thanx!
Yet another example of how a thread from the dead can help with a current problem.
|
I would've done it Tkinsters way:
OLDIFS="$IFS" IFS= for entry in `cat foo`; do [ code here ]; done |
I would've done it Tkinsters way:
Code:
OLDIFS="$IFS" Quote:
|
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 |
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 |
Quote:
Code:
IFS=whatever_you_need_now |
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
|
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. |
Problems
Having problems with reading from file. This is my script.
Code:
nr=0 |
All times are GMT -5. The time now is 02:03 AM. |