Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Outlines
• Operators
• Looping Structures
• Conditional Structures
• Functions
• Exercise
Requirements
• Computer System with suitable Configuration
• Operating System Ubuntu 16.04.1 Desktop
• Bash Shell
1
OPERATORS
s1 = s2: s1 matches
s2
s1 != s2: s1 does not
match s2
s1 < s2: s1 less than
s2
s1 > s2: s1 greater
than s2
-n s1: s1 is not null (contains one or more characters)
-z s1: s1 is null
ARITHMETIC OPERATORS
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
2
LOGICAL OPERATORS
Example# 01
3
LOOPING STRUCTURES
Computer can repeat an instruction again and again, until particular condition is
satisfied. A group of instructions that can be executed repeatedly by using loops.
FOR LOOP
The for statement executes the commands for specified number of times, execute
one for each item in the list until the list is not finished, and repeat all statement
between do and done.
Syntax:
for variable name in list
for (( assignment; condition ; increment/decrement ))
do
command(s)
done
EXAMPLE # 01
4
EXAMPLE # 02
EXAMPLE # 03
EXAMPLE # 04
5
EXAMPLE # 05
6
EXAMPLE # 06
Caution: The list of values should not be separated by comma (Mon, Tue, Wed,
Thu, Fri). The comma will be treated as part of the value.
Caution: The list of values should not be enclosed in a double quote. (“Mon Tue
Wed Thu Fri”). If you enclose in double quote, it will be treated as a single value
(instead of 5 different values)
EXAMPLE # 07
7
A PRACTICAL EXAMPLE # \*The list in this example is every
file with the .MP3 extension in
#!/bin/bash the current folder and the
variable is a file.
For file in ./*.mp3 The mpg command converts the
do MP3 file into WAV. However, you
mpg123 –w ./wavs/"${file}".wav "$file" probably need to install this
done using your package manager
first.*/
WHILE LOOP
Loop is executed as long as given condition is true.
Syntax:
while [ condition ]
do
command(s)
done
8
EXAMPLE # 01
EXAMPLE # 02
9
UNTIL LOOP
The until statement is very similar in syntax and function to the while statement.
The only real difference between the two is that the until statement executes its
code block while its conditional expression is false, and the while statement
executes its code block while its conditional expression is true.
Syntax:
until
[ condition]
do
command(s)
done
10
EXAMPLE # 01
CONDITIONAL STRUCTURES
There are two forms of conditional statements: the if statement and the case
statement. These statements are used to execute different parts of your shell
program depending on whether certain conditions are true.
if...then...fi
If given condition is true then command is executed otherwise it does nothing.
Syntax:
if condition
then
comand(s)
fi
11
EXAMPLE # 01
if...then...else…fi
If given condition is true then command1 is executed else command2 will be
executed..
Syntax:
if condition
then
$command1
else
$command2
fi
12
EXAMPLE # 01
Multilevel if
This statement will be applied when we have multiple condtions or expressions to
be tested. The elif statement is an abbreviation of else if. The fi keyword is used to
signal the end of the if statement.
Syntax:
if condition
then
13
command1
elif condition
then
command2
.
.
.
elif condition
then
command
else
commad
fi
EXAMPLE # 01
14
The case Statement
Multiple if (elif) statements perform a multiway branch. However, this is not always
the best solution, especially when all of the branches depend on the value of a single
variable.
Shell supports case...esac statement which handles exactly this situation, and it
does so more efficiently.
The case statement is good alternative to multilevel if. It enables you to match
several values against one variable. The case statement enables you to compare a
pattern with several other patterns and execute a block of code if a match is found.
The default is *) and its executed if no match is found.
Syntax:
case variable in
choice 1)
commands;;
choice 2)
commands;; …….
…….
*) commands
esac
15
EXAMPLE # 01
16
EXAMPLE # 02
FUNCTIONS
The shell languages enable you to define your own functions. These functions
behave in much the same way as functions you define in C or other programming
languages. The main advantage of using functions as opposed to writing all of your
shell code in lines is for organizational purposes. Code written using functions tend
to be much easier to read and maintain and also tends to be smaller, because you
can group common code into functions and of course put it where ever it is needed.
17
Syntax:
fname ()
{
shell commands
}
EXAMPLE # 01
EXAMPLE # 02
18
EXAMPLE # 03
19
EXERCISE
Q1: Execute all above shell programs and attach the snapshots.
Q2: Write a shell program to read a number and check whether the number is odd or
even.
Q3: Write a shell program to read a character and check whether the character is
vowel or not using case statement.
Q4: Write a shell program to create a function that calculates the average of three
numbers.
Q5: What is difference between & and && in Shell? Demonstrate with example.
Q7: In manual above, there is given a practical example of for loop. Perform that using
mp3 file and show the output.
The End
20