【发布时间】:2014-09-12 18:06:26
【问题描述】:
在下面的代码中,我想将命令行参数与参数进行比较,但我不确定将参数与参数进行比较的当前语法是什么......即“==”或“-eq”。
#!/bin/bash
argLength=$#
#echo "arg = $1"
if [ argLength==0 ]; then
#Running for the very first
#Get the connected device ids and save it in an array
N=0
CONNECTED_DEVICES=$(adb devices | grep -o '\b[A-Za-z0-9]\{8,\}\b'|sed -n '2,$p')
NO_OF_DEVICES=$(echo "$CONNECTED_DEVICES" | wc -l)
for CONNECTED_DEVICE in $CONNECTED_DEVICES ; do
DEVICE_IDS[$N]="$CONNECTED_DEVICE"
echo "DEVICE_IDS[$N]= $CONNECTED_DEVICE"
let "N= $N + 1"
done
for SEND_DEVICE_ID in ${DEVICE_IDS[@]} ; do
callCloneBuildInstall $SEND_DEVICE_ID
done
elif [ "$1" -eq -b ]; then
if [ $5 -eq pass ]; then
DEVICE_ID=$3
./MonkeyTests.sh -d $DEVICE_ID
else
sleep 1h
callCloneBuildInstall $SEND_DEVICE_ID
fi
elif [ "$1" -eq -m ]; then
echo "Check for CloneBuildInstall"
if [ "$5" -eq pass ]; then
DEVICE_ID=$3
callCloneBuildInstall $SEND_DEVICE_ID
else
echo "call CloneBuildInstall"
# Zip log file and save it with deviceId
callCloneBuildInstall $SEND_DEVICE_ID
fi
fi
function callCloneBuildInstall {
./CloneBuildInstall.sh -d $SEND_DEVICE_ID
}
【问题讨论】:
-
@user2864740 比较是真的.. 我的意思是当我写 "$1" -eq -m 或者它应该是 $1 -eq -m?
-
实际上,在使用 Bash 时,更喜欢
==而不是=。 -
@konsolebox 为什么更喜欢
==?它们在 bash 中是等价的,当您编写可移植(非 bash 依赖)脚本时,习惯使用==会让您感到困惑。 -
@GordonDavisson 如果我们不关心可移植性,
==更具可读性。