【问题标题】:Syntax error in shell script saying unexpected token `(shell 脚本中的语法错误说意外标记`(
【发布时间】:2014-05-25 08:30:50
【问题描述】:

我写了下面的shell脚本

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

它在第 3 行给我一个错误,说 syntax error in conditional expression: unexpected token('' 。我尝试在大括号之间添加空格,但它不起作用。

谁能帮帮我。

【问题讨论】:

标签: bash shell


【解决方案1】:

看起来您正在尝试使用扩展通配符。确保您的脚本中前面有 shopt -s extglob,或者重写以使用标准通配符。

#!/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

由于没有剩余的 Bashism,此脚本现在与 POSIX sh 兼容。 (我个人也觉得这样更易读。)

(还要注意无用的grep | awk的修复。)

【讨论】:

  • 哦,好吧,你的意思是说要使用shopt -x extglob,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多