Shell - test: fichier ou répertoire ?

Résolu/Fermé
jax54000 Messages postés 44 Date d'inscription mercredi 8 septembre 2004 Statut Membre Dernière intervention 24 mai 2008 - 4 avril 2007 à 22:39
 Je pense donc je suis. - 7 févr. 2014 à 15:07
Hello,

Un petit problème. Je souhaiterias tester si un chemin+fichier est un fichier ou un répertoire.
Effectivement le test -d permet de dire s'il s'agit d'un répertoire. Mais seulement un répertoire existant !!

Je voudrais savoir s'il s'agit d'un répertoire ou d'un fichier, qu'il existe ou non.........

Merciiiiiiiiiiiiiiiiiiiiiiii

A+
A voir également:

5 réponses

mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749
4 avril 2007 à 22:47
Tu as un autre test pour l'existence.
#!/bin/bash
# script plop.sh

if [ ! -e "$1" ]; then
	echo "$1 n'existe pas"
elif [ -f "$1" ]; then
	echo "$1 est un fichier"
elif [ -d "$1" ]; then
	echo "$1 est un repertoire"
fi

Ce qui donne par exemple
(mando@polgara) (~) $ ls -l
total 20
drwx------ 2 mando mando 4096 2007-03-24 12:49 Desktop
-rw-r--r-- 1 mando mando 2351 2007-04-04 22:04 plop.cpp
-rwxr-xr-x 1 mando mando  165 2007-04-04 22:45 plop.sh
drwxr-xr-x 2 mando mando 4096 2007-03-24 13:09 themes
(mando@polgara) (~) $ ./plop.sh plop.cpp
plop.cpp est un fichier
(mando@polgara) (~) $ ./plop.sh Desktop
Desktop est un repertoire
(mando@polgara) (~) $ ./plop.sh dieu
dieu n'existe pas

Bonne chance
54
ubuntu&co
5 janv. 2012 à 13:33
Super c'est ce que je cherchais merci !!

Au passage : oui, forcément que "dieu" n'existe pas. C'est "Dieu" qui existe.
1
mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749
6 janv. 2012 à 01:12
:-)
0
Ou alors, dieu existe car justement Dieu n'existe pas...
0
Une chose est sure, Albert: tu es hors-sujet, et tu ne t'es pas auto-créé.
0