Read LPI Linux Certification in a Nutshell Online
Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger
Tags: #Reference:Computers
break
break [n
]
Exit from the innermost (most deeply nested)for
,while
, oruntil
loop or from then
innermost levels of the loop.
case
casestring
inpattern1
)commands1
;;pattern2
)commands2
;;
...
esac
Choosestring
from among
a series of possible patterns. These patterns use the same form as
file globs (wildcards). Ifstring
matches patternpattern1
, perform the
subsequentcommands1
. If string matchespattern2
, performcommands2
. Proceed down the list of
patterns until one is found. To catch all remaining strings, use*)
at the end.
continue
continue [n
]
Skip remaining commands in afor
,while
, oruntil
loop, resuming with the next
iteration of the loop (or skippingn
loops).
echo
echo [options
] [string
]
Writestring
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.
Enable interpretation of escape characters.
Suppress the trailing newline in the output.
Sound an audible alert.
Insert a backspace.
Suppress the trailing newline (same as-n
).
Form feed.
exit
exit [n
]
Exit a shell script with statusn
. The value forn
can be 0 (success) or nonzero
(failure). Ifn
is not given, the exit
status is that of the most recent command.
if ! test -f somefile
then
echo "Error: Missing file somefile"
exit 1
fi
for
forx
inlist
do
commands
done
Assign each word inlist
tox
in turn and executecommands
. Iflist
is omitted, it is assumed that
positional parameters from the command line, which are stored in$@
, are to be used.
for filename in bigfile* ; do
echo "Compressing $filename"
gzip $filename
done
function
functionname
{commands
}
Define functionname
.
Positional parameters ($1
,$2
, ...) can be used withincommands
.
#function myfunc
{
echo "parameter is $1"
}
#myfunc 1
parameter is 1
#myfunc two
parameter is two
getopts
getoptsstring name
[args
]
Process command-line arguments (orargs
, 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. Thestring
contains the option letters to be recognized by
getopts
when running the script. Valid
options are processed in turn and stored in the shell variablename
. 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.
if
ifexpression1
thencommands1
elifexpression2
thencommands2
elsecommands
fi
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
kill
kill [options] IDs
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 isTERM
, instructing processes to shut
down.
List the signal names.
signal
or-signal
Specify the signal number or name.
read
read [options] variable1
[variable2
...]
Read one line of standard input, and assign each
word to the corresponding variable, with all remaining words
assigned to the last variable.
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
.
return
return [n
]
This command is used inside a function definition to
exit the function with statusn
. Ifn
is omitted, the exit status of the
previously executed command is returned.
seq
seq [OPTION
]... LAST
seq [OPTION
]... FIRST LAST
seq [OPTION
]... FIRST INCREMENT LAST
Print a sequence of numbers. This is useful infor
andwhile
loops.
Equalize the output’s width by padding with leading
zeros.
Use the printf-style floating-point FORMAT.
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
shift
shift [n
]
Shift positional parameters downn
elements. Ifn
is omitted, the default is 1, so$2
becomes$1
,$3
becomes$2
, and so on.
source
sourcefile
[arguments
]
.file
[arguments
]
Read and execute lines infile
. Thefile
does not need to be executable but
must be in a directory listed inPATH
. The dot syntax is equivalent to
statingsource
.
test
testexpression
[expression
]
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 aroundexpression
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.
file
True iffile
exists and is
a directory
file
True iffile
exists
file
True iffile
exists and is
a regular file
file
True iffile
exists and is
a symbolic link
string
True if the length ofstring
is nonzero
file
True iffile
exists and is
readable
file
True iffile
exists and has
a size greater than zero
file
True iffile
exists and is
writable
file
True iffile
exists and is
executable
string
True if the length ofstring
is zero
file1
-
otfile2
True iffile1
is older thanfile2
string1
=
string2
True if the strings are equal
string1
!=
string2
True if the strings are not equal
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
until
untiltest-commands
docommands
done
Executetest-commands
(usually a
test
command), and if the exit
status is nonzero (that is, the test fails), performcommands
and repeat. Opposite ofwhile
.