[Code et Script][bash] test et arguments

test et arguments [Code et Script][bash] - Codes et scripts - Linux et OS Alternatifs

Marsh Posté le 31-08-2017 à 21:57:58    

Bonjour,
Je voudrais vérifier quelque condition avant d'exécuter une commande et retourner le code de retour de la commande.
 
Voici mon code :
 

+---------------------------------------------------------------------------[X]+
|#!/bin/bash                                                                   |
|# hello                                                                       |
|echo -e "\033[035mhello \033[032m$USER \033[0m"                               |
|if [ $# > 2 ]; then                                                           |
|    if [ -n $1 ]; then                                                        |
|        if [ $2=="test" ]; then                                               |
|            /bin/echo -e "[$0] test $2";                                      |
|            test $2;                                                          |
|            if [ $? -eq 0 ]; then                                             |
|                exit 0;                                                       |
|            else                                                              |
|                echo $?;                                                      |
|                exit $?;                                                      |
|            fi;                                                               |
|        fi;                                                                   |
|    fi;                                                                       |
|fi;                                                                           |
|exit 0                                                                        |
+------------------------------------------------------------------------------+


 
Voici le résultat de l'appel suivant : ./hello
 

Citation :

hello sens  
[./hello] test  
1


 
dois y a voir une erreur de syntaxe ou un truc à configurer avant, j'en sais rien mais c'est pas probant.
 
Le premier test est censé tester si le nombre d'argument est supérieur à 2.
 
Un coup de main sera bien venu, s'il vous plaît. Merci.


Message édité par Profil supprimé le 31-08-2017 à 21:58:54
Reply

Marsh Posté le 31-08-2017 à 21:57:58   

Reply

Marsh Posté le 31-08-2017 à 23:30:03    

j'ai corriger un peu enfin...
 

Code :
  1. #!/bin/bash
  2. # hello
  3. echo -e "\033[035mhello \033[032m$USER \033[0m";
  4.  
  5. if [ $# > 2 ]; then
  6.     if [ -n "$1" ]; then        
  7.        if [ "$1" = "test" ]; then
  8.            /bin/echo -e "[$0] test $2";
  9.            test $2;
  10.            if [ $? -eq 0 ]; then
  11.                exit 0;
  12.            else
  13.                exit $?;
  14.            fi;
  15.        else
  16.            exit 125;
  17.        fi;
  18.     else
  19.         exit 126;
  20.    fi;
  21.  
  22. else
  23.    exit 127;
  24. fi;

Reply

Marsh Posté le 01-09-2017 à 20:56:02    

Citation :

j'ai corriger un peu enfin...


et ?
 
attention, > n'est pas un opérateur arithmétique.  
le test est donc toujours vrai, et tu as désormais un fichier nommé 2. :/
 
et $2 doit aussi être entre guillemets, au cas où il serait composé de plusieurs mots.

Reply

Marsh Posté le 01-09-2017 à 21:29:45    

soit on fait $var soit $"var" ou plus ${var}  
 
> a été expliqué par watael donc  ;  il a raison ;)  
 
les ";" sur tes fi sont facultatifs  
 
 
par contre oui ; grand OUI ; tu as indenté et ça c'est bien !!!!!  
 
 
 

Reply

Marsh Posté le 01-09-2017 à 23:33:16    

${var} n'évitera pas l'échec du test, il n'y a que les guillemets qui permettront à test de considérer la chaîne testée comme une seule "entité" :

Code :
  1. $ var="foo bar baz"
  2. $ test $var
  3. bash: test: bar : opérateur binaire attendu
  4. $ test ${var}
  5. bash: test: bar : opérateur binaire attendu
  6. $ test "$var"
  7. $ echo $?
  8. 0

Reply

Marsh Posté le 01-09-2017 à 23:35:38    

je ne prétend pas ça :) attention ! :)  
 

Reply

Marsh Posté le 02-09-2017 à 12:40:05    

Merci pour vos réponse.
 
Comment je fais un teste de comparaison arithmétique ?
 
