Je commets cette erreur en lançant un petit script sur le rock, le papier et les ciseaux. Voici mon code, et il échoue sur la deuxième déclaration elif, pourriez-vous comprendre pourquoi ??
function compare () { if [ "$userChoice" == "$computerChoice" ] then echo "Empate!" elif [ "$userChoice" == "piedra" ] if [ "$computerChoice" == "tijeras" ] then echo "La piedra gana" else echo "El papel gana" fi elif [ "$userChoice" == "papel" ] if [ "$computerChoice" == "piedra" ] then echo "El papel gana" else echo "Las tijeras ganan" fi fi } compare $userChoice $computerChoice
Le message d’erreur est le suivant:
myscript: line 10: syntax error near unexpected token `elif' myscript: line 10: ` elif [ "$userChoice" == "papel" ]'
vous avez oublié quelques “alors” déclarations. Une bonne manière de se rappeler est d’append la phrase “then” à la même ligne que le “if”. Exemple ci-dessous.
function compare () { if [ "$userChoice" == "$computerChoice" ] ; then echo "Empate!" elif [ "$userChoice" == "piedra" ] ; then if [ "$computerChoice" == "tijeras" ] ; then echo "La piedra gana" else echo "El papel gana" fi elif [ "$userChoice" == "papel" ] ; then if [ "$computerChoice" == "piedra" ] ; then echo "El papel gana" else echo "Las tijeras ganan" fi fi } compare $userChoice $computerChoice
Vous avez donc besoin après l’élif aussi.