{"id":2621,"date":"2025-07-20T16:17:26","date_gmt":"2025-07-20T15:17:26","guid":{"rendered":"https:\/\/workboot.fr\/ciela\/?page_id=2621"},"modified":"2025-07-20T16:25:58","modified_gmt":"2025-07-20T15:25:58","slug":"les-tests-en-bash-shell","status":"publish","type":"page","link":"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/","title":{"rendered":"les tests en bash shell"},"content":{"rendered":"\n<nav aria-label=\"Table des mati\u00e8res\" class=\"wp-block-table-of-contents\"><ol><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#types-de-tests\">Types de tests<\/a><ol><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#tests-de-fichiers\">Tests de fichiers<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#tests-de-chaines-de-caracteres\">Tests de cha\u00eenes de caract\u00e8res<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#tests-numeriques\">Tests num\u00e9riques<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#combinaisons-logiques\">Combinaisons logiques<\/a><ol><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#et-logique\">ET logique : &amp;&amp;<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#ou-logique\">OU logique : ||<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#negation\">N\u00e9gation : !<\/a><\/li><\/ol><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/workboot.fr\/ciela\/les-tests-en-bash-shell\/#exemple-complet\">\ud83d\udd38 Exemple complet<\/a><\/li><\/ol><\/li><\/ol><\/nav>\n\n\n\n<p class=\"wp-block-paragraph\">Les <strong>tests en Bash shell<\/strong> permettent de faire des v\u00e9rifications logiques ou arithm\u00e9tiques (fichiers, cha\u00eenes, entiers, etc.). Voici un r\u00e9sum\u00e9 des diff\u00e9rents types de tests que tu peux effectuer avec la commande <code>test<\/code>, ou sa forme \u00e9quivalente entre crochets <code>[ ... ]<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#91; CONDITION ]; then\n    # commandes si vrai\nelse\n    # commandes si faux\nfi<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Important : toujours <strong>laisser des espaces autour des crochets <code>[<\/code> et <code>]<\/code><\/strong>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"types-de-tests\">Types de tests<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tests-de-fichiers\"><strong>Tests de fichiers<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Test<\/th><th>Signification<\/th><\/tr><\/thead><tbody><tr><td><code>[ -e fichier ]<\/code><\/td><td>Le fichier existe (quelque soit son type)<\/td><\/tr><tr><td><code>[ -f fichier ]<\/code><\/td><td>Le fichier est un fichier ordinaire<\/td><\/tr><tr><td><code>[ -d dossier ]<\/code><\/td><td>C\u2019est un dossier<\/td><\/tr><tr><td><code>[ -r fichier ]<\/code><\/td><td>Le fichier est lisible<\/td><\/tr><tr><td><code>[ -w fichier ]<\/code><\/td><td>Le fichier est modifiable<\/td><\/tr><tr><td><code>[ -x fichier ]<\/code><\/td><td>Le fichier est ex\u00e9cutable<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tests-de-chaines-de-caracteres\">Tests de cha\u00eenes de caract\u00e8res<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Test<\/th><th>Signification<\/th><\/tr><\/thead><tbody><tr><td><code>[ \"$str1\" = \"$str2\" ]<\/code><\/td><td>Les cha\u00eenes sont \u00e9gales<\/td><\/tr><tr><td><code>[ \"$str1\" != \"$str2\" ]<\/code><\/td><td>Les cha\u00eenes sont diff\u00e9rentes<\/td><\/tr><tr><td><code>[ -z \"$str\" ]<\/code><\/td><td>La cha\u00eene est vide<\/td><\/tr><tr><td><code>[ -n \"$str\" ]<\/code><\/td><td>La cha\u00eene n\u2019est pas vide<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tests-numeriques\">Tests num\u00e9riques<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Test<\/th><th>Signification<\/th><\/tr><\/thead><tbody><tr><td><code>[ \"$a\" -eq \"$b\" ]<\/code><\/td><td><code>$a<\/code> est \u00e9gal \u00e0 <code>$b<\/code><\/td><\/tr><tr><td><code>[ \"$a\" -ne \"$b\" ]<\/code><\/td><td><code>$a<\/code> est diff\u00e9rent de <code>$b<\/code><\/td><\/tr><tr><td><code>[ \"$a\" -lt \"$b\" ]<\/code><\/td><td><code>$a<\/code> est inf\u00e9rieur \u00e0 <code>$b<\/code><\/td><\/tr><tr><td><code>[ \"$a\" -le \"$b\" ]<\/code><\/td><td><code>$a<\/code> est inf\u00e9rieur ou \u00e9gal \u00e0 <code>$b<\/code><\/td><\/tr><tr><td><code>[ \"$a\" -gt \"$b\" ]<\/code><\/td><td><code>$a<\/code> est sup\u00e9rieur \u00e0 <code>$b<\/code><\/td><\/tr><tr><td><code>[ \"$a\" -ge \"$b\" ]<\/code><\/td><td><code>$a<\/code> est sup\u00e9rieur ou \u00e9gal \u00e0 <code>$b<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"combinaisons-logiques\">Combinaisons logiques<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"et-logique\"><strong>ET logique<\/strong> : <code>&amp;&amp;<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">if [ $a -gt 0 ] &amp;&amp; [ $a -lt 10 ]; then<br>    echo \"Entre 1 et 9\"<br>fi<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ou-logique\"><strong>OU logique<\/strong> : <code>||<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">if [ $a -lt 0 ] || [ $a -gt 100 ]; then<br>    echo \"Hors de l'intervalle\"<br>fi<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"negation\"><strong>N\u00e9gation<\/strong> : <code>!<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">if [ ! -f fichier.txt ]; then<br>    echo \"Fichier absent\"<br>fi<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"exemple-complet\">\ud83d\udd38 Exemple complet<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash<br><br>fichier=\"mon_script.sh\"<br><br>if [ -e \"$fichier\" ]; then<br>    echo \"Le fichier existe.\"<br>    <br>    if [ -x \"$fichier\" ]; then<br>        echo \"Le fichier est ex\u00e9cutable.\"<br>    else<br>        echo \"Le fichier n'est pas ex\u00e9cutable.\"<br>    fi<br>else<br>    echo \"Le fichier n'existe pas.\"<br>fi<code><br><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Les tests en Bash shell permettent de faire des v\u00e9rifications logiques ou arithm\u00e9tiques (fichiers, cha\u00eenes, entiers, etc.). Voici un r\u00e9sum\u00e9 des diff\u00e9rents types de tests que tu peux effectuer avec la commande test, ou sa forme \u00e9quivalente entre crochets [ &#8230; ]. Important : toujours laisser des espaces autour des crochets [ et ]. Types [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-2621","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"https:\/\/workboot.fr\/ciela\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Les tests en Bash shell permettent de faire des v\u00e9rifications logiques ou arithm\u00e9tiques (fichiers, cha\u00eenes, entiers, etc.). Voici un r\u00e9sum\u00e9 des diff\u00e9rents types de tests que tu peux effectuer avec la commande test, ou sa forme \u00e9quivalente entre crochets [ ... ]. Important : toujours laisser des espaces autour des crochets [ et ]. Types\u2026","_links":{"self":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages\/2621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/comments?post=2621"}],"version-history":[{"count":3,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages\/2621\/revisions"}],"predecessor-version":[{"id":2626,"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/pages\/2621\/revisions\/2626"}],"wp:attachment":[{"href":"https:\/\/workboot.fr\/ciela\/wp-json\/wp\/v2\/media?parent=2621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}