J'ai pourtant lu le man de bash ils disent bien d'employer "< | > |<= | >=" :/


Message édité par Profil supprimé le 02-09-2017 à 12:40:45
Reply

Marsh Posté le 02-09-2017 à 14:30:20    

Salut,
 

Citation :

J'ai pourtant lu le man de bash ils disent bien d'employer "< | > |<= | >=" :/


 
T'as du louper cette partie :
 

Citation :

      arg1 OP arg2
              OP  is  one  of  -eq,  -ne,  -lt, -le, -gt, or -ge.  These arithmetic binary operators
              return true if arg1 is equal to, not equal to, less  than,  less  than  or  equal  to,
              greater  than,  or  greater than or equal to arg2, respectively.  Arg1 and arg2 may be
              positive or negative integers.


 
 ;)  


---------------
$ man woman
Reply

Marsh Posté le 02-09-2017 à 14:45:27    

Merci, mais non je l'ai pas loupé, le l'utilise d'ailleurs...
 
 
Man un peu plus haute dans la man de bash.
 

Citation :

|ARITHMETIC EVALUATION                                                                                                                                                                                                                                                   |
|       The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion).  Evaluation is done in fixed-width integers with no check for overflow, though division  by  0  is     |
|       trapped and flagged as an error.  The operators and their precedence, associativity, and values are the same as in the C language.  The following list of operators is grouped into levels of equal-precedence operators.  The levels are listed in order of     |
|       decreasing precedence.                                                                                                                                                                                                                                           |
|                                                                                                                                                                                                                                                                        |
|       id++ id--                                                                                                                                                                                                                                                        |
|              variable post-increment and post-decrement                                                                                                                                                                                                                |
|       ++id --id                                                                                                                                                                                                                                                        |
|              variable pre-increment and pre-decrement                                                                                                                                                                                                                  |
|       - +    unary minus and plus                                                                                                                                                                                                                                      |
|       ! ~    logical and bitwise negation                                                                                                                                                                                                                              |
|       **     exponentiation                                                                                                                                                                                                                                            |
|       * / %  multiplication, division, remainder                                                                                                                                                                                                                       |
|       + -    addition, subtraction                                                                                                                                                                                                                                     |
|       << >>  left and right bitwise shifts                                                                                                                                                                                                                             |
|       <= >= < >                                                                                                                                                                                                                                                        |
|              comparison                                                                                                                                                                                                                                                |


Message édité par Profil supprimé le 02-09-2017 à 14:46:30
Reply

Marsh Posté le 02-09-2017 à 15:05:01    

Donc, change ton

Code :
  1. if [ $# > 2 ]; then

par :

Code :
  1. if [ $# -gt 2 ]; then


---------------
$ man woman
Reply

Marsh Posté le 02-09-2017 à 15:05:01   

Reply

Marsh Posté le 02-09-2017 à 15:36:06    

Citation :

J'ai pourtant lu le man de bash ils disent bien d'employer "< | > |<= | >="

oui, dans une Évaluation arithmétique, mais pas dans un test.
 
une évaluation arithmétique est initiée par des parenthèses doubles :

Code :
  1. if (( $# > 2 )); then


 
 
et quand tu voudras faire une comparaison lexicale dans un test, en utilisant ces opérateurs donc, il faudra les protéger pour inhiber la redirection.

Code :
  1. if test "$a" \> "$b"; then


Message édité par Profil supprimé le 02-09-2017 à 15:45:06
Reply

Marsh Posté le 02-09-2017 à 15:38:43    

Thank you !

Reply

Marsh Posté le 29-01-2018 à 23:39:41    

Et attention également à l'utilisation de $?.
 
En effet, quand tu fais par exemple :  
echo $?
exit $?
 
Lors de la 2e instruction, exit $? utilisera le code retour de l'instruction "echo $?" soit 0 logiquement.
 
En général, il est préférable de "capturer" un code retour dans une variable pour s'en servir par la suite. C'est trompeur ;)


---------------
Burn the museum, wipe your ass with the Mona Lisa, this way, at least, God will know your name.
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed