如何循环遍历通过终端传递给Ruby脚本的bash数组变量的元素?

时间:2022-06-07 05:23:12

I have a Bash file called test.sh:

我有一个叫test.sh的Bash文件:

#!/bin/bash
arr=(whatever@gmail.com iamcool@gmail.com)
ruby testScript.rb $arr

And here's testScript.rb:

这里是testScript.rb:

ARGV.each{|a| p a}

When I run ./test.sh in the terminal, I get this output:

当我运行。/测试。在终端sh,我得到这个输出:

"whatever@gmail.com"

I want my Ruby script to loop through all elements in the Bash array variable $arr. How do I fix my code to do this?

我希望我的Ruby脚本遍历Bash数组变量$arr中的所有元素。我该如何修改我的代码呢?

1 个解决方案

#1


5  

In the bash script, pass the array like this:

在bash脚本中,像这样传递数组:

ruby testScript.rb "${arr[@]}"

#1


5  

In the bash script, pass the array like this:

在bash脚本中,像这样传递数组:

ruby testScript.rb "${arr[@]}"