linux - 如何更新书名?

标签 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

最佳答案

$updatetitle$updateauthorupdate_book() 中的 read 语句定义 不是upd_title() 和您定义的其他函数中可见。

在它们被读入后尝试导出它们。如果这不起作用,将它们作为位置参数传递给update_title():

upd_title $update_title $update_author

...

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

关于linux - 如何更新书名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28083696/

相关文章:

linux - chmod 排除

linux - 摄像机端(而非客户端)的 ONVIF 视频流

bash - 如何覆盖 helm 值

windows - 在 Windows 中使用 grep、sed、awk 进行 Shell

linux - 在命令强制脚本不从文本文件中读取

Bash 标准错误重定向

如果条件 : "-gt" vs ">" when using [[ 则 bash

linux - 在 Linux 的文本文件中用逗号替换空格

Linux/bash 管道和重定向

linux - 尽管用户存在但无法 chown