Topic 4
3/19/2021
1
Topic 4
BASH LOOPS
Bash programming: still to come
Control structures
◦ Repetition
◦ do-while, repeat-until
◦ for
◦ select
2
CSCI 330 – The
Unix System
3/19/2021
2
Repetition Constructs
3
CSCI 330 – The
Unix System
The while Loop
Purpose:
To execute commands in “command-list” as long as “expression” evaluates to true
Syntax:
while [ expression ]
do
command-list
done
4
CSCI 330 – The
Unix System
3/19/2021
3
Example: Using the while Loop
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]
do
echo The counter is $COUNTER
let COUNTER=$COUNTER+1
done
5
CSCI 330 – The
Unix System
Example: Using the while Loop
#!/bin/bash
Cont=”Y”
while [ $Cont = “Y” ]; do
ps -A
read -p “want to continue? (Y/N)” reply
Cont=`echo $reply | tr [:lower:] [:upper:]`
done
echo “done”
6
CSCI 330 – The
Unix System
3/19/2021
4
#!/bin/bash
# copies files from home- into the webserver- directory
# A new directory is created every hour
PICSDIR=/home/carol/pics
WEBDIR=/var/www/carol/webcam
while true; do
DATE=`date +%Y%m%d`
HOUR=`date +%H`
mkdir $WEBDIR/”$DATE”
while [ $HOUR -ne “00” ]; do
DESTDIR=$WEBDIR/”$DATE”/”$HOUR”
mkdir “$DESTDIR”
mv $PICSDIR/*.jpg “$DESTDIR”/
sleep 3600
HOUR=`date +%H`
done
done
7 CSCI 330 – THE
UNIX SYSTEM
The until Loop
Purpose:
To execute commands in “command-list” as long as “expression” evaluates to false
Syntax:
until [ expression ]
do
command-list
done
8
CSCI 330 – The
Unix System
3/19/2021
5
Example: Using the until Loop
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]
do
echo $COUNTER
let COUNTER-=1
done
9
CSCI 330 – The
Unix System
Example: Using the until Loop
#!/bin/bash
Stop=”N”
until [ $Stop = “Y” ]; do
ps -A
read -p “want to stop? (Y/N)” reply
Stop=`echo $reply | tr [:lower:] [:upper:]`
done
echo “done”
10
CSCI 330 – The
Unix System
3/19/2021
6
The for Loop
Purpose:
To execute commands as many times as the number of words in the “argument-list”
Syntax:
for variable in argument-list
do
commands
done
11
CSCI 330 – The
Unix System
Example 1: The for Loop
#!/bin/bash
for i in 7 9 2 3 4 5
do
echo $i
done
12
CSCI 330 – The
Unix System
3/19/2021
7
Example 2: Using the for Loop
#!/bin/bash
# compute the average weekly temperature
for num in 1 2 3 4 5 6 7
do
read -p “Enter temp for day $num: ” Temp
let TempTotal=$TempTotal+$Temp
done
let AvgTemp=$TempTotal/7
echo “Average temperature: ” $AvgTemp
13
CSCI 330 – The
Unix System
looping over arguments
simplest form will iterate over all command line arguments:
#! /bin/bash
for parm
do
echo $parm
done
14
CSCI 330 – The
Unix System
3/19/2021
8
Select command
Constructs simple menu from word list
Allows user to enter a number instead of a word
User enters sequence number corresponding to the word
Syntax:
select WORD in LIST
do
RESPECTIVE-COMMANDS
done
Loops until end of input, i.e. ^d (or ^c)
15
CSCI 330 – The
Unix System
Select example
#! /bin/bash
select var in alpha beta gamma
do
echo $var
done
Prints:
16
CSCI 330 – The
Unix System
1) alpha
2) beta
3) gamma
#? 2
beta
#? 4
#? 1
alpha
3/19/2021
9
Select detail
PS3 is select sub-prompt
$REPLY is user input (the number)
#! /bin/bash
PS3=”select entry or ^D: ”
select var in alpha beta
do
echo “$REPLY = $var”
done
17
CSCI 330 – The
Unix System
Output:
select …
1) alpha
2) beta
? 2
2 = beta
? 1
1 = alpha
Select example
#!/bin/bash
echo “script to make files private”
echo “Select file to protect:”
select FILENAME in *
do
echo “You picked $FILENAME ($REPLY)”
chmod go-rwx “$FILENAME”
echo “it is now private”
done
18
CSCI 330 – The
Unix System
3/19/2021
10
break and continue
Interrupt for, while or until loop
The break statement
◦ transfer control to the statement AFTER the done statement
◦ terminate execution of the loop
The continue statement
◦ transfer control to the statement TO the done statement
◦ skip the test statements for the current iteration
◦ continues execution of the loop
19
CSCI 330 – The
Unix System
The break command
while [ condition ]
do
cmd-1
break
cmd-n
done
echo “done”
20
CSCI 330 – The
Unix System
This iteration is over
and there are no more
iterations
3/19/2021
11
The continue command
while [ condition ]
do
cmd-1
continue
cmd-n
done
echo “done”
21
CSCI 330 – The
Unix System
This iteration is
over; do the next
iteration
Example:
for index in 1 2 3 4 5 6 7 8 9 10
do
if [ $index –le 3 ]; then
echo “continue”
continue
fi
echo $index
if [ $index –ge 8 ]; then
echo “break”
break
fi
done
22
CSCI 330 – The
Unix System