zsh有条件或失败

zsh有条件或失败,zsh,Zsh,失败于 # delete git branch git--() { BRANCH=$1 if [ -n $BRANCH]; then BRANCH=$(git rev-parse --abbrev-ref HEAD) fi if [ "$BRANCH" = 'master' || "$BRANCH" = 'develop' ]; then red "You should not delete $BRANCH" return 0 fi } 但是

失败于

# delete git branch
git--() {

  BRANCH=$1

  if [ -n $BRANCH]; then
    BRANCH=$(git rev-parse --abbrev-ref HEAD)
  fi

  if [ "$BRANCH" = 'master' || "$BRANCH" = 'develop' ]; then

    red "You should not delete $BRANCH"
    return 0
  fi
}
但是如果我改变

如果[“$BRANCH”=“master”|“$BRANCH”=“developer”];然后

如果[“$BRANCH”=“master”];然后

一切正常。我该如何做或比较


谢谢

你有两个主要的选择(当然也有一些不那么直截了当的选择)

  • 使用
    zsh
    语法并使用
    […]
    而不是
    […]

    git--:[:8: ']' expected
    zsh: master: command not found...
    git--:8: command not found: master
    No branch specified therefore I'm using the current one: master
    On branch master
    nothing to commit, working tree clean
    Do you really want to delete the branch master (y/n)?
    
  • 使用
    -o
    而不是
    |
    ,以保持POSIX兼容

    if [[ "$BRANCH" = 'master' || "$BRANCH" = 'develop' ]]; then
    
  • 如果代码只能使用
    zsh
    运行,我建议使用1


    出现错误消息的原因是
    [
    只是一个常规命令,而不是
    zsh
    语法的一部分。
    [
    期望
    ]
    作为最后一个参数,但
    |
    zsh
    语法的一部分,具有优先级。它充当命令之间的分隔符并分割“条件”分为两个命令

    if [ "$BRANCH" = 'master' -o "$BRANCH" = 'develop' ]; then
    

    如果第一个命令失败,则运行第二个命令

    运行第一个命令失败,因为缺少关闭的
    ]
    。这将导致错误消息:

    "$BRANCH" = 'develop' ]
    
    对于第二个命令,
    “$BRANCH”
    被值
    master
    替换。由于没有名为
    master
    的命令,因此返回错误消息

    git--:[:8: ']' expected
    

    你有两个主要的选择(当然也有一些不那么直截了当的选择)

  • 使用
    zsh
    语法并使用
    […]
    而不是
    […]

    git--:[:8: ']' expected
    zsh: master: command not found...
    git--:8: command not found: master
    No branch specified therefore I'm using the current one: master
    On branch master
    nothing to commit, working tree clean
    Do you really want to delete the branch master (y/n)?
    
  • 使用
    -o
    而不是
    |
    ,以保持POSIX兼容

    if [[ "$BRANCH" = 'master' || "$BRANCH" = 'develop' ]]; then
    
  • 如果代码只能使用
    zsh
    运行,我建议使用1


    出现错误消息的原因是
    [
    只是一个常规命令,而不是
    zsh
    语法的一部分。
    [
    期望
    ]
    作为最后一个参数,但
    |
    zsh
    语法的一部分,具有优先级。它充当命令之间的分隔符并分割“条件”分为两个命令

    if [ "$BRANCH" = 'master' -o "$BRANCH" = 'develop' ]; then
    

    如果第一个命令失败,则运行第二个命令

    运行第一个命令失败,因为缺少关闭的
    ]
    。这将导致错误消息:

    "$BRANCH" = 'develop' ]
    
    对于第二个命令,
    “$BRANCH”
    被值
    master
    替换。由于没有名为
    master
    的命令,因此返回错误消息

    git--:[:8: ']' expected
    

    您应该在调试模式下运行shell脚本,例如'zsh-x',它会在每个步骤中吐出哪些变量,这样您就可以很好地了解发生了什么

    分支变量在这里被删除;n检查分支是否有值,如果有,则更新它。我想你是想用-z开关

    zsh: master: command not found...
    
    我觉得你的剧本应该更像

      if [ -n $BRANCH]; then
        BRANCH=$(git rev-parse --abbrev-ref HEAD)
      fi
    

    您应该在调试模式下运行shell脚本,例如'zsh-x',它会在每个步骤中吐出哪些变量,这样您就可以很好地了解发生了什么

    分支变量在这里被删除;n检查分支是否有值,如果有,则更新它。我想你是想用-z开关

    zsh: master: command not found...
    
    我觉得你的剧本应该更像

      if [ -n $BRANCH]; then
        BRANCH=$(git rev-parse --abbrev-ref HEAD)
      fi
    

    尝试
    -o
    而不是
    |
    。尝试
    -o
    而不是
    |