bash:意外标记‘-o’附近有语法错误

bash:意外标记‘-o’附近有语法错误

运行 bash 脚本来备份 git 分支时出现错误:

脚本如下:

#!/usr/local/bin/bash
# this shebang assumes you have bashv4 installed. If not, use /bin/bash

echo "running backup branch script"

current_branch="$(git branch | grep '\* ' | sed 's/^.*\( .*\)/\1/g')"

dltbranch() {
    git push origin --delete $1
    git push upstream --delete $1
    git branch -D $1
}

backup_branch() {
    git checkout $1
    git checkout -b backup__$1
    dltbranch $1
}

reset_branch() {
    the_new_current_branch="$(git branch | grep '\* ' | sed 's/^.*\( .*\)/\1/g')"
    echo "$the_new_current_branch"
    if [ ! $the_new_current_branch = $1 ]; then
        git checkout $1
    fi
}

branch_is_protected(){
    if [[ "$1" == dev* ]] -o [[ "$1" == "master"]] -o [[ "$1" == backup* ]]
    then
        echo "protected"
        exit 1
    else
        echo "not protected"
        exit 0
    fi
}

backup_all_branches(){
    branches="$(git for-each-ref refs/heads | cut -d/ -f3-)"
    echo $branches

    for branch in `echo "$branches"`; do

        backup_branch "$branch";
        # echo "$branch"
    done
}

正如预期的那样,我在 shellcheck 中没有错误,也看不到任何错误。通过注释掉branch_is_protected

branch_is_protected(){
    if [[ "$1" == dev* ]] -o [[ "$1" == "master"]] -o [[ "$1" == backup* ]]
    then
        echo "protected"
        exit 1
    else
        echo "not protected"
        exit 0
    fi
}

在此处输入图片描述

任何帮助表示感谢,谢谢

答案1

尝试使用||来表示:

branch_is_protected(){
    if [[ "$1" == dev* || "$1" == "master" || "$1" == backup* ]]
    then
        echo "protected"
        exit 1
    else
        echo "not protected"
        exit 0
    fi
}

相关内容