#!/bin/bash
number_range=$((RANDOM%60))
login(){
read -p '请输入用户名: ’ name
if [ -z “$name” ];then
echo -e “\033[1;31m 昵称不允许为空 \033[0m”
exit 1
fi
}
检查输入是否合法,含有非数字、为空、大于60小于1都属于非法行为
check_number( ){
if [ -z “$number” -o
(
e
c
h
o
"
(echo "
(echo"number"|egrep -o ‘[^0-9]’|wc -l) -gt 1 ];then
echo -e “\033[1;31m 请不要输入非数字字符 \033[0m”
continue
elif [ -z “KaTeX parse error: Undefined control sequence: \0 at position 33: … echo -e "\̲0̲33[1;32m 请不要输入空…number” -lt 1 -o “$number” -gt 60 ];then
echo -e “\033[1;31m 请输入一个1到60之间的数字 \033[0m”
continue
fi
fi
}
提示输入和正确答案大小比对
prompt(){
if [ “
n
u
m
b
e
r
"
−
l
t
"
number" -lt "
number"−lt"number_range” ];then
echo -e “\033[1;34m 你猜的数字太小了 \033[0m”
elif [ “
n
u
m
b
e
r
"
−
g
t
"
number" -gt "
number"−gt"number_range” ];then
echo -e “\033[1;35m 你猜的数字太大了 \033[0m”
elif [ “
n
u
m
b
e
r
"
−
e
q
"
number" -eq "
number"−eq"number_range” ];then
echo -e “\033[1;36m 恭喜你猜对了 \033[0m”
echo -e “\033[1;31m 一共使用了KaTeX parse error: Undefined control sequence: \0 at position 10: count次机会 \̲0̲33[0m" …name $count” >> Game_history
else
read -p “恭喜你 ,猜对了,是否继续此游戏 (y/n):” judge
if [ $judge == “Y” -o $judge == “y” ]
then
break
else
echo -e “\033[34m 欢迎下次继续! \033[0m”
exit
fi
fi
}
主菜单
menu(){
login
count=0
while :
do
read -p '请输入一个1到60之间的数字: ’ number
let count++
check_number
prompt
done
}
menu