linux shell (6)
Post on 05-Apr-2018
228 Views
Preview:
TRANSCRIPT
-
7/31/2019 Linux Shell (6)
1/18
The Linux ShellsCompound Commands
Operating Systems Laboratory
Amir Saman Memaripour
-
7/31/2019 Linux Shell (6)
2/18
caseCommandThe casecommand compares a variable against a series
of values or patterns. If the value or pattern matches,the corresponding statements are executed. Thename comes from the fact that the variable is tested on
a case-by-case basis.
Unlike elif commands that test the status code forindividual commands, case tests the value of a
variable. The case command is preferable to a long setof elifswhen testing string values.
2
-
7/31/2019 Linux Shell (6)
3/18
caseCommandEach individual case must end with a pair of semicolons.
Without the semicolons, Bash will attempt to execute thenext casedesignation and report an error.
printf %s -> 1 = delete, 2 = archive. Please choose one
read REPLY
case $REPLY in
1) rm $TEMPFILE ;;
2) mv $TEMPFILE $TEMPFILE.old ;;
*) printf %s\n $REPLY was not one of the choices ;;
esac
3
-
7/31/2019 Linux Shell (6)
4/18
caseCommandThe pattern asterisk (*) is the catch-all case; it matches all values
that were not handled by a previous case. Although this case isoptional, its good design to always include a catch-all case, evenif it only contains a null statement (:).
The pattern-matching rules follow the file globbing rules, asdiscussed in the previous chapter. For example, vertical bars canseparate multiple patterns.
The cases dont fall through as some computer languages do,
notably C. When one case is selected, only those commands areexecuted. The commands in the following cases are notexecuted.
4
-
7/31/2019 Linux Shell (6)
5/18
whileLoopThere are several commands for repeating a set of
commands.
The while command repeats the enclosed commands
while the command being tested succeeds. If thecommand fails on the first try, the enclosed commandsare never executed.
A while loop is completed with the donecommand, not
elihwas you might expect.
An infinite loopcan be created using the truecommand.Because truealways succeeds,the loop will continue
indefinitely. 5
-
7/31/2019 Linux Shell (6)
6/18
whileLoopprintf %s\n Enter the names of companies or type control-d
while read -p Company ? COMPANY; do
if test -f orders_$COMPANY.txt ; then
printf %s\n There is an order file from this company
else
printf %s\n There are no order files from this company
fi
done
6
-
7/31/2019 Linux Shell (6)
7/18
whileLoopA whileloop can be stopped prematurely with the break
command. On reaching a break, Bash stop executingcommands and breaks out of the loop and beginsexecuting the first command after the loop. break
can be followed by a number indicating how manyenclosing loops to break out of. For example: break 2,breaks out of the current loop as well as the loopenclosing the current loop.
The counterpart to break is continue, which causes theremainder of the enclosed statements to beignored; the loop is resumed at the top. continuecanbe followed by a number indicating how manyenclosing statements to break out of before
continuing. 7
-
7/31/2019 Linux Shell (6)
8/18
whileLoopprintf %s\n Enter the names of companies or type quit
while true ; do
read -p Company ? COMPANY
if [ $COMPANY = quit ] ; then
break
elif test -f orders_$COMPANY.txt ; then
printf %s\n There is an order file from this company
else
printf %s\n There are no order files from this companyfi
done
8
-
7/31/2019 Linux Shell (6)
9/18
untilLoopThe counterpart of the while loop is the until loop. The
until command is identical to the while commandexcept that it repeats the enclosed statements until thecondition is successful, essentially the same as
while!
until test -f $INVOICE_FILE ; do
printf %s\n Waiting for the invoice file to arrive...
sleep 30
done
9
-
7/31/2019 Linux Shell (6)
10/18
forLoopsThe for command reads a sequence of values into a
variable and repeats the enclosed commands once foreach value.
for FILE_PREFIX in order invoice purchase_order; do
if test -f $FILE_PREFIX_vendor1.txt ; then
printf %s\n There is a $FILE_PREFIX file from vendor 1...
fi
done
If the inpart is missing, the forcommand will execute theenclosed statements for each argument to the shell
script. 10
-
7/31/2019 Linux Shell (6)
11/18
Embedded letThe let command returns a status code of 1 if an
expression is zero, or 0 if the expression is a valueother than zero. In the same way that the testcommand can be expressed with a pair of square
brackets for easier reading in compound statements,the let command also has a form for easier reading:double parentheses.
The forloop, as found in other programming languages, iscreated using an embedded let.
11
-
7/31/2019 Linux Shell (6)
12/18
Embedded letfor (( COUNTER=1; COUNTER
-
7/31/2019 Linux Shell (6)
13/18
Grouping CommandsCommands can be grouped together using curly braces (
{...} ).
ls -1 | {
while read FILE ; do
echo $FILE
done
}
In this example, the results of the lscommand becomethe input for all the statements in the group.
13
-
7/31/2019 Linux Shell (6)
14/18
Grouping Commands$ test -f orders.txt && { ls -l orders.txt ; rm orders.txt; }
\
|| printf no such file
If the file orders.txt exists, the file will be listed anddeleted; otherwise, no such file is printed. Thesemicolon after the last braced command is requiredonly when the braces are on a single line.
14
-
7/31/2019 Linux Shell (6)
15/18
Shell FunctionsFunctions are a way of embedding small subscripts into a
Bash script without saving them in a separate file.Because they are embedded in a script, they do nothave to be loaded in order to be used. This, in turn,
makes them run faster than separate scripts. Likeseparate scripts, functions can be used to break up acomplex script into separate, named, tasks to improvereadability.
Shell functions differ from functions in other programminglanguages in that they return a status code instead ofa return value. Without a return value, they cannot beused in expressions.
15
-
7/31/2019 Linux Shell (6)
16/18
Shell FunctionsLike variables, functions must be declared before they
can be used. Instead of the declare command,functions are declared using the function command.Each function has a name and the statements
composing the function are enclosed in curly brackets.
function trash_tmp { }
16
-
7/31/2019 Linux Shell (6)
17/18
Other Issues Bash Security
Version Control
Network Programming Data structures
17
-
7/31/2019 Linux Shell (6)
18/18
Assignment (5 points)
Write a shell script to find and print all prime numbers
less than 100. In order to do that, use Linear Sieveor
other kinds of prime number generation algorithms.
Deadline: December 18th, 10:00 AM
How to deliver?!Via email to memarypour@gmail.com
18
mailto:memarypour@gmail.commailto:memarypour@gmail.com
top related