#!/bin/bash
#A library application Environment:Ubuntu8.10
#Written by TAN 20090606 Copyright(C)
#tianjingke@gmail.com
#/////////////////////////////////////////////////////////////////////
enter_main_menu() # 主菜单函数
{
clear
#echo -e "Enter the author/title>\c"
#echo -e "You are in!!!"
#read con
#if [ $con ]
#then
echo -e "\n Linux Library -\033[1mMAIN MENU\033[0m" # \n换行 \033[1m xxx \[0m加粗
echo -e "\n 0:\033[1mExit\033[0m this program"
echo -e "\n 1:\033[1mEDIT\033[0m Menu"
echo -e "\n 2:\033[1mREPORTS\033[0m Menu"
echo -e "\n Enter your choice>\c" # \c echo -e "xx" 后光标不换行
#fi
#i=0
#until [ !$i ]
#do
read choice
case $choice in #用case语句进行选择
0) exit ;;
1) edit ;;
2) report ;;
*) echo -e "Wrong input ,try again>\c"
enter_main_menu ;;
esac
}
#//////////////////////////////////////////////////////////////////
edit() #编辑函数
{
clear
echo -e "\n Linux Library -\033[1mEDIT MENU\033[0m"
echo -e "\n 0:\033[1mRETURN\033[0m to the main menu"
echo -e "\n 1:\033[1mADD\033[0m"
echo -e "\n 2:\033[1mUPDAT STATUS\033[0m"
echo -e "\n 3:\033[1mDISPLAY\033[0m"
echo -e "\n 4:\033[1mDELETE\033[0m"
echo -e "\n Enter your choice>\c"
read choice
case $choice in
0) enter_main_menu ;;
1) edit_add ;;
2) edit_update ;;
3) edit_display ;;
4) edit_delete ;;
*) echo -e " EXIT NOW"
exit ;;
esac
}
#/////////////////////////////////////////////////////////////////////
display() #用来显示指定的内容
{
cut -d: -f1,2 `echo -e $1` #用cut来显示传入的文件信息 -d: 指定 “:”为分隔符 -f1,2显示第一个和第二个“:”
cut -d: -f3,4 `echo -e $1` #之前的内容,以此类推 $1 是传进来的第一个参数,即是调用此函数所带的参数
cut -d: -f5,6 `echo -e $1`
cut -d: -f7,8 `echo -e $1`
cut -d: -f9,10 `echo -e $1`
cut -d: -f11,12 `echo -e $1`
}
#/////////////////////////////////////////////////////////////////
edit_add() #edit 选项中的add子函数
{
clear
echo -e "\n Linux Library -\033[1mADD MODE\033[0m"
echo -e "\n Enter the title:\c"
read a b c d e f g h i j #假设输入的书名最多由10个单词组成,这样可以解决含有两个单词以上的书名只录
#入最前面的单词的逻辑错误
title=`echo -e "$a $b $c $d $e $f $g $h $i $j"` #现再将书名赋给title
echo -e "Title:$title:\c" > add_temp #将记录暂存于add_temp中 > 将前面的结果重定向到一文件中,和 >>
#的区别在于, > 要覆盖文件中的原来内容,而 >> 不覆盖,只是追加到文件尾行
echo -e "\n Enter the author:\c"
read a b c d e f g h i j
author=`echo -e "$a $b $c $d $e $f $g $h $i $j"`
echo -e "Author:$author:\c" >> add_temp
echo -e "\n Enter the category:\c"
read a b c d e f g h i j
category=`echo -e "$a $b $c $d $e $f $g $h $i $j"`
echo -e "Category:$category:Status:in:Bname::Date:`date +%Y%m%d`" >> add_temp
display add_temp #以add_temp为参数调用display函数
cat add_temp >> library
rm add_temp
echo -e " Any more to add? (Y)es (N)o>\c"
read yes
case $yes in
[Yy]*) edit_add ;;
[Nn]*) edit ;;
*) echo -e " EXIT NOW"
exit ;;
esac
}
#///////////////////////////////////////////////////////////////////////////////////////
edit_delete()
{
clear
#rm delete_temp temp
echo -e "\n Enter the author/title>\c"
read au_ti
cat library | grep `echo -e $au_ti` > delete_temp
echo -e "\n Linux Library -\033[1mDELETE MODE\033[0m"
string=`cat delete_temp`
if [ -z $string ]
then
echo -e "\n No book's infomation contain this keywords!!!"
echo -e "\n Any more to delete? (Y)es or (N)o?"
read no
case $no in
[Yy]*) edit_delete ;;
[Nn]*) edit ;;
*) echo -e "\n EXIT NOW!!!!"
exit ;;
esac
fi
display delete_temp
rm delete_temp
echo -e "\n Delete this book? (Y)es or (N)o?\c"
read yes
case $yes in
[Yy]*) echo -e "\n Deleting..."
grep -v $au_ti library > temp # sed /$au_ti/d library > temp
mv temp library
echo -e "\n Any more to delete? (Y)es or (N)o?\c"
read no
case $no in
[Yy]*) edit_delete ;;
[Nn]*) edit ;;
*) echo -e "\n EXIT NOW!!!"
exit ;;
esac ;;
[Nn]*) edit ;;
*) echo -e "\n EXIT NOW!!!"
exit ;;
esac
#cp temp library
}
#///////////////////////////////////////////////////////////////////////////////////
edit_display()
{
clear
#rm show_temp
echo -e "\n Enter the author/title>\c"
read au_ti
echo -e " Linux Library -\033[1mDISPLAY MODE\033[0m"
cat library | grep `echo -e $au_ti` > show_temp
display show_temp
rm show_temp
echo -e " Any more to look for? (Y)es N(o)>\c"
read yes
case $yes in
[Yy]*) edit_display ;;
[Nn]*) edit ;;
*) echo -e "\n EXIT NOW!!!"
exit ;;
esac
}
#/////////////////////////////////////////////////////////////////////////////////////////
edit_update()
{
clear
#rm update_temp
echo -e "\n Enter the author/title>\c"
read au_ti
echo -e " Linux Library -\033[1mUPDATE STATUS MODE\033[0m"
cat library | grep `echo -e $au_ti` > update_temp
display update_temp
#cp update_temp test2
out=`cut -d: -f8 update_temp` #查看当前此书的状态并赋给变量out
case $out in
in)
echo -e " New status:out"
echo -e " Enter the borrower's name>\c"
read name
grep -v $name library > temp #sed /$name/d library > temp 先将此书从library中删除,
#余下的信息记录在temp文件中
mv temp library #将temp覆盖library.相当于重命令名temp.
i=`cut -d: -f1,2,3,4,5,6,7 update_temp`
echo -e $i:out:Bname:$name:Date:`date +%Y%m%d` > update_temp
cat update_temp >> library
rm update_temp
echo -e "Any more to update?(Y)es or (N)o?\c"
read no
case $no in
[Yy]*) edit_update ;;
[Nn]*) edit ;;
*) echo -e "\n EXIT NOW!!!"
exit ;;
esac ;;
out)
echo -e " New status:in"
i=`cut -d: -f1,2,3,4,5,6,7 update_temp`
echo -e $i:in:Bname::`date +%Y%m%d` > update_temp
sed /$au_ti/d library > temp
mv temp library
cat update_temp >> library
rm update_temp
echo -e "Any more to update?(Y)es or (N)o?"
read no
case $no in
[Yy]*) edit_update ;;
[Nn]*) edit ;;
*) echo -e "\n EXIT NOW!!!"
exit ;;
esac ;;
esac
}
#/////////////////////////////////////////////////////////////////////////////////////////
report_display()
{
sort -`echo -e $1` -t: library > ti_temp1 #sort -nk2 -t: library 以":"将library分为几列(取决于library中有几
#个:)按第二个:前的关键字进行自然排序
i=`cat ti_temp1 | wc -l` #统计ti_temp1中的行数
j=1
while [ $j -le $i ] #为了使查看时,不出现先出现全部的书名,然后是全部的作者.....
#将一本书的相信息重定向到一临时文件
do
sed -n $j,`echo -e $j`p ti_temp1 > sub_ti_temp #将ti_temp1中的第 $j 行到 $j 行重定向到sub_ti_temp
# $j $j 相等,故只输出第 $j 行
display sub_ti_temp >> ti_temp2 #以display的格式将书本相关信息重定向到ti_temp2
echo -e "\n" >> ti_temp2 #为了美观,每本书之间加一空行
let j=j+1 #用let 命令使 j 增加1
done
cat ti_temp2 | more #用more命令来查看 ti_temp2
rm sub_ti_temp ti_temp*
echo -e "\n Enter to the \033[1mMAIN MENU\033[0m? (Y)es(N)o?\c"
read con
case $con in
[Yy]*) enter_main_menu;;
*)
echo -e "\n EXIT NOW!!!"
exit ;;
esac
}
#////////////////////////////////////////////////////////////////////////////////////////
report()
{
clear
echo -e "\033[1m Linux Library Manager\033[0m"
echo -e " \n\n\n This is the Linux Library applicatipn"
echo -e " Written by TAN"
echo -e "\n\n\n\n Please enter any key to contunue...\c"
read choice
case $choice in
0) enter_main_menu ;;
1) report_display n