Topic4
BASH LOOPS
3/19/2021
Bash programming: still to come
Control structures ◦ Repetition
◦ do-while, repeat-until ◦ for
◦ select
2
CSCI 330 – The Unix System
1
Repetition Constructs
3/19/2021
The while Loop
Purpose:
To execute commands in “command-list” as long as “expression” evaluates to true
Syntax:
while [ expression ]
do
command-list
done
3
CSCI 330 – The Unix System
4
CSCI 330 – The Unix System
2
Example: Using the while Loop
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ] do
echo The counter is $COUNTER
let COUNTER=$COUNTER+1
done
3/19/2021
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”
5
CSCI 330 – The Unix System
6
CSCI 330 – The Unix System
3
#!/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
The until Loop
Purpose:
To execute commands in “command-list” as long as “expression” evaluates to false
Syntax:
until [ expression ]
do
command-list
done
3/19/2021
7 CSCI 330 – THE UNIX SYSTEM
8
CSCI 330 – The Unix System
4
Example: Using the until Loop
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ] do
echo $COUNTER
let COUNTER-=1
done
3/19/2021
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”
9
CSCI 330 – The Unix System
10
CSCI 330 – The Unix System
5
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
3/19/2021
Example 1: The for Loop
#!/bin/bash
for i in 7 9 2 3 4 5
do
echo $i done
11
CSCI 330 – The Unix System
12
CSCI 330 – The Unix System
6
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
3/19/2021
looping over arguments
simplest form will iterate over all command line arguments:
#! /bin/bash
for parm
do
done
echo $parm
13
CSCI 330 – The Unix System
14
CSCI 330 – The Unix System
7
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)
3/19/2021
Select example
#! /bin/bash
select var in alpha beta gamma
do
echo $var
done
Prints:
15
CSCI 330 – The Unix System
1) alpha
2) beta
3) gamma
#? 2
beta
#? 4 #? 1 alpha
16
CSCI 330 – The Unix System
8
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
3/19/2021
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
9
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
The break command
19
CSCI 330 – The Unix System
3/19/2021
while [ condition ]
do
cmd-1
break
cmd-n
done
echo “done”
This iteration is over and there are no more iterations
20
CSCI 330 – The Unix System
10
The continue command
while [ condition ]
do
cmd-1
continue
cmd-n
done
echo “done”
This iteration is over; do the next iteration
3/19/2021
Example:
for index in 1 2 3 4 5 6 7 8 9 10 do
done
if [ $index –le 3 ]; then
echo “continue”
continue fi
echo $index
if [ $index –ge 8 ]; then
echo “break”
break fi
21
CSCI 330 – The Unix System
22
CSCI 330 – The Unix System
11