Shell Hungman没有在ubuntu上运行

时间:2012-11-29 15:08:22

标签: shell ubuntu

我有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"

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在Ubuntu atm上未经测试,但更改了行

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

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

应该有用。