3 ms·
Shell script interpreters like bash do. You can change a bash script while it's running (append, or truncate and re-write) and it'll keep going from the same of
by ploxiln 11y ago
Shell script interpreters like bash do. You can change a bash script while it's running (append, or truncate and re-write) and it'll keep going from the same offset.
But then again, shell script is in a different class of language I'd say.
#!/bin/bash
COUNTER=0
addmore() {
echo hi $COUNTER
((COUNTER++))
echo "addmore" >>$0
sleep 1
}
addmore
- deleted 11y ago[deleted]
- kazinator 11y agoBash reads code from a stream, and doesn't read the entire stream before executing the code. However, the stream pointer isn't its instruction pointer in the script. A backwards branch (such as the end of a while loop, or a call to a function defined earlier) does not rewind the stream to that piece of text. > But then again, shell script is in a different class of language I'd say. Not much different in this regard from how, say, Common Lisp (load "file.lisp") processes the top-level forms in the file. (defvar *counter* 0) (defun addmore () (format t "hi ~s~%" (incf *counter*)) (with-open-file (f "addmore.lisp" :direction :output :if-exists :append) (write-line "(addmore)" f))) $ clisp addmore.lisp hi 1 WARNING: OPEN: #<INPUT BUFFERED FILE-STREAM CHARACTER #P"addmore.lisp" @8> already points to file "/home/kaz/test/addmore.lisp", opening the file again for :OUTPUT may produce unexpected results Open the file anyway hi 2 WARNING: OPEN: #<INPUT BUFFERED FILE-STREAM CHARACTER #P"addmore.lisp" @9> already points to file "/home/kaz/test/addmore.lisp", opening the file again for :OUTPUT may produce unexpected results Open the file anyway hi 3 WARNING: OPEN: #<INPUT BUFFERED FILE-STREAM CHARACTER #P"addmore.lisp" @10> already points to file "/home/kaz/test/addmore.lisp", opening the file again for :OUTPUT may produce unexpected results Open the file anyway hi 4 WARNING: OPEN: #<INPUT BUFFERED FILE-STREAM CHARACTER #P"addmore.lisp" @11> already points to file "/home/kaz/test/addmore.lisp", opening the file again for :OUTPUT may produce unexpected results Open the file anyway hi 5