分享
 
 
 

lINUX SHELL --- A Smart Library

王朝system·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

A smart Library

#

# This is a smart library designed in shell under linux enverionment

# LINUX Library: This program is the main driver for the LINUXlibrary

# application program . It shows a brief startup messagr

# and then dispalys the main menu.It invokes the appropriate

# program according to the user selection.

#

PATH=$PATH:.

BOLD=`tput smso` # store code for bold mode in BOLD

NORMAL=`tput rmso` # store code for end of the

# bold mode in NORMAL

export BOLD NORMAL # make them recongized by subshells

# -------------------------------add_record------------------------------

#

# LINUX Library

# add_record: This program adds a record to the library file (L_LIB).

# It asks the title , author, and category of the book. After

# adding the information to the -ULIB_FILE file, it prompts

# the user for the next record.

#

add_record(){

answer=y

while [ "$answer" = y ]

do

tput clear

tput cup 05 10

echo -e " LINUX Library . ${BOLD} ADDMODE"

echo -e " ${NORMAL}"

tput cup 07 23

echo " Title: "

tput cup 09 22

echo " Author: "

tput cup 11 20

echo " Catagory: "

tput cup 13 20

echo "sys: system, ref: referance, tb: textbook"

tput cup 14 10

echo -e " Any more to add ? (Y)es or (N)o > _\b\c"

read answer

case $answer in

[Yy]*)

answer=y

;;

[Nn]*)

answer=n

;;

esac

done

continue

}

# -------------------------------update_record---------------------

#

# LINUX Library

# update_record: This program update the status of a specified book. It

# asks the Author/Title of the book, and changes the status

# of the specified book from in (check in) to out (check out),

# or from out to in. If the book is notfound in the file, and

# an error message is displayed.

#

update_record(){

OLD_IFS="$IFS"

answer=y

while [ "$answer" = y ]

do

new_status=

new_bname=

new_date=

# declare empty varible

tput clear

tput clear

tput cup 03 05

echo -e " Enter the Author/Title > -\b\c"

read response

grep -i "$response" LLIB_FILE < TEMP

if [ -s TEMP ]

then

IFS=""

read title author category status bname date < TTEMP

tput cup 05 10

echo -e " LNIUX Library . ${BOLD} UPDATESTATUSMODE ${NORMAL}"

tput cup 07 23

echo " Title : $title"

tput cup 08 22

echo " Author: $author"

case $category in

[Tt][Bb])

word=textbook

;;

[Ss][Yy][Ss])

word=system

;;

[Rr][Ee][Ff])

word=referance

;;

*)

word=undefined

;;

esac

tput cup 09 20

echo " Category: $word" # display the category

tput cup 10 22

echo " Ststus: $status" # display the status

if [ "$status" = "in" ] # if it check in

then

new_status=out # indicate the status

tput cup 11 18

echo " New status: $status"

tput cup 12 14

echo -e " Check out by: _ \b\c"

read new_bname

new_date=`date+%D`

tput cup 13 24

echo " Date: $new_date"

else

new_status=in

tput cup 11 14

echo " Check out by: $bname"

tput cup 12 24

echo " Date: $date"

tput cup 15 18

echo " New status: $new_status"

fi

grep -iv "$status: $author: $category: $status: $bname: $date" LLIB_FILE

>TEMP

cp TEMP LLIB_FILE

echo "$title : $author: $category: $dtatus: $bname: $date" >> LLIB_FILE

else

tput cup 7 10

echo " $response not found"

fi

tput cup 16 10

echo -e " Any more to update ? (Y)es or (N)o > _\b\c"

read answer

case $answer in

[Yy]*)

answer=y

;;

*)

answer=n

;;

esac

done

IFS="$OLD_IFS"

rm TEMP TTEMP

continue

}

# -----------------------------display_recorde----------------------

#

# LINUX Library

# display_recorde: This program display a specified record from the

# LLIB_FILE.It asks the Author/Title of the book, and

# display the specifiedbook is not found in the file.

#

