【问题标题】:Ruby for loop starting with the second function parameter从第二个函数参数开始的 Ruby for 循环
【发布时间】:2021-02-18 20:57:27
【问题描述】:

如何从第二个参数开始迭代我的函数参数?

#!/bin/bash -

function test23() {
  echo 'hello world'

  for text in $2..$@
  do
   echo $text
   done
}

test23 start with the second argument

我目前的输出是

hello world
with..start
with
the
second
argument

我想得到“第二个参数”。

【问题讨论】:

    标签: bash


    【解决方案1】:

    根据堆栈溢出:Iterate through parameters skipping the first

    function test23() {
      echo 'hello world'
      for text in "${@:2}"; do
        echo $text
      done
    }
    
    $ test23 start with the second argument
    hello world
    with
    the
    second
    argument
    

    【讨论】:

      【解决方案2】:

      你可以shift:

      #!/usr/bin/env bash
      
      test23() {
        echo 'hello world'
      
        shift
      
        for text
        do
          printf '%s\n' "$text"
        done
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        相关资源
        最近更新 更多