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



Jeg vil lige opsummere mine forskellige forsøg. Der var nemlig kun én af de 
forslag der kom frem, der virkede.

Med mindre andet er nævnt, så er forsøgene foretaget med 2 directories 
(backup0000 og backup0001).

Test 1
------

for X in backup*
do
  FileCount=$((FileCount+1))
  echo "$FileCount : $X"
done
echo "FileCount=$FileCount"

Gav dette output:

1 : backup0000
2 : backup0001
FileCount=2

Men denne funktion ved vi allerede ikke virker med 0 directories.

Test 2
------

ls backup* | while read X
do
  FileCount=$((FileCount+1))
  echo "$FileCount : $X"
done
echo "FileCount=$FileCount"

Gav dette output:

1 : backup0000:
2 :
3 : backup0001:
FileCount=0

Så den kan vi jo ikke rigtig bruge.

Test 3
------

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

Gav dette output:

1 : backup0000:
2 : backup0001:
FileCount=2

Det var jo lovende. Så jeg prøvede med 0 directories oprettet, hvilket gav 
dette resulat:

FileCount=0

Nu begyndte det at ligne noget, så jeg prøvede med kun ét directory oprettet 
(backup0000), og fik dette output:

FileCount=0

Så den kan heller ikke bruges.

Test 4
------

Til sidst brugte jeg denne metode:

ls -d backup* | wc -l

Og den virker, med 0, 1 og 2 directories! Tak til Bjørn Hartfelt for det 
tip. :-)

Nu skal jeg bare have smidt den værdi i en variabel, så jeg kan teste på den. 
Hvordan gør jeg det?

Jeg prøvede sådan:

FileCount=`ls -d backup* | wc -l`

MEd 1 og 2 directories virker det fint, men med 0 får jeg denne besked:

ls: backup*: No such file or directory

Godt nok bliver FileCount sat korrekt, men jeg vil egentlig gerne af med 
fejlbeskeden.

FileCount=`ls -d backup* 2> /dev/null| wc -l`

Ser ud til at virke.

I mellem tiden er det så endt med at jeg ikke behøver at tælle 
backup-directoryerne, men bare skal teste på om der findes 0 eller 1+ af dem. 
Jeg troede at jeg kunne bruge

if [ -d backup* ]; then

Men det giver mig denne fejlbesked hvis der findes et eller flere directories:

./dirtest.sh: line 3: [: backup0000: binary operator expected

Så jeg er tilbage til at tælle dem, og så bare teste på om der er 0 eller 
flere.

-- 

        |\     _,,,---,,_       Greetings, Jens
 ZZZzz /,`.-'`'    -.  ;-;;,_
      |,4-  ) )-,_. ,\ (  `'-'  sslug@sslug
     '---''(_/--'  `-'\_)
----------------------------------------------------
Ah, yes, divorce, from the Latin word meaning to rip out a man's genitals
through his wallet.
                                                      -- Robin Williams


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