bash脚本中的字符串评估

时间:2014-12-10 12:21:20

标签: bash shell scripting

#!/bin/bash
#DICE GAME

dir=$HOME/Desktop/SCHOOL/COMPUTERGEBRUIK/werkcolleges/oefeningenreeks2/hulpbestanden/bloemblaadjes.txt
echo $dir

MAX=$(wc -l < $dir)  # amount of lines in bloemblaadjes.txt
echo "$MAX"
num=$(( RANDOM % $MAX + 1)) # Random number between 1 and max
echo $num
RESULT=$(head -n $num $dir | tail -n 1 | cut -d' ' -f1,2,3,4,5) 
# first five numbers of       selected line
CORRECT=$(head -n $num $dir | tail -n 1 | cut -d' ' -f6) 
# last number of selected line         =       correct answer
echo $CORRECT
echo "Result:  $RESULT" # showing the person who plays the game what the result are
echo -e "Give the answer: \c" # player needs to give the correct answer
read ANS

if [ "$ANS" = "$CORRECT" ]
then
echo "COOORRECT!"
else
echo "the correct answer is $CORRECT, better luck next time!"
fi

在这个程序中,我从文件中选择一个随机行。这些行包含6个数字,前5个数字是掷骰子的结果,另一个数字是答案。我的if语句似乎有问题:当我在终端中运行脚本并给出正确答案时,它总是显示&#34;下次运气好了#34;所以不显示回声的第一部分。有谁可以提供帮助?

0 个答案:

没有答案