News for hackers. Stuff that matters.
Re:Plan to throw one away by kazinator on Tuesday December 15, 2015 @11:14AM (#10738573)
Bash 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

There are no comments on this comment yet.