繁体   English   中英

从 powershell/batch 传递和读取自定义参数到八度脚本

[英]Pass and read custom argument to an octave script from powershell/batch

我想知道在一般情况下从 Powershell/batch 执行八度脚本时是否可以直接传递自定义参数。 让我们举个例子:

$octaveExePath = "C:\Octave\Octave-7.1.0\mingw64\bin\octave-cli.exe"

# Executing the script
& $octaveExePath test.m "some_custom_string"

有没有办法从test.m脚本中接收自定义参数?

编辑 1

我已经完成了@Ander 的建议但没有成功,我想在这种情况下 matlab 和 octave beaviuor 真的不同。 我的结果制作和example.m文件

function []=example(arg1)
 disp(arg1)
endfunction

然后从powershell调用它

& "C:\Octave\Octave-7.1.0\mingw64\bin\octave-cli-7.1.0.exe" example.m asd

返回错误

error: 'arg1' undefined near line 2, column 7
error: called from
    example at line 2 column 2

编辑 2

更具体地说,我的目标是知道是否有一种方法可以将参数从 shell 直接传递到 octave 程序,而无需使用某些外部文件,例如在本文中所做的那样

我已经阐述了一个简单的解决方法,可以在 Windows 环境中工作时将变量传递给 octave 脚本。 问题是在 Windows 下不可能像 文档中所说的那样创建 Octave 可执行程序。

无论如何,这个解决方法在某种程度上接近@Tasos 的建议,我最深切的感谢。

假设我们有一个包含一行代码的tentative.m文件:

disp(external_variable)

没有变量声明。

在外面,我们只按顺序执行几行 powershell 代码(确保 powershell 的位置与您创建脚本的位置相同):

$octaveExePath = "C:\Octave\Octave-7.1.0\mingw64\bin\octave-7.1.0.exe"
$externalVariable = "just a little try"

& $octaveExePath --eval "external_variable='$($externalVariable)'; tentative" 

这个结果与输出:

just a little try

因此,通过这种方式,我通过外部执行“绕过”了--evalFILE命令行选项互斥的事实(据我所知),成功地将外部值“传递”到了 octave 脚本。 唯一要注意的是,在使用此技术时,非初始化变量(在脚本中)必须与外部评估的变量名称相一致(名称必须相同)。

我希望已经清楚并且这可以帮助其他人,请考虑在这种情况下投票。

暂无
暂无

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

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