我是Shell Script编程的新手。我试图制作一个程序,输出只返回奇数参数。例如,“我是一个机器人”应该只给“我一个”。
我以为我可以使用shift命令来通过参数以及while和if语句,但是我不确定如何在if语句的方括号中以2的余数传递除法([] )
#!/bin/sh
X=$1
shift
while [ $# -gt 0 ]; do
if [ "HELP HERE" ]; then
X=$1
echo $X
fi
shift
done
echo $X
答案 0 :(得分:3)
如何换两次?
while [ $# -gt 0 ]; do
X=$1
echo $X
shift
shift
done
答案 1 :(得分:0)
#!/bin/sh
cnt=1
while [ $# -gt 0 ]; do
X=$1
# output the current argument when the counter is odd.
if [ $(($cnt%2)) -eq 1 ]; then
echo $X
fi
cnt=$(($cnt+1))
shift
done
答案 2 :(得分:0)
#!/bin/bash
declare -a ODD
declare -a EVEN
ODDIX=0
EVENIX=0
while [ $# -ne 0 ]; do
if (( $#%2 == 1 ));then
ODD[$ODDIX]=$1
ODDIX=$((ODDIX+1))
else
EVEN[$EVENIX]=$1
EVENIX=$((EVENIX+1))
fi
shift
done
echo ODD: ${ODD[@]}
echo EVEN: ${EVEN[@]}*