display_record(){

OLD_IFS="$IFS" # save the IFSsetting

answer=y # initialize the answer to indicate yes

while [ "$answer" = y ] # as long as the answer is yes

do

tput clear

tput cup 03 05

echo -e " Enter the Author/Title> _\b\c"

read response

grep -i "$response" LLIB_FILE > TEMP # find the specified book in the file

if [ -s TEMP ] # if it is found

then

IFS=":" # set the IFS to colon

read title author category status bname date < TEMP

tput cup 05 10

echo -e "LINUX Library . ${BOLD} DISPLAYMODE ${NORMAL}"

tput cup 07 23

echo " Title: $title"

tput cup 08 22

echo " Author: $author"

case $category in

[Tt][Bb])

word=textbook

;;

[Ss][Yy][Ss])

word=system

;;

[Rr][Ee][Ff])

word=referance

;;

*)

word=undefined

;;

esac

tput cup 09 20

echo -e " Category: $word" # display the category

tput cup 10 22

echo -e " Status: $status"

if [ "$status" = "out" ]

then

tput cup 11 14

echo " Check out by: $bname"

tput cup 12 24

echo " Date: $date"

fi

else

tput cup 07 10

echo " $response not found "

fi

tput cup 15 10

echo -e "Any more to look for ? (Y)es or (N)o > _\b\c"

read answer

case $answer in

[Yy]*)

answer=y

;;

*)

answer=n

;;

esac

done # end of the while loop

IFS="$OLD_IFS" # restor the IFSto its orgiginal value

continue

}

# ----------------------------delete_record--------------------------

#

# LINUX Library

# delete_record: This program deletes a specified record from the

# LLIB_FILE. It asks the Author/Title of the book,

# and displays the specifiedbook, and deletes it after

# confirmation, or shows an error message.

#

delete_record(){

OLD_IFS="$IFS" # save the IFSsetting

answer=y

# initialize the answer to indicate yes

while [ "$answer" = y ] # as long as the answer is yes

do

sleep 1

tput clear

tput cup 03 05

echo -e " Enter the Author/Title> _\b\c"

read response

grep -i "$response" LLIB_FILE > TEMP # find the specified book in the file

if [ -s TEMP ] # if it is found

then

IFS=":" # set the IFS to colon

read title author category status bname date < TEMP

tput cup 05 10

echo -e "LINUX Library . ${BOLD} DELETEMODE ${NORMAL}"

tput cup 07 23

echo " Title: $title"

tput cup 08 22

echo " Author: $author"

case $category in

[Tt][Bb])

word=textbook

;;

[Ss][Yy][Ss])

word=system

;;

[Rr][Ee][Ff])

word=referance

;;

*)

word=undefined

;;

esac

tput cup 09 20

echo -e " Category: $word" # display the category

tput cup 10 22

echo -e " Status: $status"

if [ "$status" = "out" ]

then

tput cup 11 14

echo " Check out by: $bname"

tput cup 12 24

echo " Date: $date"

fi

tput cup 13 20

echo -e " Delete thsi book ? (Y)es or (N)o > _\b\c"

read answer

if [ $answer y ] || [ $answer = Y ] # test for Y or y

then

grep -iv "$title: $author: $category: $bname: $date" LLIB_FILE

> TEMP

mv TEMP LLIB_FILE

fi

else

tput cup 14 10

echo " $response not found"

fi

tput cup 15 10

echo -e " Any more to delete ? (Y)es or (N)o > _\b\c"

case $answer in

[Yy]*)

answer=y

;;

*)

answer=n

;;

esac

done # end of the while loop

IFS="$OLD_IFS" # restor the IFSto its orgiginal value

continue

}

# -----------------------------show_error--------------------------

# LINUX Library

# show_error: This program display an error message and waits for

# userinput to continue. It dispalys the message at the

# specifiled row and column.

#

show_error(){

tput cup $1 $2 # place the cursor on the screen

echo -e " \07Wrong Input. Try again." # show the error message

row=`expr $1 + 2`

tput cup $row $2

echo -e " Press any key to continue...> _ \b\c" # display the prompt

read answer

continue

}

# -------------------------report_recorde_no-----------------------

#

# LINUX Library

# report_record_no: This program produces reports from the LLIB_FILE

# file It checks for the report number passed to it

# on the command line, sorts and produces reports

# accordingly.

#

report_record_no(){

IFS=":"

case $1 in

1)

sort -f -d -t : LLIB_FILE > TEMP

;;

2)

sort -f -d -t : +1 LLIB_FILE > TEMP

;;

3)

sort -f -d -t : +2 LLIB_FILE > TEMP

;;

esac

#

# read records from the sorted file TEMP. Format and store

# them in PTEMP

#

while read title author category status bname date

do

echo -e "\t Title: $title" >> PTEMP

echo -e "\t Author: $author" >> PTEMP

case $category in

[Tt][Bb] )

word=textbook

;;

[Ss][Yy][Ss] )

word=system

;;

[Rr][Ee][Ff] )

word=referande

;;

*)

word=undefined

;;

esac

echo -e "\t Category: $word" >> PTEMP # format author

echo -e "\t Status: $status " >> PTEMP

if [ "$status" = "out" ]

then

echo -e "\t Check out by: $bname" >> PTEMP

echo -e "\t Date: $date \n" >> PTEMP

fi

echo >> PTEMP

done

tput clear

more -14 PTEMP

rm TEMP PTEMP

continue

}

# ----------------------------edit_record-------------------------

#

# LINUX Library

# edit_record: This program is the main driver for the EDIT program.

# It shows the EDIT menu and invokes the appropriate

# program according to the user selection.

#

edit_record(){

error_flag=0 # inilitalize the error flag, indicating no error.

while true

do

if [ $error_flag -eq 0 ] # check for the error

then

tput clear

tput cup 05 07

echo -e " LINUX Library . ${BOLD} EDIT MENU ${NORMAL}"

tput cup 07 20

echo -e " 0 : ${BOLD} RETURN ${NORMAL}to the Main Menu"

tput cup 09 20

echo -e " 1: ${BOLD} ADD ${NORMAL}"

tput cup 11 20

echo -e " 2: ${BOLD} UPDATESTATUS ${NORMAL}"

tput cup 13 20

echo -e " 3: ${BOLD} DISPLAY ${NORMAL}"

tput cup 15 20

echo -e " 4: ${BOLD} DELETE ${NORMAL}"

fi

error_flag=0 # reset the error flag

tput cup 17 20

echo -e " Enter yout chioce> _ \b\c"

read choice

#

# This case constuct for checking the user selection

#

case $choice in

0)

show_gui

;;

1)

add_record # call EDIT progran

;;

2)

update_record # call UPDATE program

;;

3)

display_record # call DISPALY program

;;

4)

delete_record # call DELETE program

;;

*)

show_error 20 10 # call the ERROR program

tput cup 20 01

tput ed # reset the cursor

error_flag=1 # set the error flag to indicate

;;

esac

done

continue

}

# ------------------------mutil_report_record---------------------

#

# LINUX Library

# mutil_report_record: This program is hte main for the REPORTS menu.

# It shows the reports menu and invokes the

# approriate program accroding to the user

# selection.

#

mutil_report_record(){

error_flag=0 # initialize the error flag.

while true

do

if [ $error_flag -eq 0 ] # check for the error

then

tput clear

tput cup 05 10

echo -e " LINUX Library . ${BOLD} REPORTSMENU ${NORMAL} "

tput cup 07 20

echo -e " 0: ${BOLD} RETURN ${NORMAL} to the Main Menu"

tput cup 9 20

echo -e " 1: Stored by ${BOLD} TITLES ${NORMAL} "

tput cup 11 20

echo -e " 2: Stored by ${BOLD} AUTHORS ${NORMAL} "

tput cup 13 20

echo -e " 3: Stored by ${BOLD} CATEGORY ${NORMAL} "

fi

error_flag=0

tput cup 17 10

echo -e " Enter your chioce > _ \b\c"

read choice

#

# This case construct for checking the user selection.

#

case $choice in # check user input

0)

show_gui

;;

1)

report_record_no 1

;;

2)

report_record_no 2

;;

3)

report_record_no 3

;;

*)

show_error 20 10

tput cup 20 01

tput ed

error_flag=1

;;

esac

done

}

# ------------------------------show_prompts--------------------------

#

# LNIUX Library

# show_prompts: This program will show some prompts of the LIB application

# and then the user press any key to continue.

#

show_prompts(){

tput clear # clear screem

tput cup 10 10

echo -e " ${BOLD}Super Duper LINUX Library" # show the title in bold

tput cup 12 10

echo -e "${NORMAL} This is the LINUX Library application"

tput cup 14 10

echo -e " Please enter any key to continue..._\b\c"

read answer # read the user input

}

# -------------------------------show_gui-----------------------------

#

# Show the title and a brief message befor showing the main meun.

#

show_gui(){

error_flag=0 # initialize the error flag,

# indicating no error

while true

do

if [ $error_flag -eq 0 ] # check for the error

then

tput clear

tput cup 5 10

echo " LINUX Library . ${BOLD}MAIN MENU ${NORMAL}"

tput cup 7 20

echo " 0: ${BOLD} EXIT ${NORMAL} this program"

tput cup 9 20

echo " 1: ${BOLD} EDITE ${NORMAL} Menu"

tput cup 11 20

echo " 2: ${BOLD} REPORTS ${NORMAL} Menu"

error_flag=0 # reset the error flag

fi

tput cup 13 10

echo -e " Entry your choice > _\b\c "

read choice # read user choice

#

# This case construct for checking the user selection.

#

case $choice in # check user input

0)

tput clear

exit 0

;;

1)

edit_recorde

continue

;;

2)

mutil_report_recorde

continue

;;

*)

show_error 20 10

;;

esac

done

contune

}

# --------------------------------main---------------------------------

show_prompts

show_gui

# --------------------------------end----------------------------------

虽然shell不是linux/unix/red hat等unix OS (Operating System)家族的核心组成部分,但是shell绝对是一功能强大的工具。在shell环境下,你可以编写很小的,很smart的程序,也可以编写大型的脚本。通过shell你可以完成计算机高级语言,比如C,所能实现的功能。而且很多时候,你的shell代码要比用C等高级程序开发语言编写的具有相同功能的程序的代码更简洁、更简单、更灵活、更实用。当然了,shell的最大的缺陷就是他只能在UNIX族操作系统上得到应用。它具有和windows操作系统下的.bat一样的实用性。Shell给用户一个执行操作命令的接口外就是这个功能强大的编辑、解释脚本的功能了。

Shell提供了一整套的与之相匹配的语法。之中有while语句,也有if、case、until、for 等结构,但是其实现法方式很高级语言中的定义出入很大。就拿for 语句为例吧。

for 的接单语言结构:

for variable in values

do

statements

done (注意:带黑体的是关键字)

我们可以利用for结构来处理一组可以由任意的字符串组成的集合。在程序里可以简单的把全体字符串豆类出来,更常见的做法是把它与shell对文件名的通配符扩展结合在一起使用。

下面是使用固定字符串的for循环脚本:

#! /bin/sh

for prempt in Hello world I am Today

do

echo $prempt

done

exit 0

脚本的运行结果为:

Hello

world

I

am

Today

如果把for prempt in Hello world I am Today改为for prempt in "Hello world I am Today" 则程序的圆形结果将是:Hell world I am Today.加上引号是在字符串里保留空客的唯一办法了。

在shell里也许用户编写一般的脚本之外,还也许用户定义函数,如果你需要编写较大的脚本程序时,使用自定义函数比较明智的做法,因为你可以通过函数来构造自己喜欢的代码。

在shell里定义函数的方式很简单,只要写出函数的名字,然后在后面加一组空括号"()",最后添加一对花括号就行了。如下:

function_name(){

statements

}

在程序中涉及函数调用时,只需要调用函数的名字就行了。很简单吧。还想提一点建议(这对初接触shell的朋友可能会有一点用处):考虑到程序的可移植性,你可以用printf语句代替echo语句。因为printf是国际自由软件基金会(Free Software Foundation)的GNU项目的一个标准,在不同版本的UNIx下都有这一条语句。而echo则不同,在不同的厂商的产品中实现方式可能会不一样。

本文旨在于给初学shell的朋友有一点指示和建议,希望能够给大家一点帮助。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有