[英]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.