简体   繁体   中英

Bash: =~ and pattern matching in Git Bash 3.1

I basically want to do the following in Bash 3.1 under Windows (Git Bash):

#!/bin/bash
#set -x
shopt -s extglob
shopt -s nocasematch

declare file='[Vol 01] - 04 - This message'
declare filesafe="${file}"

declare pattern='\[Vol ([0-9]+)\] - ([0-9]+) -*'
if [[ "${file}" =~ $pattern ]]; then
  echo "regexp: $(printf "%s %-2d %-3d" "${filesafe}" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" ) - - -"
else
        echo "failed: $(printf "%s" "${filesafe}") - - - - -"
fi

The =~ does not work because it does not exists in this version of Bash.

Note: the script works with Bash 4.3.33 in Gentoo.

Version of Git & Bash are the following:

$ git --version ; bash --version
git version 1.9.5.msysgit.1
GNU bash, version 3.1.23(6)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.

In the sole documentation on Bash 3.1 that I could find, the version is:

This is Edition 3.1-beta1, last updated 5 September 2005, of The GNU Bash Reference Manual, for Bash, Version 3.1-beta1 .

This documentation state the =~ is available:

An additional binary operator, '=~', is available, with the same precedence as '==' and '!='.

Did I miss something like some option using shopt?

You can simply use a more recent version of that bash: use git-for-windows , which will soon replace the obsolete msysgit.

No setup required: uncompress PortableGit-2.4.6-5th-release-candidate-64-bit.7z.exe anywhere you want, and call c:\\path\\to\\PortableGit-2.4.6-5th-release-candidate-64-bit\\git-bash.exe .

You will get a 2013 4.3.39 bash (instead of the old 2005 3.1.20 bash of msysgit): that is one of the very latest 4.3 patches , May 2015.

That is more than enough to make =~ work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM