好的..。我在更新新书标题时遇到了问题..我已经在这个网站上寻找解决方案了..我试过了,但都不管用..
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来获得行number..so最好我坚持这样做..
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
发布于 2015-01-22 16:51:29
由update_book()
中的read
语句定义的$updatetitle
和$updateauthor
在upd_title()
和您定义的其他函数中不可见。
在它们被read
in之后,尝试对它们进行export
。如果这不起作用,则将它们作为位置参数传递给update_title()
upd_title $update_title $update_author
...
function upd_title
{
update_title=$1
update_author=$2
...
}
https://stackoverflow.com/questions/28083696
复制相似问题