如何更新书名?

时间:2015-01-22 07:54:24

标签: linux bash shell ubuntu

好的..我在更新新书名时遇到了麻烦..我已经在这个网站上搜索了解决方案..我已经尝试过了,但没有一个能够正常工作..

function update_book
{
   #echo "Title: "
   read -p $'Title: ' updatetitle
   #echo "Author: "
   read -p $'Name: ' updatename

  if grep -Fq "${updatetitle}:${updatename}" BookDB.txt
   then
      echo "Book found!"

      if ! [ -f BookDB.txt ] ; then 
touch BookDB.txt
fi

selection=0
until [ "$selection" = "f" ]; do
      echo ""
      echo ""
      echo "Book Update System"
      echo ""
      echo ""
      echo "a) Update title"
      echo "b) Update Author"
      echo "c) Update Price"
      echo "d) Update Qty Available"
      echo "e) Update Qty Sold"
      echo "f) Back to main menu"

      echo -n "Enter your option: "
      read selection
      echo ""
      case $selection in
            a) upd_title;press_enter;;
            b) upd_author;press_enter;;
            c) upd_price;press_enter;;
            d) upd_qty_avail;press_enter;;
            e) upd_qty_sold;press_enter;;
            f) main_menu;press_enter;;
            * ) tput setf 4;echo "Please enter a, b, c, d, e, or f";tput setf 7; press_enter
      esac
    done  

   else
      echo "Error!! Book does not exist!" #not found
   fi   
}

好吧我已对这个功能的代码做了一些更改..发现我应该使用awk从旧更新到新的。并使用grep获取行号。所以我最好坚持这个..

function upd_title
{
  read -p 'New title: ' title
  #awk '/liine/{ print NR; exit }' BookDB.txt
  grep -n 'regex' | sed 's/^\([0-9]\+\):.*$/\1/'
  awk 'NR==n{$1=a}1' FS=":" OFS =":" n=$linenumber a = $title BookDB.txt
  echo "New title successfully updated!!"

}

但在我尝试了这段代码后......这就是我所得到的:

Advanced Book Inventory System


1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
Enter your option: 3

Title: The Notebook
Name: Nicholas Sparks
Book found!


    Book Update System


a) Update title
b) Update Author
c) Update Price
d) Update Qty Available
e) Update Qty Sold
f) Back to main menu
Enter your option: a

New title: Notebook

仍然无法更新标题。>。<任何人都可以帮我指出问题所在?我在这里错过了什么

帮我怎么做..谢谢! :D:D

1 个答案:

答案 0 :(得分:0)

$updatetitle中的$updateauthor语句定义的

readupdate_book() not upd_title()和其他功能中可见定义

export之后尝试read。如果这不起作用,请将它们作为位置参数传递给update_title()

upd_title $update_title $update_author

...

function upd_title
{
    update_title=$1
    update_author=$2
    ...
}