LPI Linux Certification in a Nutshell (43 page)

Read LPI Linux Certification in a Nutshell Online

Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger

Tags: #Reference:Computers

BOOK: LPI Linux Certification in a Nutshell
8.17Mb size Format: txt, pdf, ePub
Configuration files

It’s a good assumption that every Linux user will want to define
a few aliases, functions, and environment variables to suit his or her
needs. However, it’s undesirable to manually enter them upon each
login or for each new invocation of
bash
. To set
up these things automatically,
bash
uses a number
of configuration files to set its operating environment when it
starts. Some of these files are used only upon initial login, whereas
others are executed for each instance of
bash
you
start, including at login time. Some of these configuration files are
system-wide files for all users to use, whereas others reside in your
home directory for your use alone.

bash
configuration files important
to Exam 102 are listed in
Table 13-1
.

Table 13-1. 
bash
configuration files

File

Description

/etc/profile

This is the system-wide
initialization file executed during login. It usually contains
environment variables, including an initial
PATH
, and startup
programs.

/etc/bashrc

This is another system-wide
initialization file that may be executed by a user’s
.bashrc
for each
bash
shell launched. It usually contains
functions and aliases.

~/.bash_profile

If this file exists, it is executed
automatically after
/etc/profile
during
login.

~/.bash_login

If
.bash_profile
doesn’t exist, this file is
executed automatically during login.

~/.profile

If neither
.bash_profile
nor
.bash_login
exists, this file is executed
automatically during login. Note that this is the original
Bourne shell configuration file.

~/.bashrc

This file is executed automatically
when
bash
starts. This includes login, as
well as subsequent interactive and noninteractive invocations
of
bash
.

~/.bash_logout

This file is executed automatically
during logout.

~/.inputrc

This file contains optional key
bindings and variables that affect how
bash
responds to keystrokes. By default,
bash
is configured to respond like the
Emacs editor.

Note

The syntax
~
(the
tilde) refers to the current user’s “home directory”
(usually
/home/username
). Although this
shortcut may not represent much of a savings in typing, some Linux
configurations may place users’ directories in various and sometimes
nonobvious places in the filesystem. Using the tilde syntax reduces
the need for you to know exactly where a user’s home directory is
located.

In practice, users will generally (and often unknowingly) use
the system-wide
/etc/profile
configuration file
to start. In addition, they’ll often have three personal files in
their home directory:
~/.bash_profile
,
~/.bashrc
, and
~/.bash_logout
. The local files are optional, and
bash
does not mind if one or all of them are not
available in your directory.

Each of these configuration files consists entirely of plain
text. They are typically simple, often containing just a few commands
to be executed in sequence to prepare the shell environment for the
user. Since they are evaluated by
bash
as lines
of program code, they are said to be
sourced
, or
interpreted, when
bash
executes them.

Like most programming languages, shell programs allow the use of
comments. Most shells, including
bash
, consider everything immediately following
the hash mark (
#
) on a single line
to be a comment. An important exception is the
$#
variable
, which has nothing to do with comments
but contains the number of positional parameters passed to a function.
Comments can span an entire line or share a line by following program
code. All of your shell scripts and configuration files should use
comments liberally.

Files sourced at login time are created mainly to establish
default settings. These settings include such things as where to
search for programs requested by the user (the
PATH
) and creation of shortcut names for
commonly used tasks (aliases and functions). After login, files
sourced by each subsequent shell invocation won’t explicitly need to
do these things again, because they
inherit
the environment established
by the login shell. Regardless, it isn’t unusual to see a user’s
.bashrc
file filled with all of their personal
customizations. It also doesn’t hurt anything, provided the
.bashrc
file is small and quick to
execute.

Although it is not necessary to have detailed knowledge of every
item in your shell configuration files, Exam 102 requires that you
understand them and that you can edit them to modify their behavior
and your resulting operating environment. The following examples are
typical of those found on Linux systems and are annotated with
comments.
Example 13-1
shows a
typical Linux systemwide
profile
. This file is executed by
every user’s
bash
process at login time. A few
environment variables and other parameters are set in it.

Example 13-1. An example system-wide bash profile

