我的脚本接受键值对作为命令行参数。我测试给定数量的参数是否是偶数。如果偶数,我想遍历数组,将args[n]
&args[n+1]
视为一对(n = 0 to $# - 1)
并执行另一个将这两个作为参数的命令。我无法使 for 循环到位。这是我的代码 -
#!/bin/sh
usage() {
echo "$0 <property-name> <property-value> [<property-name> <property-value> ...]"
exit 1
}
if [ $# -lt 2 ] ; then
usage
fi
# Should be even number of arguments
if [ $(( $# % 2 )) -ne 0 ]
echo "Even number of arguments expected."
usage
fi
args=("$@")
COMMAND="/mycommand "
for ### WHAT SHOULD I WRITE HERE??
PROPERTY=${args[$c]}
VALUE=${args[$c + 1]}
COMMAND="mycommand $PROPERTY $VALUE"
## Execute command
done