Problem with Syntax IF Shell Script

3

I have the following code:

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:

  

/dates/status.sh: line 5: syntax error near unexpected token then' /dados/status.sh: line 5: if [["$ RESPONSE" == "OK"]]; then '

    
asked by anonymous 05.08.2014 / 16:55

2 answers

4

Your corrected script:

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

You had to put a space before and after [[ and ]] .

Note: To analyze syntax problems online use this tool .

    
05.08.2014 / 17:06
2

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

if [[ "$RESPOSTA" == "OK" ]]; then
    
05.08.2014 / 17:04