Linux脚本来更新价格 [英] linux script to update price
问题描述
我想做一个提示用户输入图书标题和作者的代码,我希望使用grep来获取基于标题和作者的数据并回显供用户查看和编辑没有用户输入旧价格的价格
i would like to do a code that prompts user to input the title and author of a book and i want to use grep to get the data based on just the title and author and echo it back for the user to see and edit the price without having the user to enter the old price
只需输入标题和作者即可获得$ price变量,需要帮助
I need help in trying to get the $price Variable just by entering the title and author
function update_cost
{
echo "Title: "
read title
echo "Author: "
read author
grep -iqs "$title:$author:$price:" BookDB.txt && echo "$title:$author:$price:"
echo "New Price: "
read price_r
sed -i "/^$title:$author:/ s/$price/$price_r" BookDB.txt || tee BookDB.txt && echo "Book Price has been updated sucessfully!"
}
推荐答案
关于上述问题我想出了一个答案。希望它有助于
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!"
}
这篇关于Linux脚本来更新价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!