LPI Linux Certification in a Nutshell (45 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
6.27Mb size Format: txt, pdf, ePub
Name

break

Syntax
break [
n
]
Description

Exit from the innermost (most deeply nested)
for
,
while
, or
until
loop or from the
n
innermost levels of the loop.

Name

case

Syntax
case
string
in
pattern1
)
commands1
;;
pattern2
)
commands2
;;
...
esac
Description

Choose
string
from among
a series of possible patterns. These patterns use the same form as
file globs (wildcards). If
string
matches pattern
pattern1
, perform the
subsequent
commands1
. If string matches
pattern2
, perform
commands2
. Proceed down the list of
patterns until one is found. To catch all remaining strings, use
*)
at the end.

Name

continue

Syntax
continue [
n
]
Description

Skip remaining commands in a
for
,
while
, or
until
loop, resuming with the next
iteration of the loop (or skipping
n
loops).

Name

echo

Syntax
echo [
options
] [
string
]
Description

Write
string
to standard
output, terminated by a newline. If no string is supplied, echo
only a newline. Some Linux distributions have a version of
echo
at
/bin/echo
. If
that is the case, the built-in
bash
version
of
echo
will usually take precedence.

Frequently used options
-e

Enable interpretation of escape characters.

-n

Suppress the trailing newline in the output.

Useful special characters
\a

Sound an audible alert.

\b

Insert a backspace.

\c

Suppress the trailing newline (same as
-n
).

\f

Form feed.

Name

exit

Syntax
exit [
n
]
Description

Exit a shell script with status
n
. The value for
n
can be 0 (success) or nonzero
(failure). If
n
is not given, the exit
status is that of the most recent command.

Example
if ! test -f somefile
then
echo "Error: Missing file somefile"
exit 1
fi
Name

for

Syntax
for
x
in
list
do
commands
done
Description

Assign each word in
list
to
x
in turn and execute
commands
. If
list
is omitted, it is assumed that
positional parameters from the command line, which are stored in
$@
, are to be used.

Example
for filename in bigfile* ; do
echo "Compressing $filename"
gzip $filename
done
Name

function

Syntax
function
name
{
commands
}
Description

Define function
name
.
Positional parameters (
$1
,
$2
, ...) can be used within
commands
.

Example
#
function myfunc
{
echo "parameter is $1"
}
#
myfunc 1
parameter is 1
#
myfunc two
parameter is two
Name

getopts

Syntax
getopts
string name
[
args
]
Description

Process command-line arguments (or
args
, if specified) and check for legal
options. The
getopts
is used
in shell script loops and is intended to ensure standard syntax
for command-line options. The
string
contains the option letters to be recognized by
getopts
when running the script. Valid
options are processed in turn and stored in the shell variable
name
. If an option letter is followed
by a colon, the option must be followed by one or more arguments
when the command is entered by the user.

Name

if

Syntax
if
expression1
then
commands1
elif
expression2
then
commands2
else
commands
fi
Description

The
if
command is used to
define a conditional statement. There are three possible formats
for using the
if
command:

if-then-fi
if-then-else-fi
if-then-elif-then-...fi
Name

kill

Syntax
kill [
options] IDs
Description

Send signals to each specified process or job ID,
which you must own unless you are a privileged user. The default
signal sent with the
kill
command is
TERM
, instructing processes to shut
down.

Options
-l

List the signal names.

-s
signal
or
-signal

Specify the signal number or name.

Name

read

Syntax
read [
options] variable1
[
variable2
...]
Description

Read one line of standard input, and assign each
word to the corresponding variable, with all remaining words
assigned to the last variable.

Example
echo -n "Enter last-name, age, height, and weight > "
read lastname everythingelse
echo $lastname
echo $everythingelese

The name entered is placed in variable
$lastname
; all of the other values,
including the spaces between them, are placed in
$everythingelse
.

Name

return

Syntax
return [
n
]
Description

This command is used inside a function definition to
exit the function with status
n
. If
n
is omitted, the exit status of the
previously executed command is returned.

Name

seq

Syntax
seq [
OPTION
]... LAST
seq [
OPTION
]... FIRST LAST
seq [
OPTION
]... FIRST INCREMENT LAST
Description

Print a sequence of numbers. This is useful in
for
and
while
loops.

Frequently used options
-w

Equalize the output’s width by padding with leading
zeros.

-f
or
--format=FORMAT

Use the printf-style floating-point FORMAT.

Example
year=$(date +%Y) # get current year
for month in $(seq -w 1 12)
{
monthname=$(date -d "${year}-${month}-01" +%B)
echo "Month $month is $monthname"
}
Month 01 is January
Month 02 is February
Month 03 is March
Month 04 is April
Month 05 is May
Month 06 is June
Month 07 is July
Month 08 is August
Month 09 is September
Month 10 is October
Month 11 is November
Month 12 is December
Name

shift

Syntax
shift [
n
]
Description

Shift positional parameters down
n
elements. If
n
is omitted, the default is 1, so
$2
becomes
$1
,
$3
becomes
$2
, and so on.

Name

source

Syntax
source
file
[
arguments
]
.
file
[
arguments
]
Description

Read and execute lines in
file
. The
file
does not need to be executable but
must be in a directory listed in
PATH
. The dot syntax is equivalent to
stating
source
.

Name

test

Syntax
test
expression
[
expression
]
Description

Evaluate the conditional expression and return a
status of 0 (true) or 1 (false). The first form explicitly calls
out the
test
command. The second form implies
the
test
command. The spaces around
expression
are required in the second
form.
expression
is constructed using
options. Some Linux distributions have a version of
test
at
/usr/bin/test
.
If that is the case, the built-in
bash
version of
test
will usually take
precedence.

Frequently used options
-d
file

True if
file
exists and is
a directory

-e
file

True if
file
exists

-f
file

True if
file
exists and is
a regular file

-L
file

True if
file
exists and is
a symbolic link

-n
string

True if the length of
string
is nonzero

-r
file

True if
file
exists and is
readable

-s
file

True if
file
exists and has
a size greater than zero

-w
file

True if
file
exists and is
writable

-x
file

True if
file
exists and is
executable

-z
string

True if the length of
string
is zero

file1
-
ot
file2

True if
file1
is older than
file2

string1
=
string2

True if the strings are equal

string1
!=
string2

True if the strings are not equal

Example

To determine if a file exists and is readable, use the
-r
option:

if test -r file
then
echo "file exists"
fi

Using the
[ ]
form
instead, the same test looks like this:

if [ -r file ]
then
echo "file exists"
fi
Name

until

Syntax
until
test-commands
do
commands
done
Description

Execute
test-commands
(usually a
test
command), and if the exit
status is nonzero (that is, the test fails), perform
commands
and repeat. Opposite of
while
.

Other books

The Twisted by Joe Prendergast
Continental Breakfast by Ella Dominguez
Deadline by Simon Kernick
The Fat Burn Revolution by Julia Buckley
The Taming of the Shrew by William Shakespeare
Swag Bags and Swindlers by Dorothy Howell
Nicola Griffith by Slow River
Hidden Fire by Alexis Fleming