简体   繁体   English

八度3对4脚本兼容性; 可执行八度程序的真实文档在哪里

[英]octave 3 vs 4 script compatibility; where's REAL documentation of Executable-Octave-Programs

Part-1: In octave 3.4.3 (on centos 6.6) following script file "joe.m" (but for 3.x minus --no-gui ): 第1部分:在脚本文件“ joe.m”之后的八度3.4.3(在centos 6.6中)(但对于3.x 减去 --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": 但是在倍频程4.2.1中,它发出警告,并且似乎自动调用(!?)jim,并且不运行顶级立即代码(!?),没有输出行“调用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: 第2部分:如果我将同一文件重命名为“ jim.m”,则在八度3.4.4中,输出为:

calling jim
program_name: 'jim.m'

in octave 4.2.1 now warning is avoided, but still missing "calling jim" line. 在倍频程4.2.1中,现在可以避免警告,但是仍然缺少“ calling jim”行。

Part-3: With zero functions defined, the top-level code will execute in both versions. 第3部分:在定义了零个函数的情况下,顶级代码在两个版本中执行。

Where are these behaviors (and this change of behavior from version 3 to 4) documented or controlled? 这些行为(行为从版本3到版本4的更改)在哪里得到记录或控制? 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/Command-Line-Options.html

https://octave.org/doc/v4.2.1/Executable-Octave-Programs.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? 如何编写与版本3.x和4.x兼容的八度文件,或者如何调用带有附加选项的4.x以与3.x兼容? How to execute top-level code in 4.x even when functions are defined? 即使定义了函数,如何在4.x中执行顶级代码?

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? 在没有相同文档的情况下,如何确定性地知道(没有反复试验)在4.x中将自动调用哪个函数(以及带有哪些参数)? 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? 这个示例不足以说明问题,因为只有一个功能:如果有多个功能(joe和jim),相对于文件名是否匹配,顺序是否重要?

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. 编辑:我包含了shebang(自包含脚本),试图不“提出错误的问题”或针对我自己的“尝试的解决方案”过早地优化问题,但是无论有无,其行为都是相同的。 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). 我需要脚本来:不要使用绝对路径来八度,而要接受额外的单独选项(我无法将--no-gui组合到4.2.1中的-qf中)。 Your simplifications are welcome. 欢迎您进行简化。

There are different .m file types including (according to Octave 4.2.1): .m文件类型不同,包括(根据Octave 4.2.1):

  1. Function files: A file that contains definition of one or more functions. Function文件:包含一个或多个功能的定义的文件。 The name of a function file often matches the name of the first function defined in the file. 函数文件的名称通常与文件中定义的第一个函数的名称匹配。
  2. Script files: A file that contain lines of code including definition of functions. Script文件:包含代码行(包括函数定义)的文件。

    Unlike a function file, a script file must not begin with the keyword function . 与功能文件不同,脚本文件不能以关键字function开头。 If it does, Octave will assume that it is a function file, and that it defines a single function that should be evaluated as soon as it is defined. 如果是这样,Octave将假定它是一个功能文件,并且它定义了一个应在定义后立即求值的功能。

  3. Class definition files. 类定义文件。

The behavior of Octave when is executed as: Octave的行为执行为:

$octave File

is documented in one of source files (oct-parse.yy) of Octave: 记录在Octave的源文件之一(oct-parse.yy)中:

Execute the contents of a script file. 执行脚本文件的内容。 For compatibility with Matlab, also execute a function file by calling the function it defines with no arguments and nargout = 0. 为了与Matlab兼容,还可以通过调用不带参数且nargout = 0的函数来执行函数文件。

Here because your file begins with the keyword function it is considered as a function file so the function jim is automatically called regardless of if you invoke jim or not. 这里,是因为你的文件开头的关键字功能它被认为是一个函数文件,因此在功能jim自动不管,如果你调用的叫jim与否。 So you can remove the expression jim from the end of file and see that the function is automatically called. 因此,您可以从文件末尾删除表达式jim ,然后看到该函数被自动调用。 In version 3.4.3 I think it is assumed as a script file. 在3.4.3版中,我认为它被假定为脚本文件。

To solve the problem you need to add an expression,other than the function keyword, to the beginning of the file to convert it to a script file: 要解决此问题,您需要在文件的开头添加除function关键字之外的表达式,以将其转换为脚本文件:

#!/bin/bash
# for-bash:
#{
  exec octave -q --no-init-file "$0" ${1+"$@"}
#}
# for-octave:
1;
function jim ()
  printf ("program_name: '%s'\n", program_name ());
endfunction
printf ("calling jim\n")
jim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM