Tester une variable texte via read

Résolu/Fermé
michael_scott Messages postés 58 Date d'inscription mercredi 2 avril 2014 Statut Membre Dernière intervention 8 novembre 2018 - 7 nov. 2018 à 21:36
 Utilisateur anonyme - 8 nov. 2018 à 00:42
Bonjour,

Quand je mets "toto" dans le read, j'arrive pas à rentrer dans la boucle dite "toto". Le shell perçoit le "toto" comme 0 et je sors donc via la boucle exit 0.

Je ne comprends pas comment tester une variable texte en shell...

Merci d'avance,

while ((chapitre!=0))
do
echo "Quel chapitre (0 pour sortir)? \c"
read chapitre

if ((chapitre==0)) #alors boucle exit 0
then
exit 0
fi

if ((chapitre==toto)) #si "c" alors boucle toto
then
echo $chapitre
fi
done

1 réponse

Utilisateur anonyme
7 nov. 2018 à 23:51
salut,

while ((chapitre!=0)) 

d'abord,
chapitre
n'étant pas défini avant le début de la boucle, bah, ça ne boucle jamais ! :(
0
michael_scott Messages postés 58 Date d'inscription mercredi 2 avril 2014 Statut Membre Dernière intervention 8 novembre 2018
7 nov. 2018 à 23:53
ha oui pardon, j 'ai omis ça .

mais c'est pas ça mon problème. soit chapitre=1 en première ligne
0
Utilisateur anonyme
7 nov. 2018 à 23:59
tu testes un nombre.
Or, tout ce qui n'est pas un nombre vaut zéro.
cf.
help test
0
michael_scott Messages postés 58 Date d'inscription mercredi 2 avril 2014 Statut Membre Dernière intervention 8 novembre 2018
8 nov. 2018 à 00:01
Merci pour ta réponse.. j'avais bien compris ça ->mais alors comment tester un texte ? enfin, comment je pourrai me débrouiller pour pouvoir faire ce que je souhaite
0
Utilisateur anonyme
8 nov. 2018 à 00:02
que vois-tu quand tu tapes la commande donnée ?
0
michael_scott Messages postés 58 Date d'inscription mercredi 2 avril 2014 Statut Membre Dernière intervention 8 novembre 2018
8 nov. 2018 à 00:03
File operators:

-a FILE True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its `sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read.

FILE1 -nt FILE2 True if file1 is newer than file2 (according to
modification date).

FILE1 -ot FILE2 True if file1 is older than file2.

FILE1 -ef FILE2 True if file1 is a hard link to file2.

String operators:

-z STRING True if string is empty.

-n STRING
STRING True if string is not empty.

STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal.
STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically.

Other operators:

-o OPTION True if the shell option OPTION is enabled.
! EXPR True if expr is false.
EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.

arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.

Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.
0