1

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.

Biffen
  • 6,249
  • 6
  • 28
  • 36
KB3BYJ
  • 41
  • 2
  • 2
    You need a space after the `[`. – Biffen Apr 22 '18 at 15:48
  • 2
    Also, `CURVAR= echo "${VAR:0:7}"` et al. don’t do what I think you think they do. May I recommend https://www.shellcheck.net/? – Biffen Apr 22 '18 at 15:50
  • Possible duplicate of [Why should there be a space after '\[' and before '\]' in Bash?](https://stackoverflow.com/questions/9581064/why-should-there-be-a-space-after-and-before-in-bash) – codeforester Apr 22 '18 at 17:34
  • Thanks for the suggestions, I"ll check out shellcheck.net. Also, with adding the space I now get: [: : integer expression expected – KB3BYJ Apr 22 '18 at 18:20
  • @KB3BYJ `-ne` is for numeric comparison. Run `man [` to work the rest out for yourself. – Biffen Apr 22 '18 at 18:50

1 Answers1

1

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}"
Mark
  • 5,994
  • 5
  • 42
  • 55