0

I have a problem with running script under bash. When the script is started and while it is executing some long operation and I change that script, after long operation is finished bash is reading rest of the script and fails with silly errors like below:

test.sh: line 1093: unexpected EOF while looking for matching `"'
test.sh: line 1098: syntax error: unexpected end of file

If I run the same script (without further changes) there are no error whatsoever.

I suspect there is a buffered reading done by bash. Is there something I can do to make bash reading script in whole?

Michael
  • 1,014
  • 2
  • 15
  • 26
  • Can you show us those lines from 1093 to 1098, without it one can't understand much of the problem – Inian Jan 08 '18 at 13:05
  • If you have to change it while it's running, change the start of it so it copies itself somewhere else and execs the copy with the original parameters. – Mark Setchell Jan 08 '18 at 14:09

3 Answers3

3

Bash reads the script line by line. If you change the length of a line that's before the current position, bash will start reading the next line from the middle of a line and most probably fail.

Don't change the source of a running script, make a copy.

choroba
  • 231,213
  • 25
  • 204
  • 289
0

Probable duplicate of the question asked:

Edit shell script while it's running

In particular, I like the answer at the comment referenced in the link (involves use of the source command and seperate .sh files).

amasmiller
  • 335
  • 2
  • 7
0

I found a solution: to add {} like

#!/bin/bash
{
code goes here
}

This way everything inside {} will be read into memory.

Michael
  • 1,014
  • 2
  • 15
  • 26