900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Linux shell脚本编程-图书管理系统

Linux shell脚本编程-图书管理系统

时间:2024-02-08 05:50:52

相关推荐

Linux shell脚本编程-图书管理系统

Shell脚本编程-图书管理系统

准备的数据库代码块前期准备进入页面主菜单展示图书查找图书添加图书编辑图书借书还书删除图书运行演示主界面展示图书查找图书添加图书编辑图书借书还书

准备的数据库

代码块

前期准备

#!/bin/bash#personal library managerment systemapp_name='{0##*[\\/]}'app_version="1.0"bookdb=${1:-"books.db"}sysName="personal library managerment system"sysVersion="Version:$app_version"sysAuthor="${User}S20-HOULIN"sysDate=$(date +%F)Red=1Green=2Brown=3Blue=4Magenta=5Cyan=6White=7colors=($Red $Green $Grown $blue $Magenta $Cyan $White)Width=$(tput cols)Height=$(tput lines)bold=$(tput bold)rev=$(tput rev)normal=$(tput sgr0)center(){tput cup $1 $(((Width-${#2})/2))}centerMessage(){tput cup $1 $(((Width-$2)/2))}bold(){echo -n ${bold}"$1"${normal}}color(){echo -ne "\e[3${1}m${2}\e[0m"}boldColor(){echo -ne "${bold}\e[3${1}m${2}\e[0m"}error(){errTip='Wrong Input! Press any key to retry...'center 26 "$errTip"; color $Red "$errTip"read -n1 -s key}

进入页面

showFlash(){tput clearcenter 10 "$sysName"; boldColor $Brown "$sysName";center 12 "$sysVersion"; echo -n "$sysVersion";center 14 "$sysAuthor"; color $Green "$sysAuthor";center 16 "$sysDate"; echo -n "$sysDate";message="Press ENTER to continue..."center 20 "$message"; color $Cyan "$message"read -n1 key}

主菜单