#!/bin/bash
# /etc/profile
pathmunge ()
{
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# ksh workaround
if [ -z "$EUID" -a -x /usr/bin/id ]; then
EUID=`id -u`
UID=`id -ru`
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
if [ -x /usr/bin/id ]; then
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
HOSTNAME=`/bin/hostname`
HISTSIZE=1000
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
INPUTRC=/etc/inputrc
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done
unset i
unset pathmunge

Example 13-2
shows a
system-wide
.bashrc
file. This file is not
executed by default when
bash
starts. Instead, it
is optionally executed by users’ local
.bashrc
files.

Example 13-2. An example system-wide .bashrc file

# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# By default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
# are we an interactive shell?
if [ "$PS1" ]; then
case $TERM in
xterm*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
else
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:\
${PWD/#$HOME/~}"; echo -ne "\007"'
fi
;;
screen)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:\
${PWD/#$HOME/~}"; echo -ne "\033\\"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=\
/etc/sysconfig/bash-prompt-default
;;
esac
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
fi
if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it gets undefined at the end of /etc/profile
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
. $i
fi
done
unset i
unset pathmunge
fi
# vim:ts=4:sw=4
alias more='less' # prefer the "less" pager
alias lsps='ls -l;ps' # a dubious command

Example 13-3
shows
an example user’s local
.bash_profile
. Note that
this file sources the user’s
.bashrc
file (which
in turn sources the system-wide
/etc/bashrc
), and
then goes on to local customizations.

Example 13-3. An example user’s .bash_profile file

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME

Example 13-4
shows
an individual’s
.bashrc
file.

Example 13-4. An example user’s .bashrc file

# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
lsps() { # Define a personal function
ls -l $1
ps aux | grep `/bin/basename $1`
}

Example 13-5
shows a short,
simple, and not uncommon
.bash_logout
file.
Probably the most likely command to find in a logout file is the
clear
command. Including a
clear
in your logout file is a nice way of being
certain that whatever you were doing just before you log out won’t
linger on the screen for the next user to ponder. This file is
intended to execute commands for a logout from a text session, such as
a system console or terminal. In a GUI environment where logout and
login are handled by a GUI program,
.bash_logout
might not be of much value.

Example 13-5. A simple .bash_logout file

# .bash_logout
# This file is executed when a user logs out of the system
/usr/bin/clear # Clear the screen
/usr/games/fortune # Print a random adage

On the Exam

Make certain that you understand the difference between
execution at login and execution at shell invocation, as well as
which of the startup files serves each of those purposes.

.inputrc

Among the many enhancements added to
bash
is the ability to perform as if your history
of commands is the buffer of an editor. That is, your command history
is available to you, and you may cut, paste, and even search among
command lines entered previously. This powerful capability can
significantly reduce typing and increase accuracy. By default,
bash
is configured to emulate the Emacs editor,
but a
vi
editing interface is also
available.

The portion of
bash
that handles this
function, and in fact handles all of the line input during interactive
use, is known as
readline
. Readline may be
customized by putting commands into an initialization file, which by
default is in your home directory and called
.inputrc
. For example, to configure
bash
to use
vi
-style editing
keys, add this line to
.inputrc
:

set editing-mode vi
Note

You may also set the
INPUTRC
variable to the name of another
file if you prefer. On your system, this variable may be set to
/etc/initrc
by default,
which would override any settings you put into a local
.initrc
. To use your own file, you must first
explicitly place the command
unset
INPUTRC
in your
.bash_profile
.

The default editing facilities enabled in
bash
are extensive and are beyond the scope of
this section and Exam 102. However, you need to understand the
concepts of adding your own custom key bindings to the
.inputrc
file and how they can help automate
common keystrokes unique to your daily routine for the test.

For example, suppose you often use
top
to
watch your system’s activity (
top
is a useful
process-monitoring utility that is described in
Chapter 6
):

$
top -Ssd1

If you do this often enough, you’ll get tired of typing the
command over and over and will eventually want an
alias for it. To create the alias, simply alias this
command to
top
:

$
alias top='/usr/bin/top -Ssd1'

Better yet, you can use
.inputrc
to create
a key binding that will enter it for you. Here’s how the
.inputrc
file would look if you were to bind your
top
command to the key sequence Ctrl-T:

# my .inputrc file
Control-t: "top -Ssd1 \C-m"

The lefthand side of the second line indicates the key
combination you wish to use (Ctrl-T). The righthand side indicates
what you wish to bind to that key sequence. In this case,
bash
outputs
top -Ssd1
and a
carriage return, denoted here by
\C-m
(
Ctrl-M
), when Control-T is
pressed.

Through modifications of your local configuration files, you can
customize your environment and automate many of your daily tasks. You
may also override system-wide settings in your personal files simply
by setting variables, aliases, and functions.

On the Exam

You won’t need to have detailed knowledge of this key-binding
syntax, but be aware of the
.inputrc
file and
the kinds of things it enables
bash
to
do.

Other books

Butterfly Weeds by Laura Miller
Vampire King of New York by Susan Hanniford Crowley
The Pantheon by Amy Leigh Strickland
Midnight Solitaire by Greg F. Gifune
The Vizard Mask by Diana Norman
Finding Fiona by Viola Grace
Sexpedida de soltera by Pandora Rebato