[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Hvorfor er 0 == 1?



On Mon, Sep 10, 2007 at 00:43:29 +0200, Hans Schou wrote:
> Sært, hvad hvis du tæller sådan:
> FileCount=0
> 
> ls backup* | while read X
> do
>    echo $X
>    echo $FileCount
>    FileCount=$((FileCount+1))
> done
> echo $FileCount
Nu er det korrekte svar blevet sendt ud, men jeg ville lige kommentere
på denne her. Det er nemlig en af de ting man kan bruge oceaner af tid
på at debugge.

Når man i bash laver en 

kommando | while

konstruktion, kommer while løkken til at køre i en subshell. Det betyder
bla. at alle de variable man ændrer inde i while løkken, ikke bliver
opdateret uden for while løkken. I eksemplet ovenfor, vil FileCount
derfor altid være 0 når man kommer ud af løkken.

-- 
Med venlig hilsen
	Klaus S. Madsen


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-10-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *