linux操作系统课程设计报告
- 格式:doc
- 大小:53.00 KB
- 文档页数:8
操作系统II(实践)期末作业(Linux Shell 程序设计)学院:专业:班级:学号:姓名:2011年6月3日一、所用仪器、材料。
PC一台Redhat linux 5.5二、实验内容:有文本文件student.txt,该文件是所有学生的信息,每个学生存储一行信息,信息格式如下学号:姓名:性别:专业如(101:张三:男:计算机)设计一个shell,名称为student.sh,该shell完成如下功能:1) 当输入 student.sh -a 101:张三:男:计算机时,首先判断101记录是否存在,如果不存在,则把该信息写入文件,如果存在,则给出提示,并输出文件中学号为101的该行信息2) 当输入 student.sh -d 101 时,首先判断101记录是否存在,如果不存在,给出提示,如果存在,则提示用户确认是否要删除记录,如用户输入y或者yes,则删除文件中学号为101的该行信息,如果用户输入n或no时,则不做删除操作3) 当输入 student.sh -s 101 时,首先判断101记录是否存在,如果不存在,给出提示,如果存在,则输出文件中学号为101的该行信息4) 当输入 student.sh时,输出文件的全部内容5) 当用户输入的选项不正确时,给出错误提示,并输入该shell的用法,如(student.sh -q 123456)三、实验代码:#!/bin/bash#student.sh# Define the 'FILENAME'declare FILENAME="student.txt"################################################################# ##### Usage of this shellusage(){echo "Usage: student.sh [OPTION] [STDID:NAME:SEX:MAJOR|STDID]"echo "Notice:"echo " This shell program need to create a file \"student.txt\""echo "in current directory. If it does not exist, the shell will"echo "create it."echo ""echo "[OPTION]"echo " without parameter, shell will show all data of student."echo " -a, --add a student"echo " -d, --delete a student"echo " -s, --search a student"echo ""echo " STDID: must be 3 numbers, for example: 001."echo " NAME: must be consist of letters at least one."echo " SEX: must be one value of following: F, f, M, m."echo " MAJOR: limit is the same with NAME."echo ""echo " Example:"echo " Add a student named Tom, his STDID is 002, major is Math."echo " e.g. student.sh -a 002:Tom:M:Math"echo " Delete the student STDID is 002."echo " e.g. student.sh -d 002"echo " Search the student STDID is 002."echo " e.g. student.sh -s 002"}################################################################# ##### Usage for parameter '-a'usage1(){echo "The parameter '-a' need a value: STDID:NAME:SEX:MAJOR"echo "e.g. student.sh -a 002:Tom:M:Math"}################################################################# ##### Usage for parameter '-d'usage2(){echo "The parameter '-d' need a value: STDID"echo "e.g. student.sh -d 001"}################################################################# ##### Usage for parameter '-s'usage3(){echo "The parameter '-s' need a value: STDID"echo "e.g. student.sh -s 001"}################################################################# ##### Show information of all student# Without parametershowalldata(){echo "========================================="echo " Welcom to Student Shell"echo "========================================="echo "NO STDID NAME SEX MAJOR"awk -F: '{OFS="\t"}{print NR,$1,$2,$3,$4;}' $FILENAME# Another way to print data of file# for student in `cat $FILENAME`# do# echo -e ${student//:/"\t"}# done}################################################################# ##### Search a given STDID# If find it , return 0, or return 1# With parameter: STDIDfindstd(){local tno=`awk -F: '$1 ~ /'$1'/ {print $1}' $FILENAME`if [ -z "$tno" ]; thenreturn 1elsereturn 0fi# Another way to search the STDID# local tno=""# for student in `cat $FILENAME`# do# if [ "${student%%:*}" = "$1" ]; then# tno=$1# break# fi# done# if [ -z "$tno" ]; then# return 1# else# return 0# fi}################################################################# ##### Print the information of a given STDID# With parameter: STDIDprintstd(){echo "STDID NAME SEX MAJOR"awk -F: '$1 ~ /'$1'/ {print $1"\t"$2"\t"$3"\t"$4;}' $FILENAME}################################################################# ##### Add a new record to file# With parameter: STDID:NAME:SEX:MAJORaddstd(){echo "$1" >> "$FILENAME"}################################################################# ##### Delete the information of a given STDID# With parameter: STDIDdelstd(){echo -n "Do you realy want to delete the student NO.$1 (y/n): "read ANScase $ANS iny|Y|yes|Yes|YES)for student in `cat $FILENAME`doif [ "${student%%:*}" = "$1" ]; thensed -i '/'$student'/d' $FILENAMEbreakfidoneecho "The student NO.$1 has been deleted successfully.";;n|N|no|No|NO)echo "The student NO.$1 does not be deleted.";;*)echo "Please enter y or n.";;esac}################################################################# ##### Check the parameter for this shell# With parameter: para[-a, -d, -s], value[STDID|...]chkpara(){case $1 in'--help')usage;;-a)# Check the validity of second parameterif [[ "$2" =~ "^[0-9]{3}:[a-zA-Z]{1,}:[F|f|M|m]{1}:[a-zA-Z]{1,}$" ]]thentno="${2%%:*}"findstd $tnoif [ "$?" = "0" ];thenecho "The student NO.$tno has already exist in file:"printstd $tnoelseaddstd $2echo "Add student NO.$tno successfully!"fielseusage1fi;;-d)# Check the validity of second parameterif [[ "$2" =~ "^[0-9]{3}$" ]]thenfindstd $2if [ "$?" != "0" ];thenecho "The student NO.$2 does not exist!"elseprintstd $2delstd $2fielseusage2fi;;-s)# Check the validity of second parameterif [[ "$2" =~ "^[0-9]{3}$" ]]thenfindstd $2if [ "$?" != "0" ];thenecho "The student NO.$2 does not exist!"elseprintstd $2fielseusage3fi;;#When the first parameter is other format, show usage to user*)echo "`basename $0`: error can't token the parameter \"$1\"" >&2echo "You can run \"student.sh --help\" to get help.";;esac}################################################################# ##### The main function of this shell# With parameter: para[-a|-d|-s], value[STDID|...]student(){# If the file does not exists, then create itif ! [ -e "$FILENAME" ]; thentouch $FILENAMEecho "001:NAME:M:MAJOR" > "$FILENAME"echo "When you run this shell first, it will create a file. It named"echo "\"student.txt\". Run \"student.sh --help\" to get help"echo "The following data is a test data."fi# When the parameter is null, print all data of fileif [ -z "$1" ]; thenshowalldata# When the parameter is not null, then do the following operationelsechkpara $1 $2fi}student $1 $2四、程序运行结果:五、实验总结:通过这次实验报告,我掌握了shell程序的创建过程,熟悉了Shell程序的基本语法,进一步了解了linux系统及其应用。