Reputation: 1130
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...
#
)$1
$1
is /my/path/*.log
and the line in question is
/other/path*.log /yet/another.log /my/path/*.log {
{
and even more white space or notI 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
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"
:
[[ $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>
grep -E
may support a slightly different regular-expression dialect.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