1 #!/bin/bashcaishu.sh
2 PRICE=$(expr $RANDOM % 1000)
3 #PRICE=100
4 TIMES=10
5 USETIMES=0
6 LEFT=0
7 RIGHT=999
8 echo "商品的实际价格为0-999,猜猜看是多少?你最多有10次机会哦!"
9 while [[ $TIMES -gt 0 ]] # [ $TIMES -gt 0 ]
10 do
11 read -p "请输入您猜测的价格数字(为一整数): " INT
12 let TIMES--
13 let USETIMES++
14 if [ $INT -eq $PRICE ];then
15 echo "恭喜您答对了,实际价格是$PRICE"
16 echo "您总共猜测了$USETIMES次"
17 #TIMES=0
18 break
19 elif [ $INT -gt $PRICE ];then
20 RIGHT=$INT
21 echo "猜高了!您还有$TIMES次机会,提示:价格位于$LEFT到$RIGHT之间。"
22 else
23 LEFT=$INT
24 echo "猜低了!您还有$TIMES次机会,提示:价格位于$LEFT到$RIGHT之间。"
25 fi
26 done
类C语言风格的bash shell脚本
#!/bin/bashfor_like_c.sh
for ((integer=1;integer<=5;integer++))
do
echo "$integer"
done
echo "-------------------------"
sum=0
for ((i=1;i<=100;i=i+2))
do
let "sum+=i"
done
echo "sum=$sum"
echo "------------------------"
LIMIT=5
for ((a = 1, b = 5;a <= LIMIT;a++,b--))
do
let "temp=a-b"
echo "$a-$b=$temp"
done
AWK
1 BEGIN{
2 for(n=1;n<=5;n++)
3 print "2^"n,2**n
4 }
5 ###awk -f for.awk
1 [zengqingfu@zengqingfu ~]$ vim shell_scripts.sh
2
3 [ -f aaaaa ]&&echo "aaaaa is a file" ||echo "aaaaa is a directory"
4 [ 10 -gt 10 ] && echo "10 is greater than 10" || echo "10 is not greater than 10"
5 echo $?
6 [ 10 -eq 10 ]
7 echo $?
8 FreeMem=`free -m | grep cache: |awk '{print $3}'`
9 echo $FreeMem
最后来个难一点的:用于回答问卷并判断结果的
1 [zengqingfu@zengqingfu shell_script]$ cat quiz
2 #!/bin/bash
3 #remove the # on the following line to turn on debugging
4 #set -o xtrace
5
6 #==========================
7 function initialize (){
8 trap 'summarize;exit 0' INT #Handle user interrupts
9 num_ques=0 #Number of questions asked so far
10 num_correct=0 #Number answered correctly so far
11 first_time=true #true until first question is asked
12 cd ${QUIZDIR:=~/quiz} || exit 2
13 }
14
15 #=========================
16 function choose_subj (){
17 subjects=($(ls))
18 PS3="Choose a subject for the quiz from the preceding list:"
19 select Subject in ${subjects[*]};do
20 if [[ -z "$Subject" ]];then
21 echo "No subject chosen. Bye." >&2
22 exit 1
23 fi
24 echo $Subject
25 return 0
26 done
27 }
28
29 #===========================
30 function exchange (){
31 temp_value=${questions[$1]}
32 questions[$1]=${questions[$2]}
33 questions[$2]=$temp_value
34 }
35
36 #===========================
37 function scramble (){
38 declare -i index quescount
39 questions=($(ls))
40 quescount=${#questions[*]} #Number of elements
41 ((index=quescount-1))
42 while [[ $index > 0 ]];do
43 ((target=RANDOM % index))
44 exchange $target $index
45 ((index -= 1))
46 done
47 }
48
49 #==========================
50 function ask (){
51 exec 3<$1
52 read -u3 ques || exit 2
53 read -u3 num_opts || exit 2
54
55 index=0
56 choices=()
57 while (( index < num_opts )) ; do
58 read -u3 next_choice || exit 2
59 choices=("${choices[@]}" "$next_choice")
60 ((index += 1 ))
61 done
62 read -u3 correct_answer || exit 2
63 exec 3<&-
64
65 if [[ $first_time = true ]];then
66 first_time=false
67 echo -e "You may press the interrupt key at any time to quit.\n"
68 fi
69
70 PS3=$ques" " #Make $ques the prompt for select
71 #and add some spaces for legibility
72 select answer in "${choices[@]}";do
73 if [[ -z "$answer" ]];then
74 echo "Not a valid choice.Please choose again."
75 elif [[ "$answer" = "$correct_answer" ]];then
76 echo 'Correct!'
77 return 1
78 else
79 echo "No,the answer is $correct_answer."
80 return 0
81 fi
82 done
83 }
84
85 #=========================
86 function summarize (){
87 echo #skip a line
88 if ((num_ques == 0 ));then
89 echo "You did not answer any questions"
90 exit 0
91 fi
92
93 ((percent=num_correct*100/num_ques))
94 echo "You answered $num_correct questions correctly,out of \
95 $num_ques total questions."
96 echo "Your score is $percent percent."
97 }
98
99 #============================
100 # main program
101 initialize #Step 1 in top-level design
102
103 subject=$(choose_subj) #Step 2
104 [[ $? -eq 0 ]] || exit 2 #If no valid choice,exit
105
106 cd $subject || exit 2 #Step 3
107 echo #skip a line
108 scramble #step 4
109
110 for ques in ${questions[*]};do #step 5
111 ask $ques
112 result=$?
113 (( num_ques=numques+1 ))
114 if [[ $result == 1 ]];then
115 (( num_correct +=1 ))
116 fi
117 echo #skip a line between questions
118 sleep ${QUIZDELAY:=1}
119 done
120
121
122 summarize #step 6
123 exit 0
[zengqingfu@zengqingfu shell_script]$ cd ..
[zengqingfu@zengqingfu ~]$ ll quiz/
总用量 40
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:23 art
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:25 biology
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:25 chemistry
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:25 Chinese
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:23 engineering
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:25 english
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:26 history
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:33 math
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:26 physics
drwxrwxr-x. 2 zengqingfu zengqingfu 4096 3月 17 10:24 politics
问题保存在脚本父目录的下面,分为好几类:
只要执行quiz脚本,制定好问题quiz所在的路径,脚本就能读取问题,然后你回答问题,而正确答案已经在问题的4个选项后面写好了,将你回答的与正确答案一比较,就能判断正误。