raspbian (pios) - bash - if then - problème avec des conditions multiples

Parce qu'il en faut pour tout le monde, discutez ici d'ADA, de shell et autres Wolfram...

Modérateur : Francois

Répondre
yopso
Messages : 4
Enregistré le : mar. 10 nov. 2020 10:18

raspbian (pios) - bash - if then - problème avec des conditions multiples

Message par yopso » ven. 23 avr. 2021 23:44

Bonjour, je n'arrive pas à régler un problème de conditions multiples avec "if then" dans mon code, je ne sais pas si je dois utiliser "elif" ou si c'est une erreur de syntaxe.

#!/bin/bash
clear
x=150
y=150
mouve="arrêt"
tourne="tout droit"

#robot à l'arrêt (pin12 bcm : 150), direction devant (pin 18 bcm: 150)
gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $x
gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $y

#lancement du streaming
tput cup 1 20;echo "... lancement du streaming ..."
tput cup 2 20;echo "lire avec : cvlc tcp/MJPEG://192.168.0.12:1234"
printf "$(raspivid -n -t 0 -w 640 -h 480 -vf -hf -cd MJPEG -l -o tcp://0.0.0.0:1234)" &
#lire coté client avec : "cvlc tcp/MJPEG://192.168.0.12:1234" (MJPEG en majuscule)
sleep 2

tput cup 3 20;printf "qqq ou Ctrl+c pour arréter"
tput cup 5 2;echo $mouve
tput cup 6 2;echo "position de la roue :" $tourne

while true
do
read -rsn3 -d '' TOUCHE
case ${TOUCHE:2} in
A) TOUCHE="↑" ;;
B) TOUCHE="↓" ;;
C) TOUCHE="→" ;;
D) TOUCHE="←" ;;
q) TOUCHE="q" ;;
esac

if [ $TOUCHE = "↑" ];then
if [ $y=50 ];then y=128;mouve=arrière lente;fi
if [ $y=128 ];then y=150;mouve=arrêt;fi
if [ $y=150 ];then y=170;mouve=avance lente;fi
if [ $y=170 ];then y=240;mouve=avance rapide;fi
gpio -g mode 18 in;gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $y
fi

if [ $TOUCHE = "↓" ];then
if [ $y=240 ];then y=170;mouve=avance lente;fi
if [ $y=170 ];then y=150;mouve=arrêt;fi
if [ $y=150 ];then y=128;mouve=arrière lente;fi
if [ $y=128 ];then y=50;mouve=arrière rapide;fi
gpio -g mode 18 in;gpio -g mode 12 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 12 $y
fi

if [ $TOUCHE = "→" ];then
if [ $x=52 ];then x=105;tourne=tout droit;fi
if [ $x=105 ];then x=220;tourne=à droite;fi
gpio -g mode 12 in;gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $x
fi

if [ $TOUCHE = "←" ];then
if [ x=220 ];then x=105;tourne=tout droit;fi
if [ x=105 ];then x=52;tourne=à gauche;fi
gpio -g mode 12 in;gpio -g mode 18 pwm;gpio pwm-ms;gpio pwmc 192;gpio pwmr 2000;gpio -g pwm 18 $x
fi
if [ $TOUCHE = "q" ];then killall raspivid;gpio -g mode 12 in; gpio -g mode 18 in;exit
fi
clear
tput cup 3 20;printf "qqq ou Ctrl+c pour arréter"
tput cup 5 2;echo $mouve
tput cup 6 2;echo "position de la roue :" $tourne
#tput cup 8 5
#echo "La flèche est \"${TOUCHE}\""
done



Merci pour votre aide.

piper
Raspinaute
Messages : 641
Enregistré le : sam. 5 juin 2021 18:57

Re: raspbian (pios) - bash - if then - problème avec des conditions multiples

Message par piper » sam. 5 juin 2021 21:30

Bonjour, tu ne dis pas où tu as une erreur de syntaxe.
J'en vois une ici :
if [ x=105 ];then x=52;tourne=à gauche;fi (tourne vaut à et fait gauche ??, ce serait pas plutôt tourne="à gauche" ??)




Mais concernant le if / else if en bash ça s'écrit comme ceci :
if
elif
fi

(il n'y a qu'un seul fi)

D'autre part, en bash, il n'y a que moi ou ??? mois j'utilise des == et pas des = pour les tests d'égalité sur des chaînes de caractères mais il est bien possible que le = simple fonctionne.

enfin, en bash, lorsque je traite une chaîne, je l'encaspule car un script qui fait ceci :
if [ $a == "1" ]; then
.........
fi
va planter si a vaut une chaîne vide car l'interpréteur va comprendre if [ == "1" ]; then

Moi je fais alors
if [ ${a} == "1" ]; then
.........
fi
ou
if [ "$a" == "1" ]; then
.........
fi

Répondre

Retourner vers « Autres langages »