user1011471
user1011471

Reputation: 1130

bash 2.0 string matching

I'm on GNU bash, version 2.05b.0(1)-release (2002). I'd like to determine whether the value of $1 is a path in one of those /path/*.log rules in, say, /etc/logrotate.conf. It's not my box so I can't upgrade it.

Edit: my real goal is given /path/actual.log answer whether it is already governed by logrotate or if all the current rules miss it. I wonder then if my script should just run logrotate -d /etc/logrotate.conf and see if /path/actual.log is in the output. This seems simpler and covers all the cases as opposed to this other approach.

But I still want to know how to approach string matching in Bash 2.0 in general...

I have something that seems to work in Bash 4 but not in Bash 2.05. Where can I go to read what Bash 2.0 supports? How would this matching be checked in Bash 2.0?

Upvotes: 0

Views: 390

Answers (1)

mklement0
mklement0

Reputation: 439767

You can find a terse bash changelog here.

You'll see that =~, the regex-matching operator, didn't get introduced until version 3.0.

Thus, your best bet is to use a utility to perform the regex matching for you; e.g.:

if grep -Eq '<your-extended-regex>' <<<"$1"; then ...

grep -Eq '<your-extended-regex>' <<<"$1":

  • IS like [[ $1 =~ <your-extended-regex> ]] in Bash 3.0+ in that its exit code indicates whether the literal value of $1 matches the extended regex <your-extended-regex>
    • Note that Bash 3.1 changed the interpretation of the RHS to treat quoted (sub)strings as literals.
    • Also note that grep -E may support a slightly different regular-expression dialect.
  • is NOT like it in that the grep solution cannot return capture groups; by contrast, Bash 3.0+ provides the overall match and capture groups via special array variable ${BASH_REMATCH[@]}.

Upvotes: 2

Related Questions