showMain(){error=0while :; doif [[ $error -eq 0 ]];thentput clearcenter 6 "Main Menu"; boldColor $Brown "Main Menu";centerMessage 8 30; bold "1: Show books"centerMessage 10 30; bold "2: Find books"centerMessage 12 30; bold "3: Add books"centerMessage 14 30; bold "4: Edit information of books"centerMessage 16 30; bold "5: Check out books"centerMessage 18 30; bold "6: Check in books"centerMessage 20 30; bold "7:Delete books"centerMessage 22 30; bold "q: Exit"message='Please inter your choice(1-7 or q):'inputCol=$(((Width-${#message})/2+${#message}))center 24 "$message"; color $Cyan "$message"firead choicecase $choice in1)showBooks;error=0;;2)findBooks;error=0;;3)addBooks;error=0;;4)editBooks;error=0;;5)checkOut;error=0;;6)checkIn;error=0;;7)deleteBooks;error=0;;[qQ]*)tput clear;exit 0;;*)error $((infoHeight+2))tput cup $((infoHeight)) $inputColtput ed;error=1;;esacdone}

展示图书

showBooks(){error=0while :; doif [ $error -eq 0 ];thentput clearcenter 6 "Show books"; boldColor $Brown "Show books"centerMessage 8 30; bold "1:show books that not borrowed"centerMessage 10 30; bold "2: show books that borrowed"centerMessage 12 30; bold "3: show all books"centerMessage 14 30; bold "q: Main Menu"message='enter your choice(1-3 or q):'inputCol=$(((Width-${#message})/2+${#message}))center 24 "$message";color $Cyan "$message"firead choicecase $choice in1)showBook 1;;2)showBook 2;;3)showBook 3;;[qQ]*)tput clear;return 0;;*)error $((infoHeight+2))tput cup $((infoHeight)) $inputColtput ed;error=1;;esacdone}showBook(){if [ ! -f $bookdb ];thenecho "The book database is empty."return 1firesult=yuntil [ "$result" = n ];dotput clearcenter 6 "Show book";boldColor $Brown "Show books"case $1 in1)showIn;;2)showOut;;3)showAll;;esacbackmessage="BACK"center 24 "$backmessage";color $Cyan "$backmessage"read resultcase $result in*)tput clear;return 0;;esacdone}showIn(){tput cup 9 50; boldColor $Blue "ID"tput cup 9 65; boldColor $Blue "Title"tput cup 9 95; boldColor $Blue "Author"tput cup 9 110;boldColor $Blue "Tags"awk 'BEGIN{i=10;FS = "%"}$5~/in/{system("tput cup "i" 50");print $1;system("tput cup "i" 65"); print $2;system("tput cup "i" 95"); print $3;system("tput cup "i" 110");print $4;i++}' $bookdb}showOut(){tput cup 9 30; boldColor $Blue "ID"tput cup 9 45; boldColor $Blue "Title"tput cup 9 75; boldColor $Blue "Author"tput cup 9 90; boldColor $Blue "Tags"tput cup 9 110;boldColor $Blue "Borrower"tput cup 9 130;boldColor $Blue "Time"awk 'BEGIN{i=10;FS = "%"}$5~/out/{system("tput cup "i" 30");print $1;system("tput cup "i" 45"); print $2;system("tput cup "i" 75"); print $3;system("tput cup "i" 90"); print $4;system("tput cup "i" 110");print $6;system("tput cup "i" 130");print $7;i++}' $bookdb}showAll(){tput cup 9 25; boldColor $Blue "ID"tput cup 9 40; boldColor $Blue "Title"tput cup 9 70; boldColor $Blue "Author"tput cup 9 85; boldColor $Blue "Tags"tput cup 9 105;boldColor $Blue "Status"tput cup 9 120;boldColor $Blue "Borrower"tput cup 9 135;boldColor $Blue "Time"awk 'BEGIN{i=10;FS = "%"}{system("tput cup "i" 25");print $1;system("tput cup "i" 40"); print $2;system("tput cup "i" 70"); print $3;system("tput cup "i" 85"); print $4;system("tput cup "i" 105");print $5;system("tput cup "i" 120");print $6;system("tput cup "i" 135");print $7;i++}' $bookdb}

查找图书

findBooks(){if [ ! -f $bookdb ];thenecho "The book database is empty."return 1fierror=0while :; doif [ $error -eq 0 ];thentput clearcenter 6 "Find books"; boldColor $Brown "Find books"centerMessage 8 20; bold "1: Find by bookId"centerMessage 10 20; bold "2:Find by title"centerMessage 12 20; bold "3: Find by author"centerMessage 14 20; bold "4:Find by tags"centerMessage 16 20; bold "q: Main Menu"message='enter your choice(1-3 or q):'inputCol=$(((Width-${#message})/2+${#message}))center 24 "$message";color $Cyan "$message"firead choicecase $choice in1)byBookId;;2)byTitle;;3)byAuthor;;4)byTags;; [qQ]*)tput clear;return 0;;*)error $((infoHeight+2))tput cup $((infoHeight)) $inputColtput ed;error=1;;esacdone}byBookId(){error=0while :; doif [ $error -eq 0 ];thentput clearcenter 6 "Find books"; boldColor $Brown "Find books"message='Please input the bookId!'center 10 "$message";color $Cyan "$message"fitput cup 13 $((Width/2-5))read bookIdtput clearcenter 6 "Find books"; boldColor $Brown "Find books"tput cup 9 50; boldColor $Blue "ID"tput cup 9 65; boldColor $Blue "Title"tput cup 9 95; boldColor $Blue "Author"tput cup 9 105; boldColor $Blue "Tags"awk -v bookId=$bookId 'BEGIN{i=11;FS = "%"}$1~bookId{system("tput cup "i" 50");print $1;system("tput cup "i" 65");print $2;system("tput cup "i" 95");print $3;system("tput cup "i" 105");print $4;i++}' $bookdbbackmessage="BACK"center 24 "$backmessage";color $Cyan "$backmessage"read resultcase $result in*)tput clear;return 0;;esacdone}byTitle(){error=0while :; doif [ $error -eq 0 ];thentput clearcenter 6 "Find books"; boldColor $Brown "Find books"message='Please input the title!'center 10 "$message";color $Cyan "$message"fitput cup 13 $((Width/2-10))read titletput clearcenter 6 "Find books"; boldColor $Brown "Find books"tput cup 9 50; boldColor $Blue "ID"tput cup 9 65; boldColor $Blue "Title"tput cup 9 95; boldColor $Blue "Author"tput cup 9 105; boldColor $Blue "Tags"awk -v title=$title 'BEGIN{i=11;FS = "%"}$2~title{system("tput cup "i" 50");print $1;system("tput cup "i" 65");print $2;system("tput cup "i" 95");print $3;system("tput cup "i" 105");print $4;i++}' $bookdbbackmessage="BACK"center 24 "$backmessage";color $Cyan "$backmessage"read resultcase $result in*)tput clear;return 0;;esacdone}byAuthor(){error=0while :; doif [ $error -eq 0 ];thentput clearcenter 6 "Find books"; boldColor $Brown "Find books"message='Please input the author!'center 10 "$message";color $Cyan "$message"fitput cup 13 $((Width/2-5))read authortput clearcenter 6 "Find books"; boldColor $Brown "Find books"tput cup 9 50; boldColor $Blue "ID"tput cup 9 65; boldColor $Blue "Title"tput cup 9 95; boldColor $Blue "Author"tput cup 9 105; boldColor $Blue "Tags"awk -v author=$author 'BEGIN{i=11;FS = "%"}$3~author{system("tput cup "i" 50");print $1;system("tput cup "i" 65");print $2;system("tput cup "i" 95");print $3;system("tput cup "i" 105");print $4;i++}' $bookdbbackmessage="BACK"center 24 "$backmessage";color $Cyan "$backmessage"read resultcase $result in*)tput clear;return 0;;esacdone}byTags(){error=0while :; doif [ $error -eq 0 ];thentput clearcenter 6 "Find books"; boldColor $Brown "Find books"message='Please input the tags!'center 10 "$message";color $Cyan "$message"fitput cup 13 $((Width/2-5))read tagstput clearcenter 6 "Find books"; boldColor $Brown "Find books"tput cup 9 50; boldColor $Blue "ID"tput cup 9 65; boldColor $Blue "Title"tput cup 9 95; boldColor $Blue "Author"tput cup 9 105; boldColor $Blue "Tags"awk -v tags=$tags 'BEGIN{i=11;FS = "%"}$4~tags{system("tput cup "i" 50");print $1;system("tput cup "i" 65");print $2;system("tput cup "i" 95");print $3;system("tput cup "i" 105");print $4;i++}' $bookdbbackmessage="BACK"center 24 "$backmessage";color $Cyan "$backmessage"read resultcase $result in*)tput clear;return 0;;esacdone}

添加图书

addBooks(){tput clearany="y"while [ "$any" = "y" ];docenter 10 "Add books";boldColor $Brown "Add books"tput cup 13 $((Width/2-10)); boldColor $Magenta "ID: "tput cup 15 $((Width/2-10)); boldColor $Magenta " Title: "tput cup 17 $((Width/2-10)); boldColor $Magenta " author: "tput cup 19 $((Width/2-10)); boldColor $Magenta " Tags: "tput cup 13 $((Width/2)); autoIdtput cup 15 $((Width/2)); read Titletput cup 17 $((Width/2)); read Authortput cup 19 $((Width/2)); read Tagssuremessage="Are you sure? [y(es)/n(0)/c(ancel)]: "center 22 "$suremessage";color $Cyan "$suremessage"read sureBookId=$(autoId)case $sure in[yY])echo "$BookId%$Title%$Author%#$Tags%in%" >>$bookdb;;[nN])continue;;[cC]|*)tput clear;return 0;;esacanymessage="Add anymore book?[y(es)/n(o)]:"center 24 "$anymessage";color $Cyan "$anymessage"read anydone}autoId(){new=`awk 'BEGIN{FS="%"}{last=$1}END{print last}' $bookdb | awk 'BEGIN{FS="-"}{print $2+1}'`if [ ${#new} -eq 1 ];thennew="S20-0000$new"elif [ ${#new} -eq 2 ];thennew="S20-000$new"elif [ ${#new} -eq 3 ];thennew="S20-00$new"elif [ ${#new} -eq 4 ];thennew="S20-0$new"elsenew="S20-$new"fiecho "$new"}

编辑图书

editBooks(){tput clearany="y"while [ "$any" = "y" ];docenter 6 "Edit books";boldColor $Brown "Edit books"centerMessage 10 16;bold "BookId:"centerMessage 13 50;bold " Title:"centerMessage 16 50;bold "Author:"centerMessage 19 50;bold " Tags:"tput cup 10 $((Width/2));read bookIdBookId="S20.*"+"$bookId"tput cup 10 $((Width/2))awk -v BookId=$BookId 'BEGIN{FS="%"}$1~BookId{print $1}' $bookdbtput cup 13 $((Width/2-15))awk -v BookId=$BookId 'BEGIN{FS="%"}$1~BookId{print $2}' $bookdboldTitle=`awk -v BookId=$BookId 'BEGIN{FS="%"}$1~BookId{print $2}' $bookdb`tput cup 16 $((Width/2-15))awk -v BookId=$BookId 'BEGIN{FS="%"}$1~BookId{print $3}' $bookdboldAuthor=`awk -v BookId=$BookId 'BEGIN{FS="%"}$1~BookId{print $3}' $bookdb`tput cup 19 $((Width/2-15))awk -v BookId=$BookId 'BEGIN{FS="%"}$1~BookId{print $4}' $bookdboldTags=`awk -v BookId=$BookId 'BEGIN{FS="%"}$1~BookId{print $4}' $bookdb`tput cup 13 $((Width/2+5)); boldColor $Blue "==>"tput cup 16 $((Width/2+5)); boldColor $Blue "==>"tput cup 19 $((Width/2+5)); boldColor $Blue "==>" tput cup 13 $((Width/2+15));read Titletput cup 16 $((Width/2+15));read Authortput cup 19 $((Width/2+15));read TagsnewTitle=${Title:-$oldTitle}newAuthor=${Author:-$oldAuthor}newTags=${Tags:-$oldTags}suremessage="Are you sure? [y(es)/n(0)/c(ancel)]: "center 24 "$suremessage";color $Cyan "$suremessage"read surecase $sure in[yY])sed -i "/S20.*$bookId/{s@$oldTitle@$newTitle@;s@$oldAuthor@$newAuthor@;s@$oldTags@$newTags@}" $bookdb;;[nN])continue;;[cC]|*)tput clear;return 0;;esacanymessage="Edit anymore book?[y(es)/n(o)]:"center 26 "$anymessage";color $Cyan "$anymessage"read anydone}

借书

checkOut(){tput clearany="y"while [ "$any" = "y" ];docenter 6 "Check out books";boldColor $Brown "Check out books"tput cup 9 40; boldColor $Blue "ID"tput cup 9 55; boldColor $Blue "Title"tput cup 9 85; boldColor $Blue "Author"tput cup 9 100; boldColor $Blue "Tags"inMessagetput cup 23 80; boldColor $Blue " bookId:"tput cup 24 80; boldColor $Blue "Borrower:"tput cup 23 90; read bookIdtput cup 24 90; read Borrowersuremessage="Are you sure? [y(es)/n(0)/c(ancel)]: "center 25 "$suremessage";color $Cyan "$suremessage"read surecase $sure in[yY])sed -i "/^$bookId/s#%in%%#%out%$Borrower%$sysDate#" $bookdb;;[nN])continue;;[cC]|*)tput clear;return 0;;esacanymessage="Borrow anymore book?[y(es)/n(o)]:"center 26 "$anymessage";color $Cyan "$anymessage"read anydone}inMessage(){awk 'BEGIN{i=11;FS = "%"}$5~/in/{system("tput cup "i" 40");print $1;system("tput cup "i" 55");print $2;system("tput cup "i" 85");print $3;system("tput cup "i" 100");print $4;i++}' $bookdb}

还书

checkIn(){tput clearany="y"while [ "$any" = "y" ];docenter 6 "Check In books";boldColor $Brown "Check In books"tput cup 9 40; boldColor $Blue "ID"tput cup 9 55; boldColor $Blue "Title"tput cup 9 85; boldColor $Blue "Author"tput cup 9 100; boldColor $Blue "Tags"outMessagetput cup 23 80; boldColor $Blue " bookId:"tput cup 23 90; read bookIdsuremessage="Are you sure? [y(es)/n(0)/c(ancel)]: "center 25 "$suremessage";color $Cyan "$suremessage"read surecase $sure in[yY])sed -i "/^$bookId/s#%out%.*#%in%%#" $bookdb;;[nN])continue;;[cC]|*)tput clear;return 0;;esacanymessage="Return anymore book?[y(es)/n(o)]:"center 26 "$anymessage";color $Cyan "$anymessage"read anydone}outMessage(){awk 'BEGIN{i=11;FS = "%"}$5~/out/{system("tput cup "i" 40");print $1;system("tput cup "i" 55");print $2;system("tput cup "i" 85");print $3;system("tput cup "i" 100");print $4;i++}' $bookdb}

删除图书

deleteBooks(){tput clearany="y"while [ "$any" = "y" ];docenter 10 "Delete books";boldColor $Brown "Delete books"deletemessage="please input the book ID that you want to delete:"center 15 "$deletemessage"; boldColor $Magenta "$deletemessage"centerMessage 17 10; echo -n "${bold}";read bookIdsuremessage="Are you sure? [y(es)/n(0)/c(ancel)]: "center 22 "$suremessage";color $Cyan "$suremessage"read surecase $sure in[yY])sed -i "/$bookId/d" $bookdb;;[nN])continue;;[cC]|*)tput clear;return 0;;esacanymessage="Delete anymore book?[y(es)/n(o)]:"center 24 "$anymessage";color $Cyan "$anymessage"read anydone}

运行

showFlashshowMain

运行就最后执行这两个函数就行了,各位小伙伴应该懂的。

演示

主界面

在Linux终端执行脚本进入下方页面

任意键进入主菜单

展示图书

1.展示没被借走的

2.展示借走的

3.展示所有图书

查找图书

1.通过书号查找

2.通过书名查找

3.通过作者查找

4.通过标签查找

添加图书

自动生成书号

编辑图书

书号位置输入书号最后的数字,回车后展示这本书的信息

借书

只展示没被借走的图书

还书

只展示被借走了的图书

删除图书

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。