文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>一个shell编程题练习

一个shell编程题练习

时间:2007-12-16  来源:clyet

一道shell编程题拿来做练习,初学awk,写的乱七八糟,但还算是做出来了:)
原题连接
http://bbs.chinaunix.net/thread-1020087-1-6.html

下面是代码

#!/bin/bash

export TITLE=title
export AUTHOR=author
export CATEGORY=category
export STATUS=in
export BNAME=none
export DATE=`date +%Y/%m/%d`

#add a book
add_mode(){
clear
read -p "UNIX library ----ADD MODE

Title: " TITLE
read -p "Author: " AUTHOR
read -p "Category: " CATEGORY
echo "$TITLE:$AUTHOR:$CATEGORY:$STATUS:$BNAME:$DATE" >> library
read -p "Any more to add? Yes or No >" YORN
if [ $YORN = "y" -o $YORN = "Y" ]; then
add_mode
else
lib_mng
fi
}

#delete a book
del_mode(){
clear
read -p "delete: " DELNAME
awk -v DEL="$DELNAME" 'BEGIN{FS=":"}{if(DEL==$1) print "UNIX library ----DELETE MODE\n" \
"Title:",$1"\n" "Author:",$2"\n" "Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library
read -p "Delete this book? Yes or No >" YON1
if [ $YON1 = "Y" -o $YON1 = "y" ];then
sed -i '/^'$DELNAME'/d' library
read -p "Any more to del? Yes or No >" YON2
if [ $YON2 = "Y" -o $YON2 = "y" ];then
del_mode
else
lib_mng
fi
else
lib_mng
fi
}

#display a book
display_mode(){
clear
DISINFO="can not find this book"
read -p "display: " DISNAME
for i in $(awk 'BEGIN{FS=":"} {print $1}' library)
do
if [ $i = $DISNAME ];then
DISINFO=`awk -v DIS="$DISNAME" 'BEGIN{FS=":"}{if(DIS==$1) print "UNIX library ----Display MODE\n" \
"Title:",$1"\n" "Author:",$2"\n" "Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library`
#DISINFO="haha"
#break
fi
done
echo $DISINFO
read -p "Any more to look for?Yes or No>" YON3
if [ $YON3 = "Y" -o $YON3 = "y" ];then
display_mode
else
lib_mng
fi
}

#update a book's status
update_mode(){
clear
read -p "title: " UPNAME
awk -v UP="$UPNAME" 'BEGIN{FS=":"}{if(UP==$1) print "UNIX library ----UPDATE MODE\n" \
"Title:",$1"\n" "Author:",$2"\n" "Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library
STATUS=`awk -v UP="$UPNAME" 'BEGIN{FS=":"}{if(UP==$1) print $4}' library`
if [ $STATUS = "in" ];then
read -p "studentname is: " BNAME
DATE=`date +%Y/%m/%d`
awk -v UP="$UPNAME" 'BEGIN{FS=OFS=":"}{if(UP==$1) {$4="out";$5="'$BNAME'";$6="'$DATE'"};print >"library"}' library
echo "New status: out"
else
DATE=`date +%Y/%m/%d`
awk -v UP="$UPNAME" 'BEGIN{FS=OFS=":"}{if(UP==$1) {$4="in";$5="none";$6="'$DATE'"};print >"library"}' library
echo "New status: in"
fi
read -p "Any more to update?Yes or No>" YON4
if [ $YON4 = "Y" -o $YON4 = "y" ];then
update_mode
else
lib_mng
fi
}

#report library's status
report_mode(){
clear
awk 'BEGIN{FS=":"}{print "UNIX library ----DELETE MODE\n" "Title:",$1"\n" "Author:",$2"\n" \
"Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library | more
lib_mng
}

#master mode
lib_mng(){
clear
read -p "UNIX library manager
0.exit this program
1.add a book
2.delete a book
3.display a book
4.update a book's status
5.report library's status

Input your choice: " NUM
case $NUM in
     "0")
     exit 1
     ;;
     "1")
     add_mode
     ;;
     "2")
     del_mode     
     ;;
     "3")
     display_mode
     ;;
     "4")
     update_mode
     ;;
     "5")
     report_mode
     ;;
     *)
     echo "Invalid chioce,press any key to try again"
     ;;
esac
}
lib_mng
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载