2

In the following code I want to compare the command line arguments with the parameters but I am not sure what is the current syntax to compare the arguments with parameters..i.e "==" or "-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
  • 60,010
  • 15
  • 145
  • 220

3 Answers3

7

From 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.

But in any case, each part of the condition is a separate argument to [.

if [ "$arg" -eq 0 ]; then

if [ "$arg" = 0 ]; then
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Why not use something like

if [ "$#" -ne 0 ]; then # number of args should not be zero
echo "USAGE: "
fi

shyamupa
  • 1,528
  • 4
  • 16
  • 24
1

When/how to use “==” or “-eq” operator in test?

To put it simply use == when doing lexical comparisons a.k.a string comparisons but use -eq when having numerical comparisons.

Other forms of -eq (equal) are -ne (not equal), -gt (greater than), -ge (greater than or equal), -lt (lesser than), and -le (lesser than or equal).

Some may also suggest preferring (( )).

Examples:

[[ $string == "something else" ]]
[[ $string != "something else" ]] # (negated)
[[ $num -eq 1 ]]
[[ $num -ge 2 ]]
(( $num == 1 ))
(( $num >= 1 ))

And always use [[ ]] over [ ] when you're in Bash since the former skips unnecessary expansions not related to conditional expressions like word splitting and pathname expansion.

konsolebox
  • 72,135
  • 12
  • 99
  • 105