且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux脚本来更新价格

更新时间:2023-12-04 20:18:16

关于上述问题我想出了一个答案。希望它有助于

In regards to the question above i came up with an answer for that. Hope it Helps

 price=$(echo "$result" | cut -f 3 -d ":")

我通过匹配结果行然后使用sed获取用户输入内容的第三个字段

I manage to get the 3rd field of what the user input by matching it with the result line followed by using the sed line i edited the third field.

function update_cost
{
   echo "Enter Title: "
   read title
   echo "Enter Author: "
   read author
   result=$(grep -ise "$title\:$author" BookDB.txt)
   price=$(echo "$result" | cut -f 3 -d ":")
   grep -iqs "$title:$author:$price:" BookDB.txt && echo "Book Found!"      
   echo "New Price: "
   read new_price
   sed -i "/^$title:$author:$price:/ s/$price/$new_price/" BookDB.txt || tee BookDB.txt && echo "Price has been updated sucessfully!"   

}