command string params
where command is one of the commands (substr, index, length),
string
is either a literal
string or a variable storing a string (or even a Linux operation that returns a string), and
params depend on the type of command as we will see below.
The substr command performs a substring operation. It returns the portion of the string
that starts at the index indicated by a start value whose length is indicated by a length
value. We specify both start and length in the substr command using the syntax
expr substr
string index length
where
string
is the string,
index
is an integer indicating the position in the string to start
the substring, and
length
is an integer indicating the length of the substring. For instance,
expr substr abcdefg 3 2
would return the string
cd
.
Any of string, index, and length can be stored in variables in which case we would ref-
erence the variable using $VAR. If NAME stores “Frank Zappa,” start is 7 and length is
3, then
expr substr "$NAME" $start $length
would return
Zap
. Notice that
$NAME is placed inside of “”. This is necessary only because NAME contains a blank. If
$NAME instead stored FrankZappa, the quotes would not be necessary.
The index command is given two strings and it returns the location within the first
string of the first character in the second string found. We can consider the first string
to be the master string and the second string to be a search string. The index returned is
the location in the master string of the first matching character of the search string. For
instance,
expr index abcdefg qcd
returns
3
because the first character to match
from qcd is c at index 3.
Shell Scripting
◾
257
The length command returns the length of a string. Unlike substr and index, there
are no additional parameters to length other than the string itself. So for instance,
you might use
expr length abcdefg
which would return
7
or
expr length
"$NAME"
which would return
11
using the value of NAME from a couple of para-
graphs back.
Each of these string operations will only work if you execute it in an expr command.
As with using expr with arithmetic expressions, these can be used as the right hand
side of an assignment statement in which case the entire expr must be placed inside ` `
marks or inside the notation $(). Here are some examples. Assume PHRASE stores “hello
world”
•
X=‘expr substr "$PHRASE" 4 6‘
• X would store “lo wor”
•
X=‘expr index "$PHRASE" nopq‘
• X would store 5
•
X=‘expr length "$PHRASE"‘
• X would store 11
7.3.6 Parameters
Parameters are specified by the user at the time that the script is invoked. Parameters
are listed on the command line after the name of the script. For instance, a script named
a_script, which expects numeric parameters, might be invoked as
./a_script 32 1 25
In this case, the values 32, 1, and 25 are made available to the script as parameters.
The parameters themselves are not variables. They cannot change value while the script
is executing. But they can be used in many types of instructions as if they were variables.
Parameters are accessed using the notation
$
n
where
n
is the index of the parameter (for
instance,
$1
for the first parameter,
$2
for the second).
As an example, the following script returns substrings of strings. The user specifies the
index and length of the substrings as parameters. The script then outputs for each string
defined in the script the corresponding substring.
#!/bin/bash
STR1
=
"Hello World"
STR2
=
"Frank Zappa Rocks!"
STR3
=
"abcdefg"
expr substr "$STR1" $1 $2
expr substr "$STR2" $1 $2
expr substr $STR3 $1 $2
258
◾
Linux with Operating System Concepts
If this script is called sub and is executed as .
/sub 2 3
, the output is
ell
ran
bcd
Notice in the first two expr statements that $STR1 and $STR2 are in “” because the
strings that they store have spaces. This is not necessary for $STR3.
Once a parameter has been used in your script, if you do not need it, you can use the
instruction
shift
which rotates all parameters down one position. This would move $2
into $1. This can simplify a script that will use each parameter in turn, one at a time so that
you only have to reference $1 and not other parameters. The proper usage of shift is in the
context of a loop. We will examine this later in this chapter.
Four special notations provide us not the parameters but information about the script.
The notation $0 returns the name of the script itself while $$ returns the PID of the script’s
running process. The notation $# returns the number of parameters that the user supplied.
Finally, $@ returns the entire group of parameters as a list. We can use $# to make sure that
the user supplied the proper number of parameters. We can use $@ in conjunction with the
for statement (covered in Section 7.5) to iterate through the entire list of parameters. Both
$# and $@ are discussed in more detail in Sections 7.4 and 7.5.
7.4 INPUT AND OUTPUT
7.4.1 Output with echo
We have already seen the output statement,
echo
. The echo statement is supplied a list of
items to output. These items are a combination of literal values, treated as strings, variables
(with the $ preceding their names), and Linux commands placed inside ` ` or $() marks.
You cannot use the expr statement though in this way.
Here are some examples of output statements. Assume that any variable listed actually
has a value stored in it.
•
echo Hello $NAME, how are you?
•
echo Your current directory is $PWD, your home is $HOME
•
echo Your current directory is ‘pwd‘, your home is ~
•
echo The date and time are $(date)
•
echo The number of seconds in a year is $((365*24*60*60))
The echo statement also permits a number of “escape” characters. These are characters
preceded by a back slash (\). You must force echo to interpret these characters, if present. This
is done through the –e option. Table 7.2 displays some of the more common escape charac-
ters available. To use the escape characters (with the exception of \\), the sequence including
any literal characters that go with the escape characters must be placed within “” marks.
Shell Scripting
◾
259
Here are some examples of using the escape characters and what they output
•
echo –e "Hello World!\nHow are you today?"
•
Hello World
•
How are you today?
•
echo –e "January\tFebruary\tMarch\tApril"
•
January February March April
•
echo –e "\x40\x65\x6C\x6C\x6F"
•
Hello
•
echo –e Hello\\World
•
Hello\World
(recall that the quote marks are not needed for \\)
The use of “” and ‘’ in echo statements is slightly different from their use in assignment
statement. As we see below, the quote marks appear to do the same thing as we saw with
assignment statements. The “” return values of variables and Linux commands whereas
in ‘’, everything is treated literally. Assume FIRST stores Frank and LAST stores Zappa.
•
echo $FIRST $LAST
•
Frank Zappa
•
echo "$FIRST $LAST"
•
Frank Zappa
•
echo '$FIRST $LAST'
•
$FIRST $LAST
•
echo "$FIRST \n$LAST"
•
Frank \nZappa
TABLE 7.2
Some Useful Escape Characters in Bash
Do'stlaringiz bilan baham: |