当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 1 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : [问题][问题]对一些命令的解析问题
帖子发表于 : 2007-11-12 22:24 

注册: 2007-07-16 18:40
帖子: 52
地址: 浙江
送出感谢: 0 次
接收感谢: 0 次
这是一个quiz的bash的解析执行过程(在/usr/games/quiz/下建立了3个目录culture politics nature并且在nature下有4个文件,其格式是:
问题
4
选项1
选项2
选项3
选项4
正确答案(选项i)
root@person:/home/luchen/mytest# bash -x quiz
+ initialize
+ trap 'summarize ; exit 0' INT
+ num_ques=0
+ num_correct=0
+ first_time=true
+ cd /usr/games/quiz
++ choose_subj
++ subjects=($(ls))
+++ ls
++ PS3='Choose a subject for the quiz from the preceding list:'
++ select Subject in '${subjects[*]}'
1) culture
2) nature
3) politics
Choose a subject for the quiz from the preceding list:2
++ [[ -z nature ]]
++ echo nature
++ return 0
+ subject=nature
+ [[ 0 -eq 0 ]]
+ cd nature
+ echo

+ scramble
+ typeset -i index quescount
+ questions=($(ls))
++ ls
+ quescount=4
+ (( index=quescount-1 ))
+ [[ 3 > 0 ]]
+ (( target=RANDOM % index ))
+ exchange 2 3
+ temp_value=nature3
+ questions[$1]=nature4
+ questions[$2]=nature3
+ (( index-=1 ))
+ [[ 2 > 0 ]]
+ (( target=RANDOM % index ))
+ exchange 0 2
+ temp_value=nature
+ questions[$1]=nature4
+ questions[$2]=nature
+ (( index-=1 ))
+ [[ 1 > 0 ]]
+ (( target=RANDOM % index ))
+ exchange 0 1
+ temp_value=nature4
+ questions[$1]=nature2
+ questions[$2]=nature4
+ (( index-=1 ))
+ [[ 0 > 0 ]]
+ for ques in '${questions[*]}'
+ ask nature2
+ exec
+ read -u3 ques
+ read -u3 num_opts
+ index=0
+ choices=()
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 correct_answer
+ exec
+ [[ true = true ]]
+ first_time=false
+ echo -e 'You may press the interrupt key at any time to quit.\n'
You may press the interrupt key at any time to quit.

+ PS3='who is a person ? '
+ select answer in '"${choices[@]}"'
1) aaa
2) sss
3) eddd
4) er
who is a person ? 2
+ [[ -z sss ]]
+ [[ sss = \e\d\d\d ]]
+ echo 'No, the answer is eddd.'
No, the answer is eddd.
+ return 0
+ result=0
+ (( num_ques=num_ques+1 ))
+ [[ 0 == 0 ]]
+ (( num_correct += 1 ))
+ echo

+ sleep 1
+ for ques in '${questions[*]}'
+ ask nature4
+ exec
+ read -u3 ques
+ read -u3 num_opts
+ index=0
+ choices=()
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 correct_answer
+ exec
+ [[ false = true ]]
+ PS3='who is a girl? '
+ select answer in '"${choices[@]}"'
1) fr
2) gt
3) hy
4) ju
who is a girl? 3
+ [[ -z hy ]]
+ [[ hy = \f\r ]]
+ echo 'No, the answer is fr.'
No, the answer is fr.
+ return 0
+ result=0
+ (( num_ques=num_ques+1 ))
+ [[ 0 == 0 ]]
+ (( num_correct += 1 ))
+ echo

+ sleep 1
+ for ques in '${questions[*]}'
+ ask nature
+ exec
+ read -u3 ques
+ read -u3 num_opts
+ index=0
+ choices=()
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 correct_answer
+ exec
+ [[ false = true ]]
+ PS3='who discovered the infinitesimal calculus? '
+ select answer in '"${choices[@]}"'
1) newton
2) fack
3) yun
4) ooo
who discovered the infinitesimal calculus? 1
+ [[ -z newton ]]
+ [[ newton = \n\e\w\t\o\n ]]
+ echo 'Correct!'
Correct!
+ return 1
+ result=1
+ (( num_ques=num_ques+1 ))
+ [[ 1 == 0 ]]
+ echo

+ sleep 1
+ for ques in '${questions[*]}'
+ ask nature3
+ exec
+ read -u3 ques
+ read -u3 num_opts
+ index=0
+ choices=()
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 correct_answer
+ exec
+ [[ false = true ]]
+ PS3='who is a bear? '
+ select answer in '"${choices[@]}"'
1) www
2) eee
3) yy
4) rf
who is a bear? 2
+ [[ -z eee ]]
+ [[ eee = \r\f ]]
+ echo 'No, the answer is rf.'
No, the answer is rf.
+ return 0
+ result=0
+ (( num_ques=num_ques+1 ))
+ [[ 0 == 0 ]]
+ (( num_correct += 1 ))
+ echo

+ sleep 1
+ summarize
+ echo

+ (( num_ques == 0 ))
+ (( percent=num_correct*100/num_ques ))
+ echo 'You answerd 3 questions correctly, out of 4 total questions.'
You answerd 3 questions correctly, out of 4 total questions.
+ echo 'Your score is 75 percent.'
Your score is 75 percent.
+ exit 0
我想问其中的
+ choices=()
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 next_choice
+ choices=("${choices[@]}" "$next_choice")
+ (( index +=1 ))
+ (( index < num_opts ))
+ read -u3 correct_answer
+ exec
该如何理解?(源代码见包)


附件:
文件注释: quiz代码
quiz.tar [10 KiB]
被下载 47 次
页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 1 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译