Linux笔记 #08# shell编程从零开始到低配学生管理系统
先熟悉一下基本语法(运行环境是装git的时候一起装的那个windows下的bash):
#!/bin/bash # 实现两个函数 # 1 appendToFile()追加一行到文件 # 2 readFile()把文件中所有内容逐行写到控制台 echo "welcome to bash!" echo 'welcome to bash!' printf '%-5s %-10s %-4s\n' No Name Mark printf '%-5s %-10s %-4.2f\n' 1 Jack 20.012 # welcome to bash! # welcome to bash! # No Name Mark # 1 Jack 20.01 v='sads' # v2 = 'dasdas' # => v2: command not found(这是是一个错误示范) echo v2 # => v2 echo $v # => sads echo ${v} # => sads echo 'hello, $v and ${v}' # => hello, $v and ${v} echo "hello, $v and ${v}" # => hello, sads and sads echo ${#v} # => 4(字符串长度) let digit1=3 let digit2=2 let result=digit1+digit2 echo $result # => 5 # echo "4 * 0.5" | bc echo "重定向到文本" > text.txt echo "追加到目标文件" >> text.txt # $ cat text.txt # 重定向到文本 # 追加到目标文件 array[0]='daasd' array[3]='asassd' echo ${array[0]} # => daasd echo ${array[*]} # => daasd asassd echo ${!array[*]} # => 0 3 # $ sh -v linux.sh # $ sh -x linux.sh 进行调试打印 function f() { echo '接下来是f的输出:' echo $1 echo $2 echo $@ echo $* } f 1 'hello' 3 # 调用函数 # 接下来是f的输出: # 1 # hello # 1 hello 3 # 1 hello 3 FILENAME=student appendToFile() { # arg1: filename, arg2: content echo $1 >> $FILENAME } appendToFile "小张 25 未婚" readFile() { while read LINE do echo $LINE done < $FILENAME } echo 'invoke readFile:' readFile # invoke readFile: # 小张 25 未婚 # 小张 25 未婚
再结合百度完成低配版的学生管理系统:
#!/bin/bash # 构造三个类似数据库的文本文件: # 第一个为学院信息文件,包含字段: # 学院编号(唯一),学院名称 # 第二个为学生信息文件,包含字段: # 学号(唯一),学生姓名,所在学院编号,说明(休学suspended,退学dropout) # 第三个为学生成绩文件,包含字段: # 学号(唯一),学生姓名,科目名称,成绩;说明(期考final,补考makeup) # 说明:每个记录占一行;分隔符可以自己选定,建议用”,”; 编码规则自己定;文件名自己定 # 程序功能:要求实现4个功能,每个功能作为一个函数 # 1.向文件中插入记录 # 2.显示文件中的每条记录的每个字段值 # 3.从文件中修改指定学号的记录 # 4.对学生成绩进行统计(包括每个学生总成绩;每个学科前3名和总成绩前5名统计) newFile() { # arg1: filename # 例子:newFile temp touch :>$1 } saveRecord() { # arg1: filename, arg2: content # 例子:saveRecord temp "小张 25 未婚" echo $2 >> $1 } initTables() { # 数据库文件初始化 # academy "academy_id,academy_name" # student "stu_id,stu_name,academy_id,stu_description" # subject "stu_id,stu_name,subject_name,score,subject_description" newFile academy newFile student newFile subject } updateStudentById() { # arg1: 学号, arg2:新的记录 # 例子:updateStudentById '2132022129' '2132022129,李四,001,dropout' oldStr=`grep $1 student` # 把grep的执行结果赋给oldStr newStr=$2 sed -i "s/${oldStr}/${newStr}/" student } echoTitle() { echo '-------------------------------------------------------' echo $1 echo '-------------------------------------------------------' } endl() { echo -e '\n' } listRecords() { # arg1: data_filename while read LINE do echo $LINE done < $1 } initRecords() { # 学院信息 saveRecord academy "001,挪威阿克苏而达学院" saveRecord academy "002,哈尔滨佛学院(简称哈佛)" # 学生信息 saveRecord student "2132022129,张三,001,dropout" saveRecord student "2132022139,李四,002,dropout" saveRecord student "2132022149,王五,001,dropout" saveRecord student "2132022159,老王,002,dropout" saveRecord student "2132022119,张飞,001,suspended" saveRecord student "2132022109,刘备,001,suspended" saveRecord student "2132022179,关羽,002,suspended" # linux成绩 saveRecord subject "2132022129,张三,linux,0,makeup" saveRecord subject "2132022139,李四,linux,13.5,makeup" saveRecord subject "2132022149,王五,linux,23.5,makeup" saveRecord subject "2132022119,张飞,linux,99.5,makeup" saveRecord subject "2132022109,刘备,linux,0,makeup" saveRecord subject "2132022179,关羽,linux,100,final" saveRecord subject "2132022159,老王,linux,43.5,makeup" # math成绩 saveRecord subject "2132022129,张三,math,10,final" saveRecord subject "2132022139,李四,math,23.5,final" saveRecord subject "2132022149,王五,math,33.5,final" saveRecord subject "2132022119,张飞,math,89.5,final" saveRecord subject "2132022109,刘备,math,55,final" saveRecord subject "2132022179,关羽,math,50,final" saveRecord subject "2132022159,老王,math,93.5,final" } totalScore() { # arg1: 学号 # 打印某个学生的学号,姓名,总成绩 echo `awk -v stuId=$1 -F, 'BEGIN{total=0} {if($1==stuId){total+=$4;name=$2}} END{print stuId,name,total}' OFS="," subject` } subjectScore() { # arg1: 学号, arg2: 科目名 # 打印某个学生的学号,姓名,单科成绩 pattern="^$1.*$2" record=`grep -E $pattern subject` echo $record } sortByColumn() { # arg1: 按第几列排,arg2: 需要排名的文件 sort -nrk $1 -t, $2 } topStudent() { # 打印成绩排名,默认打印总成绩排名 # arg1: 科目名 newFile topStudent if [ ! -n "$1" ] ;then while read LINE do stuId=${LINE%%,*} saveRecord topStudent `totalScore $stuId` done < student echoTitle '**总成绩排名' sortByColumn 3 topStudent; endl else while read LINE do stuId=${LINE%%,*} saveRecord topStudent `subjectScore $stuId $1` done < student echoTitle "**单科成绩排名>>>>$1<<<" sortByColumn 4 topStudent; endl fi } main() { initTables initRecords echoTitle '1、学院表一览' listRecords academy; endl echoTitle '2、学生表一览' listRecords student; endl echoTitle '3、成绩表一览' listRecords subject; endl updateStudentById '2132022129' '2132022129,张三,002,dropout' echoTitle '**学生表一览(更新2132022129后)' listRecords student; endl topStudent topStudent 'linux' topStudent 'math' } main