Problem with IF Shell Script Syntax

Asked

Viewed 428 times

3

I have the code below:

RESPOSTA=$(asterisk -rx "sip show peers" |grep 4003 |awk -F" " '{print $6}')
if[["$RESPOSTA" == "OK"]];then
 echo $RESPOSTA
elif[["$RESPOSTA" == "Unmonitored"]];then
 if[["$RESPOSTA" == "(Unspecified)"]]; then
  cp /dados/automatico.call /var/spool/asterisk/outgoing/
  asterisk -rx reload
 fi
 echo $RESPOSTA
fi

but it is generating this error:

/data/status.sh: line 5: syntax error near Unexpected token then' /dados/status.sh: line 5:if[[["$REPLY" == "OK"]]then;'

2 answers

4


Your script is fixed:

RESPOSTA=$(asterisk -rx "sip show peers" |grep 4003 |awk -F" " '{print $6}')
if [[ "$RESPOSTA" = "OK" ]]; then
 echo "$RESPOSTA"
elif [[ "$RESPOSTA" == "Unmonitored" ]]; then
 if [[ "$RESPOSTA" == "(Unspecified)" ]]; then
  cp /dados/automatico.call /var/spool/asterisk/outgoing/
  asterisk -rx reload
 fi
 echo "$RESPOSTA"
fi

He needed a space before and after the [[ and ]].

Note: To analyze online syntax problems use this tool.

2

Shell is a weird programming language. Technically, the [[ is not a de-taxe element, but is actually a command that needs to be separated from its arguments using spaces. Try rewriting your ifs in this format:

if [[ "$RESPOSTA" == "OK" ]]; then

Browser other questions tagged

You are not signed in. Login or sign up in order to post.