gpt4 book ai didi

Shell Hungman 没有在 ubuntu 上运行

转载 作者:行者123 更新时间:2023-12-04 18:32:39 25 4
gpt4 key购买 nike

我有 shell 脚本,但是当我在 Ubuntu 上运行它时,出现错误:

arithmetic expression expecting primary.

问题出在主程序中,尤其是 nbLigne 行。 .

这是代码:
#!/bin/sh

MotAuHasard()
{
nbr=$(($RANDOM*$RANDOM%$nbLigne)) #définit la position du mot dans listemot.txt
Mot=$(head -$nbr $FichierMot | tail -n 1) #mot trouvé dans monFichier

#la fonction suivante vérifie si le mot est supérieur à 6 caractères,
#dans le cas contraire, on ré-initialise la fonction

nbcarac1=${#Mot}
if [ $nbcarac1 -lt 6 ]
then
MotAuHasard
fi
}

Affiche()
{
MotJeu=$Mot
nbcarac2=${#cache}
for i in `seq 0 $nbcarac2`;
do MotJeu=${MotJeu//${cache:i:1}/-}
done
}

Decouvre()
{
cache=${cache//$caractere/"_"}
}

testPresence()
{
testPresence=1
for i in `seq 0 $nbcarac1`;
do
if [ "${Mot:i:1}" = "$caractere" ]
then
testPresence=0
fi
done
}

testGagne()
{
testGagne=0
for i in `seq 0 $nbcarac1`;
do
if [ "${MotJeu:i:1}" = "-" ]
then
testGagne=1
fi
done
}

#affichage du pendu selon le nombre de vies restantes

vie_pendu()
{
case $vie in

"0")
echo "_______________"
echo " | | "
echo " | O "
echo " | /|\ "
echo " | | "
echo " | / \ "
echo " | "
echo " | "
echo " | "
echo " |---|";;


"1")
echo "_______________"
echo " | | "
echo " | O "
echo " | /|\ "
echo " | | "
echo " | / "
echo " | "
echo " | "
echo " | "
echo " |---|";;


"2")
echo "_______________"
echo " | | "
echo " | O "
echo " | /|\ "
echo " | | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " |---|";;

"3")
echo "_______________"
echo " | | "
echo " | O "
echo " | /|\ "
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " |---|";;


"4")
echo "_______________"
echo " | | "
echo " | O "
echo " | / "
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " |---|";;


"5")
echo "_______________"
echo " | | "
echo " | O "
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " |---|";;

"6")
echo "_______________"
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " | "
echo " |---|";;

esac
}

#main

FichierMot='listemot.txt'
nbLigne=$(wc -l < $FichierMot) #On récupère le nombre de lignes dans FichierMot
cache="abcdefghijklmnopqrstuvwxyz" #On affiche toutes les lettres disponibles
MotAuHasard #Lancement du jeu

vie=7 #initialisation du nombre de vies
Affiche
while [ $vie -gt 0 ]
do
echo $MotJeu
echo " "
echo "Possibiite de lettres restantes : $cache"
echo " "
echo "Veuillez entrer une lettre: "
echo " "
read caractere
while [ ${#caractere} -gt 1 ]
do
echo "Plus d'une lettre saisie !!"
read caractere
done
Decouvre
testPresence
Affiche
if [ $testPresence -eq 0 ]
then
testGagne
if [ $testGagne -eq 0 ]
then
echo "Felicitation, vous avez gagne !!"
exit
fi
else
vie=$(($vie - 1))
vie_pendu
fi
done

echo "Pas de chance, le mot était : $Mot"

谢谢您的帮助!

最佳答案

未在 Ubuntu atm 上测试,但换行

nbr=$(($RANDOM*$RANDOM%$nbLigne))


nbr=$(echo "$RANDOM*$RANDOM%$nbLigne" | bc)

应该管用。

关于Shell Hungman 没有在 ubuntu 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13628708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com