通过可变数量的bash命令行参数到MATLAB函数 [英] Pass a variable number of bash command line arguments to a MATLAB function

查看:537
本文介绍了通过可变数量的bash命令行参数到MATLAB函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传递给一个bash脚本参数可以被传递到下面的方式MATLAB功能:

Arguments that are passed to a bash script can be passed on to a MATLAB function in the following way:

#!/bin/bash

matlab -nodesktop -nosplash -nodisplay -r "my_function('$1','$2')"

但如何做到这一点,如果我不知道通过先验参数的数量?所以,我想要做这样的事情:

But how to do this if I do not know the number of arguments to pass a priori? So I want to do something like this:

#!/bin/bash

matlab -nodesktop -nosplash -nodisplay -r "my_function('$1',...,'$N')"

在这里我不知道是什么数N等于先验的。

where I do not know what number N equals to a priori.

我想,你可以创建一个字符串以包含'$ 1'循环,...,'$ N',整个字符串传递给上面的命令。但是,是不是有一个更简洁的方法吗?

I figure that you could create a string with a for loop containing '$1',...,'$N' and pass the entire string to the above command. But isn't there a more succinct approach?

FIW,我不流利在bash。因此,如果循环是唯一的出路,请你告诉我如何做到这一点?

FIW, I am not fluent in bash. So if the loop is the only way to go, could you please inform me how to do this?

修改

我设法制定一个解决我的问题:

I managed to devise a solution to my problem:

#!/bin/bash

INPUT=""
for var in "$@"
do
    INPUT=$INPUT"'"$var"',"
done
INPUT=${INPUT%?}

matlab -nodesktop -nosplash -nodisplay -r "my_function($INPUT)"

是不是有一个更简单/短的方式做到这一点?

Isn't there a easier/shorter way to do this?

推荐答案

从这里获得灵感的:

#!/bin/bash

INPUT=$(printf "'%s'," "$@") && INPUT=${INPUT%,}

echo matlab -nodesktop -nosplash -nodisplay -r "my_function($INPUT)"

输出:

$ ./test.sh one two three
matlab -nodesktop -nosplash -nodisplay -r my_function('one','two','three')

这是一个有点短,至少。

It's a little shorter, at least.

这篇关于通过可变数量的bash命令行参数到MATLAB函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