8.5 Repetition



In programming, repetition is also important factor. It repeats same processing and ends it when some conditions are satisfied.

There are three ways for repetition in shell scripts. Let's confirm them surely.

8.5.1 for sentence

'for' sentence enumerates values, repeats processing by using its value.

Format

for variable in list_of_values
do
      processing
done


If the result of a command is character strings, you can use its result as list of values.


Example

$ for i in a b c d
> do
>       echo $i
> done
a
b
c
d


This is the result by putting 'a b c d' as list of values. In case of 'a', it echo 'a'.(i=a, echo $i) In case of 'b', it echo 'b'.(i=b, echo $i)… If the result of ls command is used as list of values, the 'for' sentence has following description.


for i in `ls`


It gets file and directory names as the ls result, and outputs them to standard output.

8.5.2 while/until Sentence

'while' sentence is used by loop processing that is repeated, while specified conditions are true. (ie. it will end if specified conditions become false) 'until' sentence is entirely reverse. 'until' sentence is used by loop processing that is repeated, while specified conditions are false. (ie. it will end if specified conditions become true)


Format

while conditional_expression
do
      processing
done
until conditional_expression
do
      processing
done


If you want to implement for-loop process like C language by using shell script, you should use expr command. You can control loop processing, by incrementing (or decrementing) a counting variable.


Example

$ cat loop.sh ↵          loop.sh is created and performed as follows.
#!/bin/bash
count=1                The initial value 1 is set for the counters of variable.
while [ $count -le 10 ]       While shell variable is under 10, the processing is repeated.
do
   echo " This processing was performed $count time(s) "
   count=`expr $count + 1`
done
$ ./loop.sh
This processing was performed 1 time(s) ↵
This processing was performed 2 time(s) ↵
This processing was performed 3 time(s) ↵
This processing was performed 4 time(s) ↵
This processing was performed 5 time(s) ↵
This processing was performed 6 time(s) ↵
This processing was performed 7 time(s) ↵
This processing was performed 8 time(s) ↵
This processing was performed 9 time(s) ↵
This processing was performed 10 time(s)


8.5.3 select Sentence

'select’ sentence prompts numerical value input from user.


Format

select variable in list
do
      processing
done


Here is execution example.


select name in "miyahara" "kawai" "okada"
do
      echo "You are Mr. $name";
done


Its result is below.


1) miyahara
2) kawai
3) okada
$? 1                         Inputted 1
You are Mr. miyahara    The scripts from 'do' to 'done' are performed.


8.5.4 Repeat Control

You can control program loop by using 'break' and/or 'continue'. 'break' interrupts program loop, and 'continue' goes back to the top of loop.


Example

while true
do
   echo "Continue? (y/n)"
   read input
   case $input in
      n) break
          ;;
      y) continue
          ;;
      *) echo "Please input y or n."
          ;;
esac
done


Here is the execution result.


$ sample.sh ↵
Continue? (y/n)
y ↵                               Inputted ‘y’
Continue? (y/n)       Goes back to the top of loop
a ↵                               Inputted something except both ‘x’ and ‘y’
Please input y or n.
Continue? (y/n)
n ↵                               Inputted ‘n’
$                                     End this loop



Previous Next