【问题标题】:How do I loop through elements of a bash array variable passed to a Ruby script via terminal?如何遍历通过终端传递给 Ruby 脚本的 bash 数组变量的元素?
【发布时间】:2016-08-31 23:34:23
【问题描述】:

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

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

这里是testScript.rb

ARGV.each{|a| p a}

当我在终端中运行 ./test.sh 时,我会得到以下输出:

"whatever@gmail.com"

我希望我的 Ruby 脚本循环遍历 Bash 数组变量 $arr 中的所有元素。如何修复我的代码来执行此操作?

【问题讨论】:

    标签: arrays ruby linux bash parameter-passing


    【解决方案1】:

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

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

    【讨论】:

    • 行情!行情是你的朋友。使用它们:ruby testScript.rb "${arr[@]}".
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多