Syntax error in shell script saying unexpected tok

2019-09-18 16:11发布

问题:

I have written the following shell script

while :; do
status=$($EMR_BIN/elastic-mapreduce --jobflow $JOBFLOW --list | grep "CopyLogs" | awk '{print $1}')
[[ $status == +( *RUNNING*|*PENDING*|*WAITING* ) ]] || break
sleep 60
done

Its giving me an error in line 3 saying syntax error in conditional expression: unexpected token('' . I tried giving whitespaces between my braces, but its not working.

Can anyone help me out.

回答1:

Looks like you are trying to use extended globbing. Make sure you have shopt -s extglob somewhere earlier in your script, or rewrite to use standard globbing.

#!/bin/sh
while :; do
    case $($EMR_BIN/elastic-mapreduce --jobflow $JOBFLOW --list | awk '/CopyLogs/{print $1}') in
        *RUNNING*|*PENDING*|*WAITING* ) sleep 60;; 
        *) break;;
    esac
done

Since there are no remaining Bashisms, this script is now POSIX sh compatible. (Personally, I also think it is more readable this way.)

(Note also the fix for the useless grep | awk.)



标签: bash shell