bash - What does this variable assignment do? -
i'm having code subversion hook script, , found few examples online, python , perl. found 1 or 2 shell scripts (bash) well. confused line , sorry basic question.
filter=".(sh|sh|exe|exe|bat|bat)$"
the script later uses perform test, such (assume ext=ex):
if [[ "$filter" == *"$ext"* ]]; blah
my problem above test true. however, i'm not asking assist in writing script, explaining initial assignment of filter. don't understand line.
editing in closer example filter line. of course script, written not work, because 'ex' returns true, , not 'exe'. problem here only, however, don't understant layout of variable assignment itself.
why there period @ beginning? ".(sh..."
why there dollar sign @ end? "...bat)$"
why there pipes between each pattern? "sh|sh|exe"
you looking next:
filter="\.(sh|sh|exe|exe|bat|bat)$" ext if [[ "$ext" =~ $filter ]]; echo $ext extension disallowed else echo $ext allowed fi done
save myscript.sh
, run as
myscript.sh bash ba.sh
and get
bash allowed ba.sh extension disallowed
if don't escape "dot", e.g. filter=".(sh|sh|exe|exe|bat|bat)$"
get
bash extension disallowed ba.sh extension disallowed
what (of course) wrong.
for questions:
why there period @ beginning? ".(sh..."
because want match .sh
(as extension) , not example bash
(without dot). , therefore .
must escaped, \.
because .
in regex mean "any character.
why there dollar sign @ end? "...bat)$"
the $
mean = end of string. want match file.sh
, not file.sh.jpg
. .sh
should @ end of string.
why there pipes between each pattern? "sh|sh|exe"
in rexex, (...|...|...)
construction delimites "alternatives". sure quessed.
you need read "regex tutorial" - more complicated - , can't explained in 1 answer.
ps: never use uppercase variable names, can collide environment variables.
Comments
Post a Comment