例如,以下两种方法有效, 但是最干净/最简单的方法是什么?
#!/usr/bin/env bash
function strategyOne()
{
X=0
for I in "$@"
do
X=$((X%2))
if [ $X -eq 1 ]
then
B="$I"
echo "Pair: $A,$B"
else
A="$I"
fi
X=$((X+1))
done
}
function strategyTwo()
{
ARG_COUNT=$#
COUNTER=0
while [ $COUNTER -lt $ARG_COUNT ]; do
let COUNTER=COUNTER+1
A="${!COUNTER}"
let COUNTER=COUNTER+1
B="${!COUNTER}"
if [ $COUNTER -le $ARG_COUNT ]
then
echo "Pair: $A,$B"
fi
done
}
echo
echo "Strategy 1"
strategyOne $*
echo
echo "Strategy 2"
strategyTwo $*
产生如下输出:
$ ./test.sh a b c d e
Strategy 1
Pair: a,b
Pair: c,d
Strategy 2
Pair: a,b
Pair: c,d
答案 0 :(得分:3)
另一种方法:
data=("Banana" "Apple" "Onion" "Peach")
for i in $(seq 0 2 $((${#data[@]}-1)))
do
echo ${data[i]}
done
答案 1 :(得分:2)
您可以使用数组索引使用iterate:
https://graph.facebook.com/v2.10/act_11111111111111111/ads?fields=name,insights{actions?action_type=comment,link_click}&limit=1
答案 2 :(得分:1)
这里有一个简单的Bash方法。
#!/bin/bash
data=("Banana" "Apple" "Onion" "Peach")
for ((i=0;i< ${#data[@]} ;i+=2));
do
echo ${data[i]}
done
问候!