Bash Reference Manual Reference Documentation for Bash



Yüklə 4,8 Kb.
Pdf görüntüsü
səhifə6/66
tarix08.10.2017
ölçüsü4,8 Kb.
#3723
1   2   3   4   5   6   7   8   9   ...   66

Chapter 3: Basic Shell Features
10
Bash provides looping constructs, conditional commands, and mechanisms to group
commands and execute them as a unit.
3.2.4.1 Looping Constructs
Bash supports the following looping constructs.
Note that wherever a ‘;’ appears in the description of a command’s syntax, it may be
replaced with one or more newlines.
until
The syntax of the until command is:
until test-commands; do consequent-commands; done
Execute consequent-commands as long as test-commands has an exit status
which is not zero. The return status is the exit status of the last command
executed in consequent-commands, or zero if none was executed.
while
The syntax of the while command is:
while test-commands; do consequent-commands; done
Execute consequent-commands as long as test-commands has an exit status
of zero. The return status is the exit status of the last command executed in
consequent-commands, or zero if none was executed.
for
The syntax of the for command is:
for name [ [in [words ...] ] ; ] do commands; done
Expand words, and execute commands once for each member in the resultant
list, with name bound to the current member. If ‘in words’ is not present, the
for command executes the commands once for each positional parameter that
is set, as if ‘in "$@"’ had been specified (see Section 3.4.2 [Special Parameters],
page 20). The return status is the exit status of the last command that executes.
If there are no items in the expansion of words, no commands are executed, and
the return status is zero.
An alternate form of the for command is also supported:
for (( expr1 ; expr2 ; expr3 )) ; do commands ; done
First, the arithmetic expression expr1 is evaluated according to the rules de-
scribed below (see Section 6.5 [Shell Arithmetic], page 89). The arithmetic
expression expr2 is then evaluated repeatedly until it evaluates to zero. Each
time expr2 evaluates to a non-zero value, commands are executed and the arith-
metic expression expr3 is evaluated. If any expression is omitted, it behaves as
if it evaluates to 1. The return value is the exit status of the last command in
commands that is executed, or false if any of the expressions is invalid.
The break and continue builtins (see Section 4.1 [Bourne Shell Builtins], page 42) may
be used to control loop execution.
3.2.4.2 Conditional Constructs
if
The syntax of the if command is:
if test-commands; then
consequent-commands;


Chapter 3: Basic Shell Features
11
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi
The test-commands list is executed, and if its return status is zero, the
consequent-commands list is executed. If test-commands returns a non-zero
status, each elif list is executed in turn, and if its exit status is zero, the
corresponding more-consequents is executed and the command completes. If
‘else alternate-consequents’ is present, and the final command in the final
if or elif clause has a non-zero exit status, then alternate-consequents is
executed. The return status is the exit status of the last command executed,
or zero if no condition tested true.
case
The syntax of the case command is:
case word in [ [(] pattern [| pattern]...) command-list ;;]... esac
case will selectively execute the command-list corresponding to the first pattern
that matches word. If the nocasematch shell option (see the description of
shopt in Section 4.3.2 [The Shopt Builtin], page 64) is enabled, the match is
performed without regard to the case of alphabetic characters. The ‘|’ is used
to separate multiple patterns, and the ‘)’ operator terminates a pattern list. A
list of patterns and an associated command-list is known as a clause.
Each clause must be terminated with ‘;;’, ‘;&’, or ‘;;&’. The word under-
goes tilde expansion, parameter expansion, command substitution, arithmetic
expansion, and quote removal before matching is attempted.
Each pattern
undergoes tilde expansion, parameter expansion, command substitution, and
arithmetic expansion.
There may be an arbitrary number of case clauses, each terminated by a ‘;;’,
‘;&’, or ‘;;&’. The first pattern that matches determines the command-list that
is executed. It’s a common idiom to use ‘*’ as the final pattern to define the
default case, since that pattern will always match.
Here is an example using case in a script that could be used to describe one
interesting feature of an animal:
echo -n "Enter the name of an animal: "
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
horse | dog | cat) echo -n "four";;
man | kangaroo ) echo -n "two";;
*) echo -n "an unknown number of";;
esac
echo " legs."
If the ‘;;’ operator is used, no subsequent matches are attempted after the first
pattern match. Using ‘;&’ in place of ‘;;’ causes execution to continue with the
command-list associated with the next clause, if any. Using ‘;;&’ in place of
‘;;’ causes the shell to test the patterns in the next clause, if any, and execute
any associated command-list on a successful match.


Chapter 3: Basic Shell Features
12
The return status is zero if no pattern is matched. Otherwise, the return status
is the exit status of the command-list executed.
select
The select construct allows the easy generation of menus. It has almost the
same syntax as the for command:
select name [in words ...]; do commands; done
The list of words following in is expanded, generating a list of items. The set of
expanded words is printed on the standard error output stream, each preceded
by a number. If the ‘in words’ is omitted, the positional parameters are printed,
as if ‘in "$@"’ had been specified. The PS3 prompt is then displayed and a line
is read from the standard input. If the line consists of a number corresponding
to one of the displayed words, then the value of name is set to that word. If
the line is empty, the words and prompt are displayed again. If EOF is read,
the select command completes. Any other value read causes name to be set
to null. The line read is saved in the variable REPLY.
The commands are executed after each selection until a break command is
executed, at which point the select command completes.
Here is an example that allows the user to pick a filename from the current
directory, and displays the name and index of the file selected.
select fname in *;
do
echo you picked $fname \($REPLY\)
break;
done
((...))
(( expression ))
The arithmetic expression is evaluated according to the rules described below
(see Section 6.5 [Shell Arithmetic], page 89). If the value of the expression is
non-zero, the return status is 0; otherwise the return status is 1. This is exactly
equivalent to
let "expression"
See Section 4.2 [Bash Builtins], page 49, for a full description of the let builtin.
[[...]]
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expres-
sion expression. Expressions are composed of the primaries described below in
Section 6.4 [Bash Conditional Expressions], page 87. Word splitting and file-
name expansion are not performed on the words between the [[ and ]]; tilde
expansion, parameter and variable expansion, arithmetic expansion, command
substitution, process substitution, and quote removal are performed. Condi-
tional operators such as ‘-f’ must be unquoted to be recognized as primaries.
When used with [[, the ‘<’ and ‘>’ operators sort lexicographically using the
current locale.


Yüklə 4,8 Kb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   66




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə