lINUX SHELL --- A Smart Library

A  smart  Library

##################################

@Author : Gui Quan Zhang

@Contract at : School of Software, YunNanUniversity

##################################


# 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的朋友有一点指示和建议,希望能够给大家一点帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值