пятница, 16 апреля 2010 г.

LPI exam 102 prep: Shells, scripting, programming, and compiling

Краткие заметки, сделанные при подготовке к сдаче экзамена LPI 117-102 (Junior Level Linux Professional (LPIC-1)).

Shell customization

POSIX is the Portable Operating System Interface for uniX.
Well known shells include the Korn shell (ksh), the C shell (csh) and its derivative tcsh, the Almquist shell (ash) and its Debian derivative (dash).

Shells, including bash, allow you to create and modify shell
variables, which you may export to your environment for use by other processes
running in the shell or by other shells that you may spawn from the current shell.

Table 3. Common bash environment variables
Name Function
USER The name of the logged-in user
UID The numeric user id of the
logged-in user
HOME The user's home directory
PWD The current working directory
SHELL The name of the shell
$ The process id (or PID of the running Bash shell (or other) process)
PPID The process id of the process that started this process (that is, the id of the parent process)
? The exit code of the last command

When you create a shell variable, you will often want to export it to the environment
so it will be available to other processes that you start from this shell. Variables that
you export are not available to a parent shell.

In shells such as the C and tcsh shells, you use the set command to set variables in
your shell, and the setenv command to set and export variables. The syntax differs
slightly from that of the export command.

You remove a variable from the Bash shell using the unset command.

You can use
the -v option to be sure that you are removing a variable definition. Functions can
have the same name as variables, so use the -f if you want to remove a function
definition. Without either -f or -v, the bash unset command removes a variable
definition if it exists; otherwise, it removes a function definition if one exists.

If this shell is bash, then it executes several profile scripts before you get
control. If /etc/profile exists, it is executed first. Depending on your distribution, other
scripts in the /etc tree may also be executed, for example, /etc/bash.bashrc or
/etc/bashrc. Once the system scripts have run, a script in your home directory is run
if it exists. Bash looks for the files ~/.bash_profile, ~/.bash_login, and ~/.profile in that
order. The first one found is executed.

You may force bash to read profiles as if it were a login shell using the --login
option. If you do not want to execute the profiles for a login shell, specify the
--noprofile option. Similarly, if you want to disable execution of the ~/.bashrc file
for an interactive shell, start bash with the --norc option.

The Bash shell allows you to define aliases for commands.

You can also use the alias command to display all the aliases if you use it with no
options or with just the -p option, and you can display the aliases for one or more
names by giving the names as arguments without assignments. Listing 10 shows the
aliases for which and vi.

Sometimes called the "Swiss army knife" of the UNIX and Linux toolbox, sed is an
extremely powerful editing filter that uses regular expressions. You now understand
that the challenge is to strip off the first 8 words and the blanks that follow them from
every line of output that begins with 'd'. You can do it all with sed: select only those
lines you are interested in using the pattern-matching expression /^d/, substituting
a null string for the first eight words using the substitute command
s/^d\([^ ]* *\)\(8\}//. Use the -n option to print only lines that you specify
with the p command


Inside the function, you can refer to the parameters using the bash special variables
in Table 4. You prefix these with a $ symbol to reference them as with other shell
variables.

Shell parameters for functions

0, 1, 2, ... The positional parameters starting from
parameter 0. Parameter 0 refers to the
name of the program that started bash,
or the name of the shell script if the
function is running within a shell script.
See the bash man pages for
information on other possibilities, such
as when bash is started with the -c
parameter. A string enclosed in single
or double quotes will be passed as a
single parameter, and the quotes will
be stripped. In the case of double
quotes, any shell variables such as
$HOME will be expanded before the
function is called. You will need to use
single or double quotes to pass
parameters that contain embedded
blanks or other characters that might
have special meaning to the shell.

* The positional parameters starting from
parameter 1. If the expansion is done
within double quotes, then the
expansion is a single word with the first
character of the interfield separator
(IFS) special variable separating the
parameters or no intervening space if
IFS is null. The default IFS value is a
blank, tab, and newline. If IFS is unset,
then the separator used is a blank, just
as for the default IFS.

@ The positional parameters starting from
parameter 1. If the expansion is done
within double quotes, then each
parameter becomes a single word, so
that "$@" is equivalent to "$1" "$2" ....
If your parameters are likely to contain
embedded blanks, you will want to use
this form.

# The number of parameters, not
including parameter 0.

Shell scripts

test and [
The test builtin command returns 0 (True) or 1 (False) depending on the evaluation
of an expression expr. You can also use square brackets so that test expr and
[ expr ] are equivalent. You can examine the return value by displaying $?; you can
use the return value as you have before with && and ||; or you can test it using the
various conditional constructs that are covered later in this section.

(( and [[
The test command is very powerful, but somewhat unwieldy with its requirement
for escaping and the difference between string and arithmetic comparisons.
Fortunately bash has two other ways of testing that are somewhat more natural for
people who are familiar with C, C++, or Java syntax. The (( )) compound
command evaluates an arithmetic expression and sets the exit status to 1 if the
expression evaluates to 0, or to 0 if the expression evaluates to a non-zero value.
You do not need to escape operators between (( and )). Arithmetic is done on
integers. Division by 0 causes an error, but overflow does not. You may perform the
usual C language arithmetic, logical, and bitwise operations. The let command can
also execute one or more arithmetic expressions. It is usually used to assign values
to arithmetic variables.

If, then, else statements
The bash if command is a compound command that tests the return value of a test
or command ($? and branches based on whether it is True (0) or False (not 0).
Although the tests above returned only 0 or 1 values, commands may return other
values. You will learn more about testing those a little later in this tutorial. The if
command in bash has a then clause containing a list of commands to be executed
if the test or command returns 0. The command also has one or more optional elif
clauses. Each of these optional elif clauses has an additional test and then
clause with an associated list of commands, an optional final else clause, and list of
commands to be executed if neither the original test, nor any of the tests used in the
elif clauses was true. A terminal fi marks the end of the construct.

The Bash shell has a shopt builtin that allows you to set or unset many shell
options. One is nocasematch, which, if set, instructs the shell to ignore case in
string matching.

Loops
Bash and other shells have three looping constructs that are somewhat similar to
those in the C language. Each will execute a list of commands zero or more times.
The list of commands is surrounded by the words do and done, each preceded by a
semicolon.
for
loops come in two flavors. The most common form in shell scripting iterates
over a set of values, executing the command list once for each value. The set
may be empty, in which case the command list is not executed. The other form
is more like a traditional C for loop, using three arithmetic expressions to
control a starting condition, step function, and end condition.
while
loops evaluate a condition each time the loop starts and execute the command
list if the condition is true. If the condition is not initially true, the commands are
never executed.
until
loops execute the command list and evaluate a condition each time the loop
ends. If the condition is true the loop is executed again. Even if the condition is
not initially true, the commands are executed at least once.

Комментариев нет: