语法错误=〜在msysgit bash的运营商 [英] Syntax error =~ operator in msysgit bash

查看:161
本文介绍了语法错误=〜在msysgit bash的运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个功能添加到我的的.bash_profile 的的 msysgit

I'm trying to add a function to my bash_profile for msysgit:

function git-unpushed {
    brinfo=$(git branch -v | grep git-branch-name)
    if [[ $brinfo =~ ("[ahead "([[:digit:]]*)]) ]]
    then
        echo "(${BASH_REMATCH[2]})"
    fi
}

不过,我得到以下错误:

But I get the following error:

庆典:有条件的二元运算expected`

bash: conditional binary operator expected`

庆典:语法错误附近 =〜

这是我能找到的等于波浪操作符( =〜)的计算结果为在bash正则表达式。

From what I can find, the "equals tilde" operator (=~) evaluates as regex in bash.

为什么 =〜抛出一个错误?

Why is =~ is throwing an error?

更新:这是人工输入它的截图(这是运行sh.exe):

UPDATE: Here's a screenshot of inputting it manually (this is running sh.exe):

推荐答案

2015年更新:msysgit现在已经过时结果
您应该使用它自带的 混帐换窗户 。结果
正如提到这个答案,它采用了更近的bash(4.3+),为此, =〜语法会工作。

Update 2015: msysgit is now obsolete.
You should use the bash which comes with git-for-windows.
As mentioned in this answer, it uses a much more recent bash (4.3+), for which the =~ syntax will work.

原来的答复(2013年3月)

Original answer (march 2013)

与msysgit包装bash的可能仅仅是太老了,完全支持此运算符。结果
这当然是太旧了不带引号的正则表达式进行比较,如提到的的猛砸,第3版我如何使用常规的前pressions的bash脚本」:

The bash packaged with msysgit might simply be too old to fully support this operator.
It is certainly too old to compare with unquoted regex, as mentioned in "Bash, version 3" and "How do I use regular expressions in bash scripts?":

从版本猛砸,前pression 3.2〜匹配不再报价。

As of version 3.2 of Bash, expression to match no longer quoted.

其实, mklement0 提到的评论:

=〜在bash 3.0中引入的,一贯支持在RHS一个不带引号标记。结果
  截至3.1.X,引用令牌被视为与未加引号标记:两者都是PTED为正则表达式间$ P $结果。
  什么在3.2改变的是引用标记(或引用令牌子)现在都被视为的文本

=~ was introduced in bash 3.0 and always supported an unquoted token on the RHS.
Up to 3.1.x, quoted tokens were treated the same as unquoted tokens: both were interpreted as regexes.
What changed in 3.2 was that quoted tokens (or quoted substrings of a token) are now treated as literals.

但我试过的报价(在最新的msysgit 1.8.1.2),它仍然失败:

But I tried with quotes (in the latest msysgit 1.8.1.2), and it still fails:

vonc@voncvb /
$ /bin/bash --version
GNU bash, version 3.1.0(1)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.
vonc@voncvb /
$ variable="This is a fine mess."
vonc@voncvb /
$ echo "$variable"
This is a fine mess.
vonc@voncvb /
$ if [[ "$variable" =~ T.........fin*es* ]] ; then echo "ok" ; fi
bash: conditional binary operator expected
bash: syntax error near `=~'
vonc@voncvb /
$ if [[ "$variable" =~ "T.........fin*es*" ]] ; then echo "ok" ; fi
bash: conditional binary operator expected
bash: syntax error near `=~'
vonc@voncvb /

这篇关于语法错误=〜在msysgit bash的运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