# \[Bash\]Tester si un argument est un dossier

**URL:** <https://www.debian-fr.org/t/bash-tester-si-un-argument-est-un-dossier/28453>\
**Category:** Programmation\
**Created:** [Février 20, 2016, 11:20 UTC](https://www.debian-fr.org/t/bash-tester-si-un-argument-est-un-dossier/28453 "2016-02-20T23:20:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glorf](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/d6d6ee/32.png) [@Glorf](https://www.debian-fr.org/u/Glorf)\
**Post date:** [Février 20, 2016, 11:20 UTC](https://www.debian-fr.org/t/bash-tester-si-un-argument-est-un-dossier/28453/1 "2016-02-20T23:20:22Z")

</div>

Bonjour,  
j’ai un petit script de rangement qui prends 2 arguments : le premier indique le répertoire de rangement, et le deuxième (optionnel), celui où iront les fichiers rangés.  
Le code :

`if [-e $1] && [-d $1]
then
	echo "Rangement du répertoire $1"
else
	echo "Syntaxe : rangement.sh dossier_a_ranger [dossier_ou_ranger]"
	exit
fi
#Si on précise $2, on enverra là bas, sinon ce sera dans $1
if [-e $2] && [-d $2] 
then
	destination=$2
else
	destination=$1
fi
echo $destination`  
Le premier if marche bien, si c’est pas un répertoire, il envoie la syntaxe.  
Mais mystérieusement, le deuxième if affecte à destination la valeur de $2, toujours, même quand je ne renseigne pas celui ci.  
Une idée ?  
Glorf

---

<div class="post-metadata">

**Author:** ![Totor](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/58f4c7/32.png) [@Totor](https://www.debian-fr.org/u/Totor)\
**Post date:** [Février 20, 2016, 11:20 UTC](https://www.debian-fr.org/t/bash-tester-si-un-argument-est-un-dossier/28453/2 "2016-02-20T23:20:23Z")

</div>

Bonjour,

Bash a une builtin «&nbsp;test&nbsp;» («&nbsp;[&nbsp;») qui diffère de l’instruction test.

Voilà comment elle évalue les expressions qui composent ses arguments :

> [@man bash](#):
>
> test and [ evaluate conditional expressions using a set of rules based on the number of arguments.
> 
> ```
> 0 arguments
> The expression is false.
> 
> ```
> 
> 1 argument  
> The expression is true if and only if the argument is not null.  
> 2 arguments  
> If the first argument is !, the expression is true if and only if the second argument is null. If the first argument is one of the  
> unary conditional operators listed above under CONDITIONAL EXPRESSIONS, the expression is true if the unary test is true. If the  
> first argument is not a valid unary conditional operator, the expression is false.  
> 3 arguments  
> If the second argument is one of the binary conditional operators listed above under CONDITIONAL EXPRESSIONS, the result of the  
> expression is the result of the binary test using the first and third arguments as operands. The -a and -o operators are considered  
> binary operators when there are three arguments. If the first argument is !, the value is the negation of the two-argument test  
> using the second and third arguments. If the first argument is exactly ( and the third argument is exactly ), the result is the  
> one-argument test of the second argument. Otherwise, the expression is false.  
> 4 arguments  
> If the first argument is !, the result is the negation of the three-argument expression composed of the remaining arguments. Other‐  
> wise, the expression is parsed and evaluated according to precedence using the rules listed above.  
> 5 or more arguments  
> The expression is parsed and evaluated according to precedence using the rules listed above.

Il faut comprendre que if [-e $2] && [-d $2] se traduit pas if [-e] && [-d] après l’expansion des variables.

Du coup, les deux "test n’ont qu’un seul argument (-e pour l’un et -d pour l’autre)  
Ces arguments sont considérés comme étant des chaines de caractères et non comme des opérateurs conditionnels. Ainsi, la règle «&nbsp;1 argument&nbsp;» est appliquée.

Pour résoudre ton problème, il faut encadrer ta variable $2 :  
if [-e «&nbsp;$2&nbsp;»] && [-d «&nbsp;$2&nbsp;»]

Tu conviendras qu’il faut faire la même chose pour if [-e «&nbsp;$1&nbsp;»] && [-d «&nbsp;$1&nbsp;»] !

---

<div class="post-metadata">

**Author:** ![Glorf](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/d6d6ee/32.png) [@Glorf](https://www.debian-fr.org/u/Glorf)\
**Post date:** [Février 20, 2016, 11:20 UTC](https://www.debian-fr.org/t/bash-tester-si-un-argument-est-un-dossier/28453/3 "2016-02-20T23:20:23Z")

</div>

Ah oui ça marche beaucoup mieux merci !  
Je passe le sujet en résolu
