1
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 "
}

我想显示“书已经存在”,以防输入文件中类似名称的书

4

2 回答 2

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
于 2013-01-23T11:32:44.800 回答
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 "
}
于 2013-11-18T12:45:07.510 回答