库存簿管理中的shell编程

时间:2013-01-23 11:22:09

标签: bash shell

function add_new_book
{
  echo "1) add_new_book"

  echo -n "Title :"
    read title
  echo -n "Author :"
    read author
  echo -n "Price :"
    read price
  echo -n "Qty Available  : "
    read available 
  echo -n "Qty Sold : "
    read sold 


  echo $title:$author:$price:$available:$sold >> BookDB.txt
  echo $BookDB "New book title ' $title ' added successfully "
}

如果输入与文件名称相似的书,我想显示“已预订的书”

2 个答案:

答案 0 :(得分:0)

在添加图书之前,请检查它是否存在:

     grep "$title" BookDB.txt >/dev/null 2>&1
     rc=$?

     if [[ "$rc" != 0 ]]; then
        echo $title:$author:$price:$available:$sold >> BookDB.txt
        echo $BookDB "New book title ' $title ' added successfully "
     else
        echo "The book $title already exists."
     fi

答案 1 :(得分:0)

function add_new_book
{
  echo "1) add_new_book"
  echo -n "Title :"
    read title
    grep -iqs "^$title:" BookDB.txt && echo "THE BOOK ALREADY EXISTS" && return

  echo -n "Author :"
    read author
  echo -n "Price :"
    read price
  echo -n "Qty Available  : "
    read available 
  echo -n "Qty Sold : "
    read sold 
  echo $title:$author:$price:$available:$sold >> BookDB.txt
  echo $BookDB "New book title ' $title ' added successfully "
}