【Linux基础】综合应用

至此,我们己经学习完作为程序设计语言的shell的主要功能。是时候将我们所学的知识结合起来以编写一个实际的示范程序了。
纵贯全章,我们将编写及设计一个记录学生成绩的应用程序。
需求分析
设计框图
对数据进行更新、检索和显示的这三项需求表明采用一个简单的菜单应该就足够了。由于所有需要存储的数据全部都是文本,而且假设要记录的学生数不是很多,所以我们没有必要使用复杂的数据库,使用一些简单的文本文件即可。将资料保存在文本文件中将使我们的应用程序比较简单,而且如果需求发生了变化,操纵文本文件总是要比操纵其它类型的文件更加容易。至少我们可以使用一个编辑器来手工输入和删除数据,而不必非要通过编写程序来完成。
首先,
程序说明
(1)先定义学生个人信息文件、成绩文件和一个临时文件。同时还设置Ctrl+C组合键的中断处理,以确保用户中断脚本程序时删除临时文件。
students_file="students_file"
achievements_file="achievements_file"
temp_file="temp_file"
trap 'rm -f $temp_file' EXIT
(2)由于在与脚本互动时,经常要对一些选项进行判断选择,所以下面将一些利用较高的功能代码做成两个简单的工具型函数。
get_return()
{
echo -e "Please return \c"
# read x
return 0
}
get_confirm()
{
echo -e "Are you sure?\n"
while true
do
read x
case "$x" in
y | Y | YES | yes )
return 0;;
n | N | no | NO )
# echo
echo "Cancelled"
return 1;;
* ) echo "Please enter yes or no" ;;
esac
done
}
菜单项
下面函数为主菜单函数。菜单的选项多样,这主要是根据功能模块来设计的,而且内容是动态变化的,当用户选择了
set_menu_choice()
{
echo "Options :-"
echo
echo " a) Add new Student_Information"
echo " f) Find Student_Information"
echo " c) Count the Student_Informations and scores in the catalog"
echo " s) Reference Information about students"
if [ "$stu_catnum" != "" ];then
echo " l) List scores on $stu_name"
echo " r) Remove $stu_name"
echo " u) Update achievements for $stu_name"
fi
echo " q) Quit"
echo
echo -e "Please enter choice then press return: \c"
read menu_choice
return
}
4、新增
insert_student() # what
{
echo $* >> $students_file
return
}
insert_scores() # what
{
echo $* >> $achievements_file
return
}
add_record_achievements()
{
echo "Enter score information for this student"
echo "When no more scores enter q"
score_num=1
subject_ttitle=""
while [ "$subject_ttitle" != "q" ] # where from
do
echo -e "Enter Subject $score_num title: \c"
read tmp
subject_ttitle=${tmp%%,*}
if [ "$tmp" != "$subject_ttitle" ]; then
echo "Sorry, no commas allowed"
continue
fi
if [ -n "$subject_ttitle" ] ; then
if [ "$subject_ttitle" != "q" ] ;then
echo -e "Enter score: \c"
read tmp
score=${tmp%%,*}
insert_scores $stu_catnum,$score_num,$subject_ttitle,$score
fi
else
score_num=$((score_num-1))
fi
score_num=$((score_num+1))
done
}
add_records()
{
echo -e "Enter student's numble: \c"
read tmp
stu_catnum=${tmp%%,*}
echo -e "Enter student's name: \c"
read tmp
stu_name=${tmp%%,*}
echo -e "Enter student's major: \c"
read tmp
stu_major=${tmp%%,*}
echo -e "Enter student's professor: \c"
read tmp
stu_professor=${tmp%%,*}
echo "about to add new entry"
echo "$stu_catnum $stu_name $stu_major $stu_professor"
if get_confirm ;then
insert_student $stu_catnum,$stu_name,$stu_major,$stu_professor
add_record_achievements
else
remove_records
fi
return
}
5、删除
remove_records()
{
if [ -z "$stu_catnum" ];then
echo "You must select a student first"
find_student n
fi
if [ -n "$stu_catnum" ];then
echo "You are about to delete $stu_name"
get_confirm && {
grep -v "^${stu_catnum}," $students_file > $temp_file
mv $temp_file $students_file
grep -v "^${stu_catnum}," $achievements_file > $temp_file
mv $temp_file $achievements_file
stu_catnum=""
echo "Enter removed"
}
get_return
fi
return
}
6、更新
update_cd()
{
if [ -z "$stu_catnum" ];then
echo "You must select a student first"
find_student n
fi
if [ -n "$stu_catnum" ];then
echo "Current achievement are :-"
list_achievements
echo
echo "This will re-enter the achievements for $stu_name"
get_confirm && {
grep -v "^${stu_catnum}," $achievements_file > $temp_file
mv $temp_file $achievements_file
echo
add_record_achievements
}
fi
return
}
7、检索
find_student()
{
if [ "$1" = "n" ];then
asklist=n
else
asklist=y
fi
stu_catnum=""
echo -e "Enter a string to search for in the student titles: \c"
read searchstr
if [ "$searchstr" = "" ];then
return 0
fi
grep "$searchstr" $students_file > $temp_file
set $(wc -l $temp_file)
linesfound=$1
case "$linesfound" in
0) echo "Sorry,nothing found"
get_return
return 0
;;
1) ;;
2) echo "Sorry,not unique."
echo "Found the following"
cat $temp_file
get_return
return 0
esac
IFS=","
read stu_catnum stu_name stu_major stu_professor < $temp_file
IFS=" "
if [ -z "$stu_catnum" ];then
echo "Sorry,could not extract catalog field from $temp_file"
get_return
return 0
fi
echo
echo "Student' number: $stu_catnum"
echo "Student' name: $stu_name"
echo "Student's major: $stu_major"
echo "Student's professor: $stu_professor"
echo
get_return
if [ "$asklist" = "y" ];then
echo -e "View scores for this student(y or n ?): \c"
read x
if [ "$x" = "y" ];then
echo
list_achievements
echo
fi
fi
return 1
}
8、
count_students()
{
set $(wc -l $students_file)
num_student=$1
set $(wc -l $achievements_file)
num_achievement=$1
echo "found $num_student students,with a total of $num_achievement achievements"
get_return
return
}
9、
list_achievements()
{
echo "student's numble=$stu_catnum"
if [ "$stu_catnum" = "" ];then
echo "no student select yet"
return
else
grep "^${stu_catnum}," $achievements_file > $temp_file
num_achievements=$(wc -l $temp_file)
if [ "$num_achievements" = "0" ];then
echo "no achievements found for $stu_name"
else {
echo
echo "$stu_name :-"
echo
cut -f 2- -d , $temp_file
echo
} | ${PAGER:-more}
fi
fi
get_return
return
}
10、主程序
rm -f $temp_file
if [ ! -f students_file ];then
touch students_file
fi
if [ ! -f achievements_file ];then
touch achievements_file
fi
clear
echo
echo "*************************************************************"
echo ">> Student_achievement manager <<"
echo ">> http://www.gec-edu.org <<"
echo "*************************************************************"
echo
sleep 1
quit=n
while [ "$quit" != "y" ];
do
set_menu_choice
case "$menu_choice" in
a) add_records;;
r) remove_records;;
f) find_student y;;
u) update_cd;;
c) count_students;;
l) list_achievements;;
s)
echo
more $students_file
echo
get_return;;
q | Q) quit=y;;
*) echo "Sorry,choice not recognized";;
esac
done
rm -f $temp_file
echo "Finish"
exit 0
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值