(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系统及其应用。不过对其过程还不是很熟练,需要进一步巩固和加强。下载本文