【问题标题】:syntax error in conditional expression: unexpected token `;'条件表达式中的语法错误:意外标记 `;'
【发布时间】:2021-02-03 14:51:14
【问题描述】:

我有一个应该接受多个参数的 shell 脚本。

它可以接受参数“更新”或“创建”。如果没有传递参数,用户应该得到一个错误。但是,在构建我的 if/elif 条件时,我遇到了错误:

syntax error in conditional expression: unexpected token `;'

代码:

firstParam=$1
echo $firstParam //update/create/{empty}

if [[ "$firstParam" == "" ]]; then
    printf "${RED}Use this script as \"tzfrs update/new [projectName]\"${NC} \n"
    exit 1
elif [[ "$firstParam" == "update"]]; then
  printf "update"
  exit 1
fi

如果我有这样的脚本

if [[ "$firstParam" == "" ]]; then
    printf "${RED}Use this script as \"tzfrs update/new [projectName]\"${NC} \n"
    exit 1
fi

错误处理有效,我看到以下消息

Use this script as "tzfrs update/new [projectName]"

但是,在添加 elif 条件时,我遇到了上述错误。有人知道吗?

【问题讨论】:

    标签: bash shell if-statement


    【解决方案1】:
    elif [[ "$firstParam" == "update"]]; then 
    

    应该是

    elif [[ "$firstParam" == "update" ]]; then
    

    "update"]] 之间有一个空格

    【讨论】:

    • 哇,这么愚蠢的错误。感谢那 ! :) 必须再等 11 分钟才能接受答案
    • 我不敢相信我犯了这个错误! 而且它是如此受欢迎!
    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多