Part-1: In octave 3.4.3 (on centos 6.6) following script file "joe.m" (but for 3.x minus --no-gui
):
#!/bin/bash
# for-bash:
#{
exec octave -q --no-gui --no-init-file "$0" ${1+"$@"}
#}
# for-octave:
function jim ()
printf ("program_name: '%s'\n", program_name ());
endfunction
printf ("calling jim\n")
jim
produces output:
calling jim
program_name: 'joe.m'
But in octave 4.2.1 it gives a warning, and appears to auto-call(!?) jim, and does NOT run top-level immediate code(!?), no output line "calling jim":
warning: function name 'jim' does not agree with function filename '/tmp/joe.m'
program_name: 'joe.m'
Part-2: If I rename same file to "jim.m", then in octave 3.4.4 the output is:
calling jim
program_name: 'jim.m'
in octave 4.2.1 now warning is avoided, but still missing "calling jim" line.
Part-3: With zero functions defined, the top-level code will execute in both versions.
Where are these behaviors (and this change of behavior from version 3 to 4) documented or controlled? Nothing of the sort is mentioned in:
https://octave.org/doc/v4.2.1/Command-Line-Options.html
https://octave.org/doc/v4.2.1/Executable-Octave-Programs.html
How can one write an octave file compatible with both version 3.x and 4.x, or how to invoke 4.x with an extra option to behave compatibly with 3.x? How to execute top-level code in 4.x even when functions are defined?
How can one deterministically know (without trial and error) what function will be auto-called (and with what arguments) in 4.x without documentation of same? This example not enough to nail it down, since there's one and only one function: if there are multiple functions (joe and jim), does the order matter, relative to whether either or none matches the file name?
Edit: I include the shebang (self-contained script) in attempt to not "ask the wrong question" or prematurely optimize the question towards my own "attempted solution", yet behaviors are same with or without it. I need script to: not use absolute path to octave, and accept extra separate options (I could not combine --no-gui
into -qf
in 4.2.1). Your simplifications are welcome.