我在 Octave 中编写了一个函数,它将从文件中读取的一行(一次一行)作为输入参数。我使用 bash 脚本一次从文件中读取一行,然后将其作为参数从脚本中传递给 Octave 函数。
我的 bash 脚本如下所示:
#!/bin/bash
while read line
do
octave --silent --eval 'myOctaveFunc("${line}")'
done < "inFileName"
当我执行上面的脚本时,octave 会抛出如下错误:
error: called from:
error: /usr/share/octave/3.2.3/m/miscellaneous/fullfile.m at line 43, column 11
error: evaluating argument list element number 2
error: evaluating argument list element number 1
error: /usr/libexec/octave/packages/gsl-1.0.8/i386-redhat-linux-gnu-api-v37/PKG_ADD at line 47, column 1
error: addpath: expecting all args to be character strings
error: addpath: expecting all args to be character strings
error: addpath: expecting all args to be character strings
error: addpath: expecting all args to be character strings
等等..
我已经能够从命令行使用输入参数(例如 helloWorld
)运行 Octave 脚本 myOctaveFunc.m
。当我尝试从 bash 脚本中运行它时出现问题。
我的问题是:
1. 如何让 octave 函数在 bash 脚本中运行?
2. 我正在使用 gvim
来编辑 bash 脚本。当我输入调用 Octave 脚本的行时,我看到 ${line}
的颜色与正常情况下不同。那是因为用于调用 Octave 函数的 ''
吗?如果是这样,我应该担心吗?
最佳答案
单引号阻止 shell 替换变量:
octave --silent --eval "myOctaveFunc(\"$line\")"
如果octave让你用单引号来引用字符串,它看起来会干净一点(双引号里面,单引号没有特殊意义):
octave --silent --eval "myOctaveFunc('$line')"
此外,在 vim 中,确保以 unix 格式保存文件,这样每一行都不会以回车符结尾::set ff=unix
关于bash - 从 bash 脚本中将参数传递给 Octave 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6844228/