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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6844228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:27:20  来源:igfitidea点击:

Passing arguments to octave function from within bash script

bashscriptingcommand-lineoctave

提问by Sriram

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.

我在 Octave 中编写了一个函数,它将从文件中读取的一行(一次一行)作为输入参数。我使用 bash 脚本从文件中一次读取一行,然后将其作为参数从脚本中传递给八度函数。

My bash script looks like so:

我的 bash 脚本如下所示:

#!/bin/bash  

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

When I execute above script, octave throws errors like:

当我执行上面的脚本时,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

and so on..

等等..

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

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

My questions are:
1. How do I get the octave function running from within the bash script?
2. I am using gvimto 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?

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

回答by glenn Hymanman

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

单引号阻止 shell 替换变量:

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 允许你使用单引号来引用字符串,它会看起来更简洁一些(在双引号里面,单引号没有特殊含义):

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

此外,在 vim 中,请确保以 unix 格式保存文件,以便每一行都不会以回车符结尾: :set ff=unix