KB3BYJ
KB3BYJ

Reputation: 41

[: -ne: unary operator expected

I'm trying to work on a script that will search for a certain variable in an array. Unfortunately the system I'm running changes the order of the array based on other variables at the time. I know the first seven characters of what I'm looking for will be RPT_NUM so I tried the following while loop, but I keep getting the error [: -ne: unary operator expected

START=5
MYVAR=( $(/usr/sbin/asterisk -rx "rpt showvars 47168"))

#VAR=${MYVAR[3]}

VAR="${MYVAR[START]}"

CURVAR= echo "${VAR:0:7}"
echo $VAR

while ["$CURVAR" -ne "RPT_NUM" ]
do

        let START+=1
        CURVAR= echo "${VAR:0:7}"
        echo "End loop"
done
STATUS=echo "${VAR: -1}"

echo $STATUS

I'm fairly new and still learning so any help would be great.

Upvotes: 1

Views: 1544

Answers (1)

Mark
Mark

Reputation: 6404

Try to change your code to the following one:

#!/usr/bin/env bash

START=5
MYVAR=( $(/usr/sbin/asterisk -rx "rpt showvars 47168"))

#VAR=${MYVAR[3]}

VAR="${MYVAR[START]}"

CURVAR="${VAR:0:7}"
echo $VAR

while [ "$CURVAR" == "RPT_NUM" ]
do

  let START+=1
  CURVAR="${VAR:0:7}"
  echo "End loop"
done
STATUS="${VAR:-1}"

echo $STATUS
  1. Change the condition expression in the while loop to this:
while [ "$CURVAR" == "RPT_NUM" ]
  1. Change the subshell expression to this:
CURVAR="${VAR:0:7}"   
...    
STATUS="${VAR:-1}"

Upvotes: 1

Related Questions