利用Linux系统中的时间秒1-59来进行猜数字游戏,详细代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/bin/bash
#author wangning
#date 2017-7-15
#qq 1198143315
#Email 1198143315@qq.com
####################################define color##########################
red=
"\033[31m"
green=
"\033[32m"
yellow=
"\033[33m"
color=
"\033[0m"
######################check number if conform 1-59########################
check_number(){
expr
$num1 + 1 &>
/dev/null
if
[ $? -
ne
0 ];
then
echo
-e
"please input $red number $color"
continue
1
elif
[ -z $num1 ];
then
echo
-e
"$yellow Can't be empty $color"
continue
2
elif
[ $num1 -gt 59 ];
then
echo
-e
"$green Can't great than 59 $color"
continue
3
fi
}
################################guess number ##############################
guess_number(){
if
[ $num1 -gt $num ];
then
echo
-e
" $yellow too big $color"
elif
[ $num1 -
eq
$num ];
then
echo
-e
"$green congratulation, you have guessed ! $color"
elif
[ $num1 -lt $num ];
then
echo
-e
"$green too small $color"
fi
}
###############################while circulation################################
main(){
while
true
do
num=$(
date
+%S)
read
-p
"please input number 1-59:"
num1
check_number
guess_number
done
}
main
|
本文转自 茁壮的小草 51CTO博客,原文链接:http://blog.51cto.com/wn2100/1947812,如需转载请自行联系原作者