Demonstrating practical applications
Here, I’m using various variables and arrays that were introduced in previous chapters. Let’s put this into practice with the following Bash script:
Figure 4.11 – Introducing BASH_REMATCH in a practical application
This example code can be found in the ch04_regex_02.sh
file in this chapter’s folder. In this script, I declared the user_list
variable on line 3. On line 6, I declared the pattern
variable. On line 8, I started a while
loop that reads each line of data from the $
user_list
variable.
On line 9, I used the match operator, =~
, to compare each line ($line
) against our regex pattern ($pattern
). These are referred to by the $line
and $pattern
variables, which are declared. When you use the match operator, the string on the left (represented by the $line
variable) is matched against the regex pattern on the right. If the pattern matches, the expression returns true (0); otherwise,...