我正在尝试编写一个可以作为可执行文件运行的Octave脚本。
我使用的是八度版本 3.6.0。我在这里运行以下脚本下载表单:
#!/usr/local/bin/octave -qf
# An example Octave script
len = input( "What size array do you wish to use for the evaluation: " );
clear a;
tic();
for i=1:len
a(i) = i;
endfor
time1 = toc();
a = [1];
tic();
for i=2:len
a = [a i];
endfor
time2 = toc();
a=zeros( len, 1 );
tic();
for i=1:len
a(i) = i;
endfor
time3 = toc();
printf( "The time taken for method 1 was %.4f secondsn", time1 );
printf( "The time taken for method 2 was %.4f secondsn", time2 );
printf( "The time taken for method 3 was %.4f secondsn", time3 );
但是,当我在命令行上运行脚本时,出现以下错误:
'usr/local/bin/octave: 无效选项 -- '
但是,当我在命令行键入相同的命令时:
/usr/local/bin/octave -qf
我得到八度命令提示符。我做错了什么?
我假设你在某种Unix/Linux系统上。 文件是否为"DOS"格式,带有 DOS 样式的行尾? 这可能会导致命令的解释方式出现问题。
你的 shebang 行(顺便说一句,它有一个不应该的空格(正在调用/usr/local/bin/octave
,但错误来自 /usr/bin/octave
.这是错误吗?如果是这样,您需要复制并粘贴代码和错误。否则,local
版本可能是一个脚本,该脚本在非交互式运行时使用不正确的选项调用二进制文件。特别是,看起来脚本(或至少(正在尝试使用长选项(--option
(,而二进制文件不支持它(因此它将其解释为短选项(。
首先,发布的脚本在我的系统上运行良好。
我键入nano test.sh,将其复制到文件中,将第一行更改为#!/usr/bin/octave -qf
。
我按 Ctrl-O 和 Ctrl-X。
然后,我使用 chmod +x test.sh 使脚本可执行。
然后,我使用 ./test.sh 或八度 -qf test.sh 运行脚本,它按预期工作。
笔记:我的系统上的八度是
$ which octave
/usr/bin/octave
$ file /usr/bin/octave
/usr/bin/octave: symbolic link to `octave-3.6.1'
$file /usr/bin/octave-3.6.1
/usr/bin/octave-3.6.1: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x061b0a703928fc22af5ca93ee78346a7f5a0e481, stripped
在我的系统上,/usr/bin
和/usr/local/bin
都在$PATH
我可以生成您提到的错误的唯一方法是将脚本的第一行更改为 #!/usr/bin/octave - -
或#!/usr/bin/octave -q -f
.
这给了
$ ./test.sh
/usr/bin/octave: invalid option -- ' '
这意味着在计算机上的脚本中,shebang 行不正确或被错误地解释。
验证脚本中的第一行是否正确。
还要确定如果将行更改为 #!/usr/local/bin/octave -q
或 #!/usr/local/bin/octave -f
会发生什么情况。
有关舍邦线解析的更多信息,请参阅:
- #!魔法,关于各种Unix风格的shebang/hash-bang机制的细节。
- 如何在 shebang 中使用多个参数(即 #!(?
- #! 处理中的错误 - 再来一次