2012-11-29 88 views
2

運行我的shell腳本,但是當我運行它在Ubuntu上,有一個錯誤:殼牌Hungman不是在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" 

謝謝您的幫助!

+3

嘗試在第一行更改'/ bin/sh'到'/ bin/bash'。 – choroba

回答

1

在Ubuntu ATM未經檢驗的,但改變線

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

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

應該工作。