0

I'm fairly new to writing bash and I'm a bit confused on the errors/problem with my code. I have two main problems. In case P) I would like to see if a certain file exist. If it exist I would like to update the balance with the newbalance.

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
   ;;

when I try to read in a payment such as 100.00 I receive an error:

(standard_in) 2: syntax error
sed: -e expression #1, char 0: no previous regular expression

The file format its self is pretty simple and the balance only appears once.

email  name
apt-XX rent balance duedate

The code worked when I redirected echo's to modify the file. Now that I've tried to implement sed the code has stopped working, I'm unsure of what is causing it. I've tried using find with sed but that isn't working either. I've looked at multiple stack overflow questions but the examples are similar to mine.

This is one of the files I'm dealing with

jboat@xyz.com John Boat
APT-5B 1100.00 100.00 2017-10-02
below_avg_st
  • 187
  • 15

2 Answers2

0

You can use bash to do the calculation, no need for bc.

newbalance=$((payment + balance))

The sed command is ok, you probably got the error because one of the variables contained crap.

yacc
  • 2,915
  • 4
  • 19
  • 33
  • This still seems to give me an error `./menu.bash: line 32: 130.00: syntax error: invalid arithmetic operator (error token is ".00").` I was under the impression that I needed bc for floats @yacc. – below_avg_st Sep 15 '17 at 01:02
  • Ok I thought it's all integers. Then do a `echo "s/$balance/$newbalance/"` to check what sed gets as regex. @below_avg_st – yacc Sep 15 '17 at 01:11
  • With the echo I'm getting `(standard_in) 2: syntax error s// sed: -e expression #1, char 0: no previous regular expression` @yacc . – below_avg_st Sep 15 '17 at 01:21
0

The most likely reason you're getting that error is because $balance is empty. For example:

$ echo 100.00 | sed 's//200.00/'
sed: -e expression #1, char 0: no previous regular expression

(The echo is just there because sed expects input no matter what.)

So you'll need to track where $balance is getting its value and make sure that's not failing.

But, since it's still going to be prone to failure, I'd suggest a slightly more resilient approach using AWK instead:

awk -v newbalance="$newbalance" 'FNR==2 {$3=newbalance; print;}' "$email" > "$email.tmp" && mv "$email.tmp" "$email"

This sets an awk variable called newbalance to the value of the bash variable $newbalance and then runs the script. The FNR tells it which line to read (in this case the 2nd line in the file), and $3=newbalance changes the value of the 3rd field in that line to the value we passed in earlier.

The mv at the end is just because older versions of awk can't edit files in place. See: awk save modifications in place

Be warned that if you go down this rabbit hole, you might end up rewriting the entire script in awk before you're done. For inspiration, you could start with moving the arithmetic into awk itself:

awk -v payment="$payment" 'FNR==2 {$3+=payment; print;}' "$email" > "$email.tmp" && mv "$email.tmp" "$email"
demonbane
  • 1
  • 1