Opération conditionnelle sur des fichiers...

Résolu
Nico -  
 Nico -
Bonjour,

j'ai un soucis pour finir une routine...
J'ai un fichier qui contient des emplacements de fichiers :
path.dat :
/data/rep1/file
/data/rep2/file
[...]
/data/rep1245/file

J'ai besoin d'un fichier de sortie qui me retourne la 1ere ligne de "file" si le fichier existe et un qui me retourne la 5eme ligne.
Si il n'existe pas, il devra me noter un warning, peut importe quoi.

je travaille sous #!/bin/sh

J'arrive a retourner mes fichiers de sortie mais je me suis rendu compte que mes fichiers d'entrée n'existent pas forcement, et dans ce cas je me retrouve avec moins de lignes dans mes fichiers que dans mon path.dat ...

Et j'arrive pas a intégrer un if test -f $0 [etc] pour résoudre mon pb de fichiers qui n'existent pas...

bref... j'espére avoir été clair...

Merci de me donner un coup de pouce :)

2 réponses

  1. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
     
    Salut,

    while read line
    do
    if [ -e "${line}" ]
    then echo "Le fichier existe"
    else echo "Le fichier n'existe pas"
    fi
    done < fichier_data
    0
  2. Nico
     
    Salut zipe31

    ca ne fonctionne pas : test: argument expected
    0
    1. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
       
      Quel système ?
      Quel shell ?

      Chez moi ça marche ;-\
      $ ls -l
      total 16
      -rw-rw-r-- 1 zipe zipe 26 2011-05-18 08:22 A
      -rw-rw-r-- 1 zipe zipe 26 2011-05-18 08:22 B
      -rw-rw-r-- 1 zipe zipe 25 2011-05-18 08:22 C
      -rw-rw-r-- 1 zipe zipe 10 2011-05-18 11:46 plop
      
      $ cat plop
      A
      F
      B
      D
      C
      
      $ while read line;do if [ -e "${line}" ]; then echo "OK";else echo "Pas bon";fi;done < plop
      OK
      Pas bon
      OK
      Pas bon
      OK
      $
      0
    2. Nico
       
      Je suis sous sun OS 5.8 en sh ...
      0
    3. Nico
       
      ll
      total 40
      drwxrwxr-x 2 j0279088 EP 4096 May 18 12:31 .
      drwxrwxr-x 15 j0279088 EP 16384 May 18 12:29 ..
      -rw-rw-r-- 1 j0279088 EP 0 May 18 12:30 A
      -rw-rw-r-- 1 j0279088 EP 0 May 18 12:30 B
      -rw-rw-r-- 1 j0279088 EP 0 May 18 12:31 C
      -rw-rw-r-- 1 j0279088 EP 10 May 18 12:31 plop


      cat plop
      A
      F
      B
      D
      C


      while read line ; do if [ -e "${line}" ]; then echo "OK"; else echo "Pas Bon"; fi;done < plop
      while: Expression syntax
      0
    4. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
       
      Pourtant "while" est bien une commande de "sh" (man sh) :

             while list; do list; done
             until list; do list; done
      	      The while command continuously executes the do list as  long  as
      	      the  last	 command  in list returns an exit status of zero.  The
      	      until command is identical to the while command, except that the
      	      test  is	negated;  the  do list is executed as long as the last
      	      command in list returns a non-zero exit status.  The exit status
      	      of  the  while and until commands is the exit status of the last
      	      do list command executed, or zero if none was executed

      Jette un oeil à ton "man sh" au cas ou il serait différent ;-(
      0
    5. Nico
       
      while list do list done
      A while command repeatedly executes the while list
      and, if the exit status of the last command in the
      list is zero, executes the do list; otherwise the loop
      0