Read LPI Linux Certification in a Nutshell Online
Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger
Tags: #Reference:Computers
Depending upon the computing environments you’re used to, the concepts
of shells and shell programs (usually called
scripts
) may be a little foreign. On
Linux systems, the shell is a full programming environment that can be
scripted or used interactively.
This chapter covers Topic 105 and its three Objectives:
This Objective covers your shell and basic scripting concepts,
including environment variables, functions, and script files that
control the login environment. Weight: 4.
Customization of the many scripts found on a Linux system is
important for its management and automation. Topics for this Objective
include shell syntax, checking the status of executed programs, and
issues surrounding the properties of script files. Weight: 4.
This objective covers the basic use of SQL databases to store
and query data. Topics for this Objective include communicating with a
SQL database, basic queries, basic database concepts, and the
relationship between data and tables. Weight: 2.
It is important for Linux administrators to become comfortable with at
least one shell and its programming language. This can be an area of some
concern to those used to graphics-only environments, where the use of a
command interpreter is not a daily activity. As you’ll see, becoming adept
at working with your favorite shell will allow you to customize many trivial
tasks and become a more efficient system administrator.
This Objective could be considered a brief “getting started
with shells” overview because it details many of the basic concepts
necessary to utilize the shell environment on Linux. These concepts are
fundamental and very important for system administrators working on Linux
systems. If you’re new to shells and shell scripting, take heart. You can
think of it as a combination of computer interaction (conversation) and
computer programming (automation). It is nothing more than that, but the
result is far more than this simplicity implies. If you’re an old hand
with shell programming, you may want to skip ahead to brush up on some of
the particulars necessary for Exam 102.
If you’ve never taken a computer programming course before, don’t be
too discouraged. Shell programming is mostly automating repetitive tasks.
Your shell scripts can conceivably become relatively complicated programs
in their own right, but shell scripting does not have the learning curve
of a “conventional” programming language such as C or C++.
A shell is a fundamental and important part of your Linux
computing environment. Shells are user programs not unlike other
text-based programs and utilities. They offer a rich, customizable
interface to your system. Some of the main items provided by your shell
are:
In this role, the shell is a command interpreter and display
portal to the system. It offers you a communications channel to
the kernel and is often thought of as the “shell around the
kernel.” That’s where the name
shell
originates and is a good metaphor for conceptualizing how shells
fit into the overall Linux picture.
Shells set up an
environment
for the
execution of other programs, which affects the way some of them
behave. This environment consists of any number of
environment variables
, each of
which describes one particular environment property by defining aname=value
pair. Other features such as
aliases
enhance your operating
environment by offering shorthand notations for commonly used
commands.
Shells are used not only by users but also by the system to
launch programs and support those programs with an operating
environment.
Shells offer their own programming languages. At its
simplest, this feature allows user commands to be assembled into
useful sequences. At the other end of the spectrum, complete
programs can be written in shell languages, with loop control,
variables, and all of the capabilities of Linux’s rich set of
operating system commands.
All of the shells share some common concepts:
They are all distinct from the kernel and run as user
programs.
Each shell can be customized by tuning the shell’s operating
environment.
Shells are run for both interactive use by end users and
noninteractive use by the system.
A shell can be run from within another shell, enabling you to
try a shell other than your default shell. To do this, you simply
start the other shell from the command line of your current shell.
In fact, this happens constantly on your system as scripts are
executed and programs are launched. The new shell does not replace
the shell that launched it; instead, the new shell is a process
running with the original shell as a parent process. When you
terminate the child shell, you go back to the original one.
Shells use a series of
configuration files
in order to
establish their operating
environment
.
Shells pass on environment variables to child
processes.
bash
is the GNU Project
implementation of the standard Unix shell
sh
. Since
the original
sh
was the “Bourne shell,”
bash
is the “Bourne again shell”. As the
bash
home
page
says:
Bash is an
sh
-compatible shell that
incorporates useful features from the Korn shell
(
ksh
) and C shell (
csh
). It
is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and
Tools standard. It offers functional improvements over
sh
for both programming and interactive
use.
While there are a number of shells available to choose from on a
Linux system,
bash
is very popular and powerful,
and it is the default shell for new accounts. Bash has become popular
enough that it is available on many other Unix flavors as well,
including Sun’s Solaris and Hewlett-Packard’s HP/UX. Exam 102
concentrates on its use and configuration. The next few sections deal
with common shell concepts, but the examples are specific to
bash
.
Many programs running under Linux require information
about you and your personal preferences to operate sensibly. Although
you could manually provide this information to each program you run,
much of the information you’d convey would be redundant because you’d
be telling every command you enter the same ancillary information at
each invocation. For example, you’d need to tell your paging program
about the size and nature of your terminal or terminal window each
time you use it. You would also need to give fully qualified directory
names for the programs you run.
Rather than force users to include so much detail to issue
commands, the shell handles much of this information for you
automatically. You’ve already seen that the shell creates an operating
environment for you. That
environment is made up of a series of
variables
, each of which has a value that is used
by programs and other shells. The two types of variables used by most
shells are:
These variables can be thought of as
global variables
because they
are passed on to all processes started by the shell, including
other shells. This means that child processes inherit the
environment. By convention, environment variables are given
uppercase names.
bash
doesn’t require the
case convention; it is just intended for clarity to humans.
However, variable names are case-sensitive. Your shell maintains
many environment variables, including the following
examples
:
PATH
A colon-delimited list of directories through which
the shell looks for executable programs as you enter them
on the command line. All of the
directories
that contain
programs that you’ll want to execute are stored together
in thePATH
environment
variable. Your shell looks through this list in sequence,
from left to right, searching for each command you enter.
YourPATH
may differ
from thePATH
s of other
users on your system because you may use programs found in
different locations or you may have a local directory with
your own custom programs that need to be available. ThePATH
variable can
become quite long as more and more directories are
added.
HOME
Your home directory, such as
/home/adamh
.
USERNAME
Your username.
TERM
The type of terminal or terminal window you are
running. This variable is likely to have a value such asxterm
orxterm-color
. If you are running
on a physical VT100 (or compatible) terminal,TERM
is set tovt100
.
These variables can be thought of as
local
because they are
specific only to the current shell. Child processes do not
inherit them. Some shell variables are automatically set by the
shell and are available for use in shell scripts. By convention,
shell variables are given lowercase names.
To create a new
bash
shell variable, simply
enter aname=value
pair on the command
line:
#pi=3.14159
To see that this value is now assigned to the local variablepi
, use the
echo
command to display its contents:
#echo $pi
3.14159
The dollar sign preceding the variable name indicates that the
name will be replaced with the variable’s value. Without the dollar
sign,
echo
would just return the text that was
typed, which in this case is the variable namepi
. At this point,pi
is a local variable and is not available
to child shells or programs. To make it available to other shells or
programs, the variable must be exported to the environment:
#export pi
Among the features missing from
sh
was the ability to easily make new commands or modify existing
commands.
bash
has the ability to set an alias
for commonly used commands or sequences of commands. For example, if
you habitually call for the older pager
more
but
actually prefer
less
, an alias can be handy to
get the desired behavior, regardless of the command you use:
$alias more='less'
This has the effect of intercepting any command entries for
more
, substituting
less
. The
revised command is passed along to the shell’s command
interpreter.
Another common use for an alias is to modify a command slightly
so that its default behavior is more to your liking. Many people,
particularly when operating with superuser privileges, will use this
alias:
$alias cp='cp -i'
With this alias in effect, the use of the
cp
(copy) command becomes safer, because with the
-i
option always enforced by the alias,
cp
prompts you for approval before overwriting a
file of the same name. Additional options you enter on the command
line are appended to the end of the new command, such that
cp -p
becomes
cp -i
-p
, and so on.
If the righthand side of the aliased command is bigger than a
single word or if it contains multiple commands (separated by
semicolons,
bash
’s command terminator), you
probably need to enclose it in single quotation marks to get your
point across. For example, suppose you wished to use a single alias to
pair two simple commands:
$alias lsps=ls -l; ps
Your current
bash
process will interpret
this command not as a single alias but as two separate commands. First
the alias
lsps
will be created for
ls
-l
, and then a
ps
command will be
added for immediate execution. What you really want is:
$alias lsps='ls -l; ps'
Now, entering the command
lsps
will be
aliased to
ls -l; ps
, and will correctly generatels
output immediately followed byps
output, as this example
shows:
$lsps
total 1253
drwx------ 5 root root 1024 May 27 17:15 dir1
drwxr-xr-x 3 root root 1024 May 27 22:41 dir2
-rw-r--r-- 1 root root 23344 May 27 22:44 file1
drwxr-xr-x 2 root root 12288 May 25 16:13 dir3
PID TTY TIME CMD
892 ttyp0 00:00:00 bash
1388 ttyp0 00:00:00 ps
Admittedly, this isn’t a very useful command, but it is built
upon in the next section.
After adding aliases, it may become easy to confuse which
commands are aliases or native. To list the aliases defined for your
current shell, simply enter the
alias
command by
itself. This results in a listing of all the aliases currently in
place:
$alias
alias cp='cp -i'
alias lsps='ls -l;ps'
alias mv='mv -i'
alias rm='rm -i'
Note that aliases are local to your shell and are not passed
down to programs or to other shells. You’ll see how to ensure that
your aliases are always available in the section
Configuration files
.
Aliases are mainly used for simple command replacement. The
shell inserts your aliased text in place of your alias name before
interpreting the command. Aliases don’t offer logical constructs and
are limited to a few simple variable replacements. Aliases can also
get messy when the use of complicated quoting is necessary, usually to
prevent the shell from interpreting characters in your alias.
In addition to aliases,
bash
also
offers
functions
. They work in much the same way
as aliases, in that some function name of your choosing is assigned to
a more complex construction. However, in this case that construction
is a small program rather than a simple command substitution.
Functions have a simple syntax:
[
function
]
name
() {
command-list
; }
This declaration defines a function calledname
.function
is optional, and the parentheses aftername
are required iffunction
is omitted. The
body of the function is thecommand-list
between the curly brackets ({
and}
). This list is a series of
commands, separated by semicolons or by newlines. The series of
commands is executed whenevername
is
specified as a command. The simplelsps
alias shown earlier could be
implemented as a function like this:
$lsps () { ls -l; ps; }
Using this new function as a command yields exactly the same
result the alias did. However, if you implement this command as a
function, parameters can be added to the command. Here is a new
version of the same function, this time entered on multiple lines
(which eliminates the need for semicolons within the function):
$lsps () {
>ls -l $1
>ps aux | grep `/bin/basename $1`
>}
The>
characters come from
bash
during interactive entry, indicating that
bash
is awaiting additional function commands or
the}
character, which terminates
the function definition (this is called the secondary shell prompt).
This new function allows us to enter a single
argument
to the function, which is
inserted everywhere$1
is found in
the function. These arguments are called
positional parameters
because each
one’s number denotes its position in the argument list. This example
uses only one positional parameter, but there can be many, and the
number of parameters is stored for your use in a special variable$#
.
The command implemented in the previous example function now
prints to
STDOUT
a directory listing and process
status for any program given as an argument. For example, if the
Apache web server is running, the command:
$lsps /usr/sbin/httpd
yields a directory listing for
/usr/sbin/httpd
and also displays all currently
running processes that matchhttpd
:
-rwxr-xr-x 1 root root 317072 2010-01-22 14:31 /usr/sbin/httpd
root 1882 0.0 1.5 22664 8088 ? Ss Aug10 0:14 /usr/sbin/httpd
apache 20869 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd
apache 20870 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd
apache 20872 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd
apache 20874 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd
apache 20875 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd
apache 20876 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd
apache 20877 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd
apache 20878 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd