[英]When/how to use “==” or “-eq” operator in test?
在下面的代码中,我想比较命令行参数和参数,但我不确定将参数与parameters..ie“==”或“-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
}
来自help test
:
[...]
STRING1 = STRING2 True if the strings are equal.
[...]
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne, -lt, -le, -gt, or -ge.
但无论如何,条件的每个部分都是[
。
if [ "$arg" -eq 0 ]; then
if [ "$arg" = 0 ]; then
为什么不使用像
if [“$#” - ne 0]; 那么args的数量不应该是零
回声“用法:”
科幻
何时/如何在测试中使用“==”或“-eq”运算符?
简单地说,在进行词法比较(即字符串比较)时使用==
,但在进行数值比较时使用-eq
。
其他形式的-eq
(相等)是-ne
(不等于), -gt
(大于), - -ge
(大于或等于), -lt
(小于)和-le
(小于或等于)。
有些人也可能建议更喜欢(( ))
。
例子:
[[ $string == "something else" ]]
[[ $string != "something else" ]] # (negated)
[[ $num -eq 1 ]]
[[ $num -ge 2 ]]
(( $num == 1 ))
(( $num >= 1 ))
当你在Bash中时总是使用[[ ]]
不是[ ]
,因为前者跳过了与词分裂和路径名扩展等条件表达式无关的不必要的扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.