[go: up one dir, main page]

0% found this document useful (0 votes)
80 views18 pages

Linux Shell

The case command compares a variable against a series of values or patterns and executes the corresponding statements if a match is found. It is preferable to long elif statements when testing string values. Each case must end with semicolons and there is typically a catch-all pattern. A while loop repeats commands while its condition is true. It is completed with done and can be stopped with break or continued with continue. An until loop is like a while loop but repeats until its condition is true. A for loop iterates a variable through a list of values. Embedded let can create for loops. Commands can be grouped with curly braces. Functions are a way to embed reusable code in scripts without external files.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views18 pages

Linux Shell

The case command compares a variable against a series of values or patterns and executes the corresponding statements if a match is found. It is preferable to long elif statements when testing string values. Each case must end with semicolons and there is typically a catch-all pattern. A while loop repeats commands while its condition is true. It is completed with done and can be stopped with break or continued with continue. An until loop is like a while loop but repeats until its condition is true. A for loop iterates a variable through a list of values. Embedded let can create for loops. Commands can be grouped with curly braces. Functions are a way to embed reusable code in scripts without external files.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

The Linux Shells Compound Commands

Operating Systems Laboratory Amir Saman Memaripour

case Command
The case command compares a variable against a series of values or patterns. If the value or pattern matches, the corresponding statements are executed. The name comes from the fact that the variable is tested on a case-by-case basis. Unlike elif commands that test the status code for individual commands, case tests the value of a variable. The case command is preferable to a long set of elifs when testing string values.

case Command
Each individual case must end with a pair of semicolons. Without the semicolons, Bash will attempt to execute the next case designation 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

case Command
The pattern asterisk (*) is the catch-all case; it matches all values that were not handled by a previous case. Although this case is optional, its good design to always include a catch-all case, even if it only contains a null statement (:). The pattern-matching rules follow the file globbing rules, as discussed in the previous chapter. For example, vertical bars can separate multiple patterns. The cases dont fall through as some computer languages do, notably C. When one case is selected, only those commands are executed. The commands in the following cases are not executed.

while Loop
There are several commands for repeating a set of commands. The while command repeats the enclosed commands while the command being tested succeeds. If the command fails on the first try, the enclosed commands are never executed. A while loop is completed with the done command, not elihw as you might expect. An infinite loop can be created using the true command. Because true always succeeds, the loop will continue indefinitely.

while Loop
printf %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

while Loop
A while loop can be stopped prematurely with the break command. On reaching a break, Bash stop executing commands and breaks out of the loop and begins executing the first command after the loop. break can be followed by a number indicating how many enclosing loops to break out of. For example: break 2, breaks out of the current loop as well as the loop enclosing the current loop. The counterpart to break is continue, which causes the remainder of the enclosed statements to be ignored; the loop is resumed at the top. continue can be followed by a number indicating how many enclosing statements to break out of before continuing.

while Loop
printf %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 company fi done

until Loop
The counterpart of the while loop is the until loop. The until command is identical to the while command except that it repeats the enclosed statements until the condition 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

for Loops
The for command reads a sequence of values into a variable and repeats the enclosed commands once for each 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 in part is missing, the for command will execute the enclosed statements for each argument to the shell script.

10

Embedded let
The let command returns a status code of 1 if an expression is zero, or 0 if the expression is a value other than zero. In the same way that the test command 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 for loop, as found in other programming languages, is created using an embedded let.

11

Embedded let
for (( COUNTER=1; COUNTER<10; COUNTER++ )) ; do printf The counter is now %d\n $COUNTER Done The first expression in the double parentheses is executed when the loop is started. Each time the loop is restarted, the third expression is executed and the second expression is checked. When the second expression returns false, the loop ends.

12

Grouping Commands
Commands can be grouped together using curly braces ( {...} ). ls -1 | { while read FILE ; do echo $FILE done } In this example, the results of the ls command become the input for all the statements in the group.

13

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 and deleted; otherwise, no such file is printed. The semicolon after the last braced command is required only when the braces are on a single line.

14

Shell Functions
Functions 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 not have to be loaded in order to be used. This, in turn, makes them run faster than separate scripts. Like separate scripts, functions can be used to break up a complex script into separate, named, tasks to improve readability. Shell functions differ from functions in other programming languages in that they return a status code instead of a return value. Without a return value, they cannot be used in expressions.

15

Shell Functions
Like 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

Other Issues
Bash Security Version Control Network Programming Data structures

17

Assignment (5 points)
Write a shell script to find and print all prime numbers less than 100. In order to do that, use Linear Sieve or other kinds of prime number generation algorithms.

Deadline: December 18th, 10:00 AM How to deliver?! Via email to memarypour@gmail.com

18

You might also like