|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I would like to know how can we find the end of file when reading from
a file using shell script. Basically, I want to read 2 lines using a while loop till it reaches the end of a file. Here is how I am reading 2 lines from the result-data file inside a while loop. do { read line1 read line2 } < result-data done Can anyone let me know how can I check for the end of file from a shell script. Thanks, Sekar |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
2006-11-7, 11:38(-08), doni:
> I would like to know how can we find the end of file when reading from > a file using shell script. > > Basically, I want to read 2 lines using a while loop till it reaches > the end of a file. Here is how I am reading 2 lines from the > result-data file inside a while loop. > > do > { > read line1 > read line2 > } < result-data > done [...] while IFS= read -r line1 <&3 && IFS= read -r line2 <&3 do ... done 3< result-data read without "-r" and/or if $IFS contains any blank character has a very special meaning to the shell, you should not do that unless you know what you're doing and why. That's one of the dodgy designs of shells. Using file descriptor 3 instead of 0 s in case you want to get something from the user within the loop. It should be noted that if the end of line is reached after line1 is read but before line2 is read, the loop will stop without processing the $line1 just read. -- Stéphane |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 2006-11-07, doni wrote:
> I would like to know how can we find the end of file when reading from > a file using shell script. > > Basically, I want to read 2 lines using a while loop till it reaches > the end of a file. Here is how I am reading 2 lines from the > result-data file inside a while loop. > > do > { > read line1 > read line2 > } < result-data > done > > Can anyone let me know how can I check for the end of file from a shell > script. while read line1; read line2 do # Do whatever you want here, e.g.: printf "%s %s\n" "$line1" "$line2" done < result-data Depending on the input, you may want to use: while IFS= read -r line1; IFS= read -r line2 -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Thanks Chris and Stephane.
Sekar Chris F.A. Johnson wrote: > On 2006-11-07, doni wrote: > > I would like to know how can we find the end of file when reading from > > a file using shell script. > > > > Basically, I want to read 2 lines using a while loop till it reaches > > the end of a file. Here is how I am reading 2 lines from the > > result-data file inside a while loop. > > > > do > > { > > read line1 > > read line2 > > } < result-data > > done > > > > Can anyone let me know how can I check for the end of file from a shell > > script. > > while read line1; read line2 > do > # Do whatever you want here, e.g.: > printf "%s %s\n" "$line1" "$line2" > done < result-data > > > Depending on the input, you may want to use: > > while IFS= read -r line1; IFS= read -r line2 > > -- > Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell> > Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) > ===== My code in this post, if any, assumes the POSIX locale > ===== and is released under the GNU General Public Licence |
|
![]() |
| Outils de la discussion | |
|
|