中南大学linux程序设计环境实验三.docx
《中南大学linux程序设计环境实验三.docx》由会员分享,可在线阅读,更多相关《中南大学linux程序设计环境实验三.docx(15页珍藏版)》请在冰豆网上搜索。
![中南大学linux程序设计环境实验三.docx](https://file1.bdocx.com/fileroot1/2023-1/7/253400f8-bfd7-490b-b051-3d22dc2cd761/253400f8-bfd7-490b-b051-3d22dc2cd7611.gif)
中南大学linux程序设计环境实验三
实验3LinuxShell程序设计
1、实验目的
(1)掌握Linuxshell程序运行方法
(2)掌握LinuxShell程序基本语法
(3)了解Linux环境变量
(4)编写简单Linuxshell程序
2、实验内容
完成简单CD唱片管理系统。
(1)需求
假设你收集了大量的CD唱片,现在为了方便管理,需要设计和实现一个管理CD唱片的程序。
程序至少能够把每张CD唱片的资料保存起来,如唱片的名称、音乐类型、艺术家或作曲家的名字等;然后针对某张CD唱片而言,还需要保存一些简单的曲目信息;程序要可以以CD唱片为单位进行搜索,而不是以曲目为单位;为了使程序具备完整性,还需要实现对CD唱片资料的输入、修改和删除等功能。
(2)设计
把标题信息和曲目信息分开,用两个文本文件分别来存储。
每张CD唱片保存如下信息:
编号、标题、曲目类型(流行、爵士等)、作曲家等。
曲目只保存下面两个信息:
编号和曲名。
如下表所示:
表1.唱片信息
唱片编号
标题
曲目类型
作曲家
CD123
Coolsax
jazz
Bix
CD234
Classicviolin
classic
Bach
CD345
Hits99
pop
Various
表2.曲目信息
目录编号
曲目编号
曲名
CD123
1
Somejazz
CD123
2
Morejazz
CD234
1
SonatainDminor
CD345
1
Dizzy
各项信息用逗号来分隔。
(3)实现
程序共包含如下函数:
get_return()
get_confirm()
set_menu_choice()
insert_title()
insert_track()
add_record_tracks()
add_records()
find_cd()
update_cd()
count_cds()
remove_records()
list_tracks()
准确无误代码:
#!
/bin/bash
#VerysimpleexampleshellscriptformanagingaCDcollection.
#Copyright(C)2012-2013CentralSouthUniversity.
menu_choice=""
current_cd=""
title_file="title.cdb"
tracks_file="tracks.cdb"
temp_file=/tmp/cdb.$$
trap'rm-f$temp_file'EXIT
get_return(){
echo-e"Pressreturn:
\c"
readx
return0
}
get_confirm(){
echo-e"Areyousure?
\c"
whiletrue
do
readx
case"$x"in
y|yes|Y|Yes|YES)
return0;;
n|no|N|No|NO)
echo
echo"Cancelled"
return1;;
*)
echo"Pleaseenteryesorno";;
esac
done
}
set_menu_choice(){
clear
echo"Options:
-"
echo
echo"a)AddnewCD"
echo"f)FindCD"
echo"c)CounttheCDsandtracksinthecatalog"
if["$cdcatnum"!
=""];then
echo"l)Listtrackson$cdtitle"
echo"r)Remove$cdtitle"
echo"u)Updatetrackinformationfor$cdtitle"
fi
echo"q)Quit"
echo
echo-e"Pleaseenterchoicethenpressreturn\c"
readmenu_choice
return
}
insert_title(){
echo$*>>$title_file
return
}
insert_track(){
echo$*>>$tracks_file
return
}
add_record_tracks(){
echo"EntertrackinformationforthisCD"
echo"Whennomoretracksenterq"
cdtrack=1
cdtitle=""
while["$cdtitle"!
="q"]
do
echo-e"Track$cdtrack,tracktitle?
\c"
readtmp
cdtitle=${tmp%%,*}
if["$tmp"!
="$cdtitle"];then
echo"Sorry,nocommasallowed"
continue
fi
if[-n"$cdtitle"];then
if["$cdtitle"!
="q"];then
insert_track$cdcatnum,$cdtrack,$cdtitle
fi
else
cdtrack=$((cdtrack-1))
fi
cdtrack=$((cdtrack+1))
done
}
add_records(){
#Promptfortheinitialinformation
echo-e"Entercatalogname:
\c"
readtmp
cdcatnum=${tmp%%,*}
echo-e"Entertitle:
\c"
readtmp
cdtitle=${tmp%%,*}
echo-e"Entertype:
\c"
readtmp
cdtype=${tmp%%,*}
echo-e"Enterartist/composer:
\c"
readtmp
cdac=${tmp%%,*}
#Checkthattheywanttoentertheinformation
echoAbouttoaddnewentry
echo"$cdcatnum$cdtitle$cdtype$cdac"
#Ifconfirmedthenappendittothetitlesfile
ifget_confirm;then
insert_title$cdcatnum,$cdtitle,$cdtype,$cdac
add_record_tracks
else
remove_records
fi
return
}
find_cd(){
if["$1"="n"];then
asklist=n
else
asklist=y
fi
cdcatnum=""
echo-e"EnterastringtosearchforintheCDtitles\c"
readsearchstr
if["$searchstr"=""];then
return0
fi
grep"$searchstr"$title_file>$temp_file
set$(wc-l$temp_file)
linesfound=$1
case"$linesfound"in
0)
echo"Sorry,nothingfound"
get_return
return0
;;
1)
;;
2)
echo"Sorry,notunique."
echo"Foundthefollowing"
cat$temp_file
get_return
return0
esac
IFS=","
readcdcatnumcdtitlecdtypecdac<$temp_file
IFS=""
if[-z"$cdcatnum"];then
echo"Sorry,couldnotextractcatalogfieldfrom$temp_file"
get_return
return0
fi
echo
echoCatalognumber:
$cdcatnum
echoTitle:
$cdtitle
echoType:
$cdtype
echoArtist/Composer:
$cdac
echo
get_return
if["$asklist"="y"];then
echo-e"ViewtracksforthisCD?
\c"
readx
if["$x"="y"];then
echolist_tracks
echo
fi
fi
return1
}
update_cd(){
if[-z"$cdcatnum"];then
echo"YoumustselectaCDfirst"
find_cdn
fi
if[-n"$cdcatnum"];then
echo"Currenttracksare:
-"
list_tracks
echo
echo"Thiswillre-enterthetracksfor$cdtitle"
get_confirm&&{
grep-v"^${cdcatnum},"$tracks_file>$emp_file
mv$temp_file$tracks_file
echo
add_record_tracks
}
fi
return
}
count_cds(){
set$(wc-l$title_file)
num_titles=$1
set$(wc-l$tracks_file)
num_tracks=$1
echofound$num_titlesCDs,withatotalof$num_trackstracks
get_return
return
}
remove_records(){
if[-z"$cdcatnum"];then
echo"YoumustselectaCDfirst"
find_cdn
fi
if[-n"$cdcatnum"];then
echo"Youareabouttodelete$cdtitle"
get_confirm&&{
grep-v“^${cdcatnum},”$title_file>$temp_file
mv$temp_file$title_file
grep-v“^${cdcatnum},”$tracks_file>$temp_file
mv$temp_file$tracks_file
cdcatnum=""
echoEntryremoved
}
get_return
fi
return
}
list_tracks(){
if["$cdcatnum"=""];then
echo"noCDselectedyet"
return
else
grep"^${cdcatnum},"$tracks_file>$temp_file
num_tracks=$(wc-l$temp_file)
if["$num_tracks"="0"];then
echo"notracksfoundfor$cdtitle"
else{
echo
echo"$cdtitle:
-"
echo
cut-f2--d,$temp_file
echo
}|${PAGER:
-more}
fi
fi
get_return
return
}
rm-f$temp_file
if[!
-f$title_file];then
touch$title_file
fi
if[!
-f$tracks_file];then
touch$tracks_file
fi
#Nowtheapplicationproper
clear
echo
echo
echo"MiniCDmanager"
sleep1
quit=n
while["$quit"!
="y"];
do
set_menu_choice
case"$menu_choice"in
a)
add_records;;
r)
remove_records;;
f)
find_cdy;;
u)
update_cd;;
c)
count_cds;;
l)
list_tracks;;
b)
echo
more$title_file
echoget_return;;
q|Q)
quit=y;;
*)
echo"Sorry,choicenotrecognized";;
esac
done
#Tidyupandleave
rm-f$temp_file
echo"Finished"
exit0
3、实验记录
4、心得体会
老师提供的源码里有许多错误,通过对需求的理解以及脚本的分析,得出正确的脚本代码,比如,对$1(数字)的含义不理解,就会把它当成$l(字母),$1(数字)在shell中称为“位置参数”,表示传入的第1个参数,而$l(字母)几乎不存在,除了l(字母)是个变量外。
其它类似的还有许多,所以要理解具体语句的含义后才会知道日后怎样去用。