【发布时间】:2018-02-24 00:37:19
【问题描述】:
我对编写 bash 相当陌生,我对我的代码的错误/问题有点困惑。我有两个主要问题。如果P) 我想看看某个文件是否存在。如果它存在,我想用newbalance 更新balance。
P) read -p "Enter email: " email
read -p "Enter payment amount: " payment
newbalance=$(echo $payment + $balance | bc)
if [ -f $email ]; then
sed -i "s/$balance/$newbalance/" $email
#also tried
#find . -type f -name "$email" -exec sed -i "s/${balance}{newbalance}/" {} \;
else
echo -e "Error: customer not found\n\n"
fi
balance=$newbalance
continue
;;
当我尝试读取 payment(例如 100.00)时收到错误消息:
(standard_in) 2: syntax error
sed: -e expression #1, char 0: no previous regular expression
它本身的文件格式非常简单,余额只出现一次。
email name
apt-XX rent balance duedate
当我重定向回声以修改文件时,代码有效。现在我已经尝试实现sed 代码已经停止工作,我不确定是什么原因造成的。我已经尝试将find 与sed 一起使用,但这也不起作用。我查看了多个堆栈溢出问题,但示例与我的相似。
这是我正在处理的文件之一
jboat@xyz.com John Boat
APT-5B 1100.00 100.00 2017-10-02
【问题讨论】:
-
newbalance和=之间有空格。应该是newbalance=$(... -
newbalance是注册商标。在遇到麻烦之前,您可能应该使用不同的变量名。 -
从您的文件中包含一组示例行,以便清楚您正在处理的数据类型。
-
@codeforester 我添加了一个文件示例(其中电子邮件也是文件名)。