shell awk 交互程序 (一个英语生字测验的程序)
2014-03-07 17:27
351 查看
执行awk程序时, awk会自动从文件中读取数据来进行处理, 直到文件结束.只要将awk读取数据的来源改成键盘输入,便可设计与awk 交互的程序了。
首先编写一个数据档test.dat
编辑awk程序"c2e"如下:
执行时键入如下指令 :
$./c2e test.dat
屏幕将产生如下的画面:
Press "ctrl-d " to exit
莲雾 的英文生字是:
若输入 bellfruit
程序将产生
You are right !! Press Enter to Continue ---
转自:http://www.aslibra.com/doc/awk.htm
首先编写一个数据档test.dat
apple 苹果 orange 柳橙 banana 香蕉 pear 梨子 starfruit 杨桃 bellfruit 莲雾 kiwi 奇异果 pineapple 菠萝 watermelon 西瓜
编辑awk程序"c2e"如下:
#!/bin/sh awk ' BEGIN { while( getline < ARGV[1] ){ #由指定的文件中读取测验数据 English[++n] = $1 # 最后, n 将表示题目之题数 Chinese = $2 } ARGV[1] = "-" # "-"表示由stdin(键盘输入) srand() # 以系统时间为随机数启始的种子 question() #产生考题 } {# awk自动读入由键盘上输入的数据(使用者回答的答案) if($1 != English[ind] ) print "Try again!" else{ print "\nYou are right !! Press Enter to Continue --- " getline question()#产生考题 } } function question(){ ind = int(rand()* n) + 1 #以随机数选取考题 system("clear") print " Press \"ctrl-d\" to exit" printf("\n%s ", Chinese[ind] " 的英文生字是: ") } ' $*
执行时键入如下指令 :
$./c2e test.dat
屏幕将产生如下的画面:
Press "ctrl-d " to exit
莲雾 的英文生字是:
若输入 bellfruit
程序将产生
You are right !! Press Enter to Continue ---
转自:http://www.aslibra.com/doc/awk.htm
相关文章推荐
- shell 基础学习
- shell中的括号(小括号,大括号/花括号)
- apk使用adb shell命令 Process p = Runtime.getRuntime().exec(cmd)
- shell脚本报错:"[: =: unary operator expected"
- 写一篇最简单的加壳,介绍一下壳的原理(Shell)
- shell脚本中for、while和do的使用以及区别
- Linux中shell文件操作大全
- shell编程:引用变量、内部变量、条件测试、字符串比较、整数比较、文件比较、逻辑操作、引号、通配符、调试执行
- bash 小技巧
- linux shell中的一些关键字
- Shell变量$0 $# $@ $*
- adb shell dumpsys 命令
- SHELL脚本扩展
- Shell脚本编程的常识
- 插入排序,选择排序,Shell排序
- shell递归遍历目录
- Tee Command Usage Examples
- 正则表达式 流派(flavor)及差异简介
- Shell 常用的一些命令技巧
- Why do you need to put #!/bin/bash at the beginning of a script file?