PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Finding end of file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Finding end of file

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2006, 19h38   #1
doni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Finding end of file

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

  Réponse avec citation
Vieux 07/11/2006, 19h51   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding end of file

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
  Réponse avec citation
Vieux 07/11/2006, 19h52   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding end of file

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
  Réponse avec citation
Vieux 07/11/2006, 20h01   #4
doni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding end of file

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11553 seconds with 12 queries