从 bash 脚本中将参数传递给八度函数

发布于 2024-11-26 17:18:29 字数 1177 浏览 1 评论 0原文

我在 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

等等..

我已经能够使用输入参数(例如 )运行octave 脚本 myOctaveFunc.m从命令行输入 helloWorld。当我尝试从 bash 脚本中运行它时,问题就出现了。

我的问题是:
1. 如何在 bash 脚本中运行八度函数?
2. 我正在使用 gvim 编辑 bash 脚本。当我输入行来调用八度音程脚本时,我发现 ${line} 的颜色与正常情况下不同。这是因为 '' 用于调用八度函数吗?如果是这样,我应该担心吗?

I wrote a function in Octave that takes in a line read from a file (one line at a time) as input argument. I use a bash script to read a line at a time from the file and then pass that as argument to the octave function from within the script.

My bash script looks like so:

#!/bin/bash  

while read line
do
  octave --silent --eval 'myOctaveFunc("${line}")'
done < "inFileName"

When I execute above script, octave throws errors like:

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

and so on..

I have been able to run the octave script myOctaveFunc.m with input arguments such as helloWorld from the command line. The problem arises when I try to run it from within the bash script.

My questions are:
1. How do I get the octave function running from within the bash script?
2. I am using gvim to edit the bash script. When I type in the line to invoke the octave script, I see that the ${line} is colored differently as compared to normal circumstances. Is that because of the '' used to invoke the octave function? If so, should I be worried about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-12-03 17:18:29

单引号阻止 shell 替换变量:

octave --silent --eval "myOctaveFunc(\"$line\")"

如果 Octave 允许您使用单引号来引用字符串,它看起来会更干净一些(在双引号内,单引号没有特殊含义):

octave --silent --eval "myOctaveFunc('$line')"

另外,从 vim 中,请确保您以 unix 格式保存文件,以便每行不以回车符结尾: :set ff=unix

The single quotes are preventing the shell from substituting the variable:

octave --silent --eval "myOctaveFunc(\"$line\")"

If octave lets you use single quotes to quote strings, it will look a little cleaner (inside double quotes, single quotes have no special meaning):

octave --silent --eval "myOctaveFunc('$line')"

Also, from vim, make sure you save the file in unix format so each line does not end with a carriage return character: :set ff=unix

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文