bash:意外令牌'-o'附近的语法错误

codyc4321

我在运行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
}

在此处输入图片说明

任何帮助表示赞赏,ty

屠夫

尝试使用||代表以下任何一种:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章