Comment comparer un script shell?Comment comparer un script shell?
Ou, pourquoi le script suivant n'imprime rien?
x=1
if[ $x = 1 ] then echo "ok" else echo "no" fi
Comment comparer un script shell?Comment comparer un script shell?
Ou, pourquoi le script suivant n'imprime rien?
x=1
if[ $x = 1 ] then echo "ok" else echo "no" fi
Avec des chiffres, utilisez -eq
, -ne
, ... pour égale, différent, ...
x=1
if [ $x -eq 1 ]
then
echo "ok"
else
echo "no"
fi
Et pour les autres, utilisez ==
pas =
.
En utilisant -eq et le; après] ça donne 'bash: erreur de syntaxe près du jeton inattendu \ 'alors'' –
Par changin juste mon' == 'à' -eq' il n'imprime toujours rien ici .. –
Oui, en fait vous devez laisser un "espace" "avant et après chaque" ["... Je viens d'éditer un code valide. –
Cela dépend de la langue. Avec bash, vous pouvez utiliser l'opérateur ==
. Otherwize vous pouvez utiliser -eq
-lt
-gt
pour égal, lowererthan, greaterthan.
$ x=1
$ if [ "$x" == "2" ]; then echo "yes"; else echo "no"; fi
no
Modifier: espaces ajoutés ARROUND ==
et testés avec 2.
Vous avez besoin d'espaces autour du "==" - essayez votre commande avec '[" $ x "==" 2 "]' et vous verrez que c'est vrai aussi. Vous avez besoin de '[" $ x "=" 1 "]' ou '[$ x -eq 1]', ou avec bash '[[$ x == 1]]' ou '((x == 1))' –
merci pour cette correction, problème de retrait manuel .. Mais [] fonctionne aussi. – Aif
solution court avec raccourci AND et OR:
x=1
(($x == 1)) && echo "ok" || echo "no"
première ligne: ** $ x = 1 ** – Hossein
dont vous avez besoin une ; après] et un espace après si: 'if [$ x = 1]; puis écho "ok"; d'autre écho "non"; – Sergio
@Sergio il donne 'bash: erreur de syntaxe près de jeton inattendu \ 'alors'' –