Handling Repetition
Presentation Subtitle
2
What is Handling Repetition ?
Handling Repetition is control structure also known as looping control structure,Handling Repetition is control structure also known as looping control structure,
is a type of control structure in programming languages that is used to simplify repetitive oris a type of control structure in programming languages that is used to simplify repetitive or
recursive tasks operation.recursive tasks operation.
Delete zero sized files in system.Delete zero sized files in system.
3
Code for write a script to delete zero sized files from a given directory
(and all its subdirectories) in Unix / Linux / Ubuntu
Code for write a script to delete zero sized files from a given directory
(and all its subdirectories) in Unix / Linux / Ubuntu
4
5
Script 19
Handling Repetition is control structure also known as looping control structure,Handling Repetition is control structure also known as looping control structure,
is a type of control structure in programming languages that is used to simplify repetitive oris a type of control structure in programming languages that is used to simplify repetitive or
recursive tasks operation.recursive tasks operation.
6
Repetitive tasks
1.1. The for loopThe for loop
2.2. The while loopThe while loop
3.3. The until loopThe until loop
7
The for loopThe for loop
The for loop is the first of the three shell looping constructs.
This loop allows for specification of a list of values.
A list of commands is executed for each value in the list.
8
for loop Syntaxfor loop Syntax
9
for loop Examplefor loop Example
$ cat for1.sh
i=1
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i++)) : $day"
done
$ ./for1.sh
Weekday 1 : Mon
Weekday 2 : Tue
Weekday 3 : Wed
Weekday 4 : Thu
Weekday 5 : Fri
10
The while loopThe while loop
The while construct allows for repetitive execution of a list of commands, as long as the
command controlling the while loop executes successfully (exit status of zero)
The while construct allows for repetitive execution of a list of commands, as long as the
command controlling the while loop executes successfully (exit status of zero)
11
The while loopThe while loop
12
while loop Syntaxwhile loop Syntax
13
while loop Examplewhile loop Example
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
14
The until loopThe until loop
15
until loop Syntaxuntil loop Syntax
until <TEST-COMMAND>;
do
CONSEQUENT - COMMANDS;
done
16
until loop Exampleuntil loop Example
until [ $DISKFUL -ge "90" ];
do
DATE=`date +%Y%m%d`
HOUR=`date +%H`
mkdir $WEBDIR/"$DATE"
while [ $HOUR -ne "00" ];
do
DESTDIR=$WEBDIR/"$DATE"/"$HOUR"
mkdir "$DESTDIR" mv $PICDIR/*.jpg "$DESTDIR"/
sleep 3600 HOUR=`date +%H`
done
Functions in script
18
What is Function ?
A function is a subroutine, a code block that implements a set of operations, a "black
box" that performs a specified task. Wherever there is repetitive code, when a task repeats
with only slight variations in procedure, then consider using a function.
19
Syntax of Function ?
function function_name {
command...
}
or
function_name () {
command...
}
20
Example of Function ?
21
Example of Function ?
22
23
24
25
26
27
28
29
30
31
32

Advanced Scripting - 2 (Ch-8)

  • 1.
  • 2.
    2 What is HandlingRepetition ? Handling Repetition is control structure also known as looping control structure,Handling Repetition is control structure also known as looping control structure, is a type of control structure in programming languages that is used to simplify repetitive oris a type of control structure in programming languages that is used to simplify repetitive or recursive tasks operation.recursive tasks operation.
  • 3.
    Delete zero sizedfiles in system.Delete zero sized files in system. 3 Code for write a script to delete zero sized files from a given directory (and all its subdirectories) in Unix / Linux / Ubuntu Code for write a script to delete zero sized files from a given directory (and all its subdirectories) in Unix / Linux / Ubuntu
  • 4.
  • 5.
    5 Script 19 Handling Repetitionis control structure also known as looping control structure,Handling Repetition is control structure also known as looping control structure, is a type of control structure in programming languages that is used to simplify repetitive oris a type of control structure in programming languages that is used to simplify repetitive or recursive tasks operation.recursive tasks operation.
  • 6.
    6 Repetitive tasks 1.1. Thefor loopThe for loop 2.2. The while loopThe while loop 3.3. The until loopThe until loop
  • 7.
    7 The for loopThefor loop The for loop is the first of the three shell looping constructs. This loop allows for specification of a list of values. A list of commands is executed for each value in the list.
  • 8.
  • 9.
    9 for loop Exampleforloop Example $ cat for1.sh i=1 for day in Mon Tue Wed Thu Fri do echo "Weekday $((i++)) : $day" done $ ./for1.sh Weekday 1 : Mon Weekday 2 : Tue Weekday 3 : Wed Weekday 4 : Thu Weekday 5 : Fri
  • 10.
    10 The while loopThewhile loop The while construct allows for repetitive execution of a list of commands, as long as the command controlling the while loop executes successfully (exit status of zero) The while construct allows for repetitive execution of a list of commands, as long as the command controlling the while loop executes successfully (exit status of zero)
  • 11.
  • 12.
  • 13.
    13 while loop Examplewhileloop Example a=0 while [ $a -lt 10 ] do echo $a a=`expr $a + 1` done
  • 14.
  • 15.
    15 until loop Syntaxuntilloop Syntax until <TEST-COMMAND>; do CONSEQUENT - COMMANDS; done
  • 16.
    16 until loop Exampleuntilloop Example until [ $DISKFUL -ge "90" ]; do DATE=`date +%Y%m%d` HOUR=`date +%H` mkdir $WEBDIR/"$DATE" while [ $HOUR -ne "00" ]; do DESTDIR=$WEBDIR/"$DATE"/"$HOUR" mkdir "$DESTDIR" mv $PICDIR/*.jpg "$DESTDIR"/ sleep 3600 HOUR=`date +%H` done
  • 17.
  • 18.
    18 What is Function? A function is a subroutine, a code block that implements a set of operations, a "black box" that performs a specified task. Wherever there is repetitive code, when a task repeats with only slight variations in procedure, then consider using a function.
  • 19.
    19 Syntax of Function? function function_name { command... } or function_name () { command... }
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.