[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] [bash] sætte en variabel i en while-løkke



Peter Makholm wrote (07-01-2005 09:33):
Jeg har følgende stump kode:

#!/bin/bash

bar=foo

    find . -type f | while read p ; do
      if /bin/true ; then bar=$p ; fi
    done

echo $bar

Jeg ville forvente at det ville skrive det sidste filnavn ud, men den
skriver bare "foo". Både min betingelse og 'bar' er noget mere
kompliceret i virkeligheden.


Der er et eller andet som sætter et scope, kan jeg komme uden om
dette?

Bash forker en sub-proces til at håndtere while-løkken. Det sker ofte når man bruger en indbygget kommando i en pipeline. Dermed sker tildelingen inde i løkken i en anden proces.


Hvis du vil have den sidste fil der opfylder en betingelse så kan du vel bruge (ikke testet):

bar=$(find . -type f | while read p ; do
  if /bin/true ; then echo $p ; fi
done | tail -1)

--
René Seindal (sslug@sslug)
Seindal Consult






 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 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] *