command string params
‘
.
There is another command we could use called match. But before we look at match, there
290
◾
Linux with Operating System Concepts
is a simpler way to obtain a substring. We do this through
${string:start:length}
where
start
and
length
are integer numbers or variables storing integer numbers. This
operation returns the substring of
string
starting at
start
with
length
characters. As with
the array, but unlike using the
expr substr
command, the first character of a string is
at location 0 so that
${string:2:4}
would actually provide the third, fourth, fifth, and
sixth characters of the string, or the equivalent of
‘expr substr $string 3 4‘
.
Imagine that we have three variables, First, Middle, and Last, which are a person’s first
name, middle name, and last name. We want to form a variable, Initials, to store the initials
of the three names. We could do this in one of two ways.
Initials
=
"‘expr substr $First 1 1‘
‘expr substr $Middle 1 1‘‘expr substr $Last 1 1‘“
or
Initials
=
${First:0:1}${Middle:0:1}${Last:0:1}
When using the
${string:start:length}
notation, if the value for
start
is greater
than the length of the string, nothing is returned but it does not yield an error. If the value
for
length
is larger than the remaining length of the string, it returns the rest of the string.
For instance, if
name
=
"Frank Zappa"
then
${name:6:15}
returns “Zappa.” Note
that if you omit length, it defaults to the remainder of the string, for instance
${name:0}
returns “Frank Zappa” and
${name:3}
returns “nk Zappa.”
You can also use a negative value for
start
. This indicates that counting will start from
the right end of the string rather than the left end. For this to work, you must either place
the negative value in parentheses or add a space after the colon. For instance,
${name:
-3}
returns “ppa” and
${name:(-7)}
returns “k Zappa.” If you fail to include the space or
parenthesize the negative number, the entire string is returned. You can combine the nega-
tive number with a length, which operates as you would expect; it returns
length
characters
starting from the right side as indicated by the negative start value. For instance,
${name:
-7:3}
returns “k Z” and
${name:(-4:3)}
returns “app.”
Just as the notation
${string:start:length}
is a shortcut for
‘expr substr‘
,
we can employ a shortcut for
‘expr length $string‘
. In this case, the notation is
${#string}
, which is similar to the notation used to obtain the number of items in an
array. Using name from the previous examples, echo
${#name}
will return 11 (11 charac-
ters in the string “Frank Zappa”).
7.8.2 String Regular Expression Matching
Let us return to the expr command and look at another string operation known as
match
.
This allows us to compare a string to a regular expression. The expr operation then returns
the portion of the string that matched the regular expression. Unlike grep, match will only
begin matching from the left hand side of the string. The notation is
Shell Scripting
◾
291
‘
expr match "string" '\(
Do'stlaringiz bilan baham: |