如何在bash中访问偶数数组元素

时间:2019-06-29 09:43:43

标签: arrays bash

我想在bash中回显数组的偶数元素,如何实现?

2 个答案:

答案 0 :(得分:0)

假设您的数组不是稀疏的(不包含空格), 假设即使您从1开始计数(而不是像bash一样从0开始计数),也可以对索引进行循环:

array=(a b c d e f g h)
for index in "${!array[@]}"; do
  (( index % 2 )) && echo "${array[index]}"
done
:

输出:

b
d
f
h

答案 1 :(得分:0)

假设您在谈论索引而不是关联数组,并且您希望偶数索引的值而不是偶数值-从零循环到数组大小,每次迭代将索引增加2。

借用@Camunsensei的示例:

@Bean
public IntegrationFlow httpGetTest() {
    return IntegrationFlows.from(Http.inboundGateway("/test")
                .requestMapping(r -> r.methods(HttpMethod.GET))
                .crossOrigin(cors -> cors.origin("*"))
                .headerMapper(headerMapper()))
          .channel("http.test.channel")
          .handle("testEndpoint", "hello")
          .get();
}

如果这不是您所需要的,则可以编辑问题以包括一些示例输入,预期输出以及到目前为止您已经尝试的操作。