视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
linux操作系统课程设计报告
2025-10-03 14:38:32 责编:小OO
文档
操作系统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 shell

usage()

{

  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 parameter

showalldata()

{

  echo "========================================="

  echo "         Welcom to Student Shell"

  echo "========================================="

  echo "NO    STDID    NAME    SEX    MAJOR"

  awk -F: '{OFS="\"}{print NR,$1,$2,$3,$4;}' $FILENAME

  # Another way to print data of file

  # for student in `cat $FILENAME`

  # do

  #   echo -e ${student//:/"\"}

  # done

}

#####################################################################

# Search a given STDID

# If find it , return 0, or return 1

# With parameter: STDID

findstd()

{

  local tno=`awk -F: '$1 ~ /'$1'/ {print $1}' $FILENAME`

  if [ -z "$tno" ]; then

    return 1

  else

    return 0

  fi

  # 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: STDID

printstd()

{

  echo "STDID    NAME    SEX    MAJOR"

  awk -F: '$1 ~ /'$1'/ {print $1"\"$2"\"$3"\"$4;}' $FILENAME

}

#####################################################################

# Add a new record to file

# With parameter: STDID:NAME:SEX:MAJOR

addstd()

{

echo "$1" >> "$FILENAME"

}

#####################################################################

# Delete the information of a given STDID

# With parameter: STDID

delstd()

{

  echo -n "Do you realy want to delete the student NO.$1 (y/n): "

  read ANS

  case $ANS in

    y|Y|yes|Yes|YES)

      for student in `cat $FILENAME`

      do

        if [ "${student%%:*}" = "$1" ]; then

          sed -i '/'$student'/d' $FILENAME

          break

        fi

      done

      echo "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 parameter

      if [[ "$2" =~ "^[0-9]{3}:[a-zA-Z]{1,}:[F|f|M|m]{1}:[a-zA-Z]{1,}$" ]]

      then

        tno="${2%%:*}"

        findstd $tno

        if [ "$?" = "0" ];then

          echo "The student NO.$tno has already exist in file:"

          printstd $tno

        else

          addstd $2

          echo "Add student NO.$tno successfully!"

        fi

      else

        usage1

      fi

      ;;

    -d)

      # Check the validity of second parameter

      if [[ "$2" =~ "^[0-9]{3}$" ]]

      then

        findstd $2

        if [ "$?" != "0" ];then

          echo "The student NO.$2 does not exist!"

        else

          printstd $2

          delstd $2

        fi

      else

        usage2

      fi

      ;;

     -s)

      # Check the validity of second parameter

      if [[ "$2" =~ "^[0-9]{3}$" ]]

      then

        findstd $2

        if [ "$?" != "0" ];then

          echo "The student NO.$2 does not exist!"

        else

          printstd $2

        fi

      else

        usage3

      fi

      ;;

    #When the first parameter is other format, show usage to user

    *)

echo "`basename $0`: error can't token the parameter \\"$1\\"" >&2

      echo "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 it

  if ! [ -e "$FILENAME" ]; then

    touch $FILENAME

echo "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 file

  if [ -z "$1" ]; then

    showalldata

  # When the parameter is not null, then do the following operation

  else

    chkpara $1 $2

  fi

}

student $1 $2

四、程序运行结果:

五、实验总结:

通过这次实验报告,我掌握了shell程序的创建过程,熟悉了Shell程序的基本语法,进一步了解了linux系统及其应用。不过对其过程还不是很熟练,需要进一步巩固和加强。下载本文

显示全文
专题