[问题][问题]对一些命令的解析问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
luchen
帖子: 52
注册时间: 2007-07-16 18:40
来自: 浙江

[问题][问题]对一些命令的解析问题

#1

帖子 luchen » 2007-11-12 22:24

这是一个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.tar
quiz代码
(10 KiB) 已下载 90 次
回复