[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?



Michael Rasmussen wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 10 Sep 2007 00:36:08 +0200
Jens Bang <sslug@sslug> wrote:

Hvorfor er FileCount lig med 1 både når der er ét backup*-directory
og når der ikke er noget backup*-directory?


Kunne det ikke skyldes, at hvis du ikke videresender stderr
til /dev/null, så vil fejlmeddelelser blive medtalt?
ls backup* giver denne fejlmeddelelse, hvis intet matcher:
ls: backup*: No such file or directory

Det virker sådan her:
#!/bin/sh
FileCount=0

for X in $(ls backup* 2> /dev/null)
do
    FileCount=$((FileCount+1))
done

Det virker også uden at videresende stderr til /dev/null
Men hvis der skal pipes til et statistik-program er det bedst et være fri for "uvedkommende" tekst.


Problemet med

FileCount=0
for X in backup*
do
  FileCount=$((FileCount+1))
done

er, at der ikke er betinget adgang til for-løkken. Først ved løkkens slut bliver betingelsen sat til false; og så er tælleren jo talt op.
Ved at bruge ls bliver adgangsbetingelsen false i og med at udtrykket (ls backup*) er false, hvis ingen filer matcher.


Vær iøvrigt opmærksom på, at ls medtæller både katalognavne og indhold
i kataloger. Har du f.eks følgende:

backup0001
    |
    ---- file1
    |
    ---- file2

giver ovenstående 3 som resultat.


Brug ls -d backup*, så viser ls kun de katalogindgange der matcher, går altså ikke ned subkataloger.



-- Med venlig hilsen

Jørgen Heesche
mailto:sslug@sslug


 
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] *