Bash Reference Manual Reference Documentation for Bash



Yüklə 4,8 Kb.
Pdf görüntüsü
səhifə7/66
tarix08.10.2017
ölçüsü4,8 Kb.
#3723
1   2   3   4   5   6   7   8   9   10   ...   66

Chapter 3: Basic Shell Features
13
When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator
is considered a pattern and matched according to the rules described below in
Section 3.5.8.1 [Pattern Matching], page 31, as if the extglob shell option were
enabled. The ‘=’ operator is identical to ‘==’. If the nocasematch shell option
(see the description of shopt in Section 4.3.2 [The Shopt Builtin], page 64)
is enabled, the match is performed without regard to the case of alphabetic
characters. The return value is 0 if the string matches (‘==’) or does not match
(‘!=’)the pattern, and 1 otherwise. Any part of the pattern may be quoted to
force the quoted portion to be matched as a string.
An additional binary operator, ‘=~’, is available, with the same precedence as
‘==’ and ‘!=’. When it is used, the string to the right of the operator is consid-
ered an extended regular expression and matched accordingly (as in regex 3)).
The return value is 0 if the string matches the pattern, and 1 otherwise. If the
regular expression is syntactically incorrect, the conditional expression’s return
value is 2. If the nocasematch shell option (see the description of shopt in
Section 4.3.2 [The Shopt Builtin], page 64) is enabled, the match is performed
without regard to the case of alphabetic characters. Any part of the pattern
may be quoted to force the quoted portion to be matched as a string. Bracket
expressions in regular expressions must be treated carefully, since normal quot-
ing characters lose their meanings between brackets. If the pattern is stored
in a shell variable, quoting the variable expansion forces the entire pattern to
be matched as a string. Substrings matched by parenthesized subexpressions
within the regular expression are saved in the array variable BASH_REMATCH.
The element of BASH_REMATCH with index 0 is the portion of the string match-
ing the entire regular expression. The element of BASH_REMATCH with index n
is the portion of the string matching the nth parenthesized subexpression.
For example, the following will match a line (stored in the shell variable line) if
there is a sequence of characters in the value consisting of any number, including
zero, of space characters, zero or one instances of ‘a’, then a ‘b’:
[[ $line =~ [[:space:]]*(a)?b ]]
That means values like ‘aab’ and ‘ aaaaaab’ will match, as will a line containing
a ‘b’ anywhere in its value.
Storing the regular expression in a shell variable is often a useful way to avoid
problems with quoting characters that are special to the shell. It is sometimes
difficult to specify a regular expression literally without using quotes, or to keep
track of the quoting used by regular expressions while paying attention to the
shell’s quote removal. Using a shell variable to store the pattern decreases these
problems. For example, the following is equivalent to the above:
pattern=’[[:space:]]*(a)?b’
[[ $line =~ $pattern ]]
If you want to match a character that’s special to the regular expression gram-
mar, it has to be quoted to remove its special meaning. This means that in the
pattern ‘xxx.txt’, the ‘.’ matches any character in the string (its usual regular
expression meaning), but in the pattern ‘"xxx.txt"’ it can only match a literal
‘.’. Shell programmers should take special care with backslashes, since back-


Chapter 3: Basic Shell Features
14
slashes are used both by the shell and regular expressions to remove the special
meaning from the following character. The following two sets of commands are
not equivalent:
pattern=’\.’
[[ . =~ $pattern ]]
[[ . =~ \. ]]
[[ . =~ "$pattern" ]]
[[ . =~ ’\.’ ]]
The first two matches will succeed, but the second two will not, because in the
second two the backslash will be part of the pattern to be matched. In the
first two examples, the backslash removes the special meaning from ‘.’, so the
literal ‘.’ matches. If the string in the first examples were anything other than
‘.’, say ‘a’, the pattern would not match, because the quoted ‘.’ in the pattern
loses its special meaning of matching any single character.
Expressions may be combined using the following operators, listed in decreasing
order of precedence:
( expression )
Returns the value of expression. This may be used to override the
normal precedence of operators.
! expression
True if expression is false.
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.
The && and || operators do not evaluate expression2 if the value of expression1
is sufficient to determine the return value of the entire conditional expression.
3.2.4.3 Grouping Commands
Bash provides two ways to group a list of commands to be executed as a unit. When com-
mands are grouped, redirections may be applied to the entire command list. For example,
the output of all the commands in the list may be redirected to a single stream.
()
( list )
Placing a list of commands between parentheses causes a subshell environment
to be created (see Section 3.7.3 [Command Execution Environment], page 37),
and each of the commands in list to be executed in that subshell. Since the list
is executed in a subshell, variable assignments do not remain in effect after the
subshell completes.
{}
{ list; }


Chapter 3: Basic Shell Features
15
Placing a list of commands between curly braces causes the list to be executed
in the current shell context. No subshell is created. The semicolon (or newline)
following list is required.
In addition to the creation of a subshell, there is a subtle difference between these
two constructs due to historical reasons. The braces are reserved words, so they must
be separated from the list by blanks or other shell metacharacters. The parentheses are
operators, and are recognized as separate tokens by the shell even if they are not separated
from the list by whitespace.
The exit status of both of these constructs is the exit status of list.
3.2.5 Coprocesses
A coprocess is a shell command preceded by the coproc reserved word. A coprocess is
executed asynchronously in a subshell, as if the command had been terminated with the
‘&’ control operator, with a two-way pipe established between the executing shell and the
coprocess.
The format for a coprocess is:
coproc [NAME] command [redirections]
This creates a coprocess named NAME. If NAME is not supplied, the default name is
COPROC. NAME must not be supplied if command is a simple command (see Section 3.2.1
[Simple Commands], page 8); otherwise, it is interpreted as the first word of the simple
command.
When the coprocess is executed, the shell creates an array variable (see Section 6.7
[Arrays], page 91) named NAME in the context of the executing shell. The standard output
of command is connected via a pipe to a file descriptor in the executing shell, and that file
descriptor is assigned to NAME[0]. The standard input of command is connected via a pipe
to a file descriptor in the executing shell, and that file descriptor is assigned to NAME[1].
This pipe is established before any redirections specified by the command (see Section 3.6
[Redirections], page 32). The file descriptors can be utilized as arguments to shell commands
and redirections using standard word expansions. The file descriptors are not available in
subshells.
The process ID of the shell spawned to execute the coprocess is available as the value of
the variable NAME PID. The wait builtin command may be used to wait for the coprocess
to terminate.
Since the coprocess is created as an asynchronous command, the coproc command always
returns success. The return status of a coprocess is the exit status of command.
3.2.6 GNU Parallel
There are ways to run commands in parallel that are not built into Bash. GNU Parallel is
a tool to do just that.
GNU Parallel, as its name suggests, can be used to build and run commands in parallel.
You may run the same command with different arguments, whether they are filenames,
usernames, hostnames, or lines read from files. GNU Parallel provides shorthand references
to many of the most common operations (input lines, various portions of the input line,
different ways to specify the input source, and so on). Parallel can replace xargs or feed
commands from its input sources to several different instances of Bash.


Yüklə 4,8 Kb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   10   ...   66




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə