shell脚本小游戏
时间: 2023-04-28 08:06:03 浏览: 108
这个话题非常广泛,可以有很多种实现方式。但是我可以给出一个简单的例子。
猜数字游戏:
```
#!/bin/bash
# generate a random number between 1 and 100
answer=$(($RANDOM % 100 + 1))
# while the user has not guessed the correct number
while true; do
read -p "Guess a number between 1 and 100: " guess
# check if the guess is less than the answer
if [[ $guess -lt $answer ]]; then
echo "Too low. Try again."
# check if the guess is greater than the answer
elif [[ $guess -gt $answer ]]; then
echo "Too high. Try again."
# if the guess is correct
else
echo "Congratulations! You've guessed the correct number."
break
fi
done
```
这就是一个简单的使用 shell 脚本编写的猜数字小游戏。运行后,程序会随机生成一个数字,然后提示用户猜测,根据猜测的结果告诉用户猜小了或猜大了。用户猜对了,程序就会提示并退出。
阅读全文