In this lecture, we’ll take a look at for loops. A for statement is used to execute a block 1
of code repeatedly. 2
One of the reasons a computer is so useful is that it can repeat operations multiple times very quickly. 3
In programming 4
a for loop represents one way of executing repetitive tasks and is a fundamental concept of any other 5
programming language. 6
The standard Bash For Loop is similar to the Python one, but is different from the for loop used in 7
C++ or Java. 8
The Bash For Loop takes this form: item is a temporary variable that is created and takes a value from the 9
list sequentially at each operation. 10
Let's create the first script and iterate over a list of strings. 11
So for os in and the list Ubuntu CentOs Slakware 12
and Kali. By default, the list is a series of strings separated by spaces: do and done ; and the for 13
block of code between do and done so echo 14
"os is $os" I'm saving the script, setting the execution permission and running 15
it in another terminal. 16
We see how the OS variable is set in turn to each element of the list in each iteration. 17
If a string element of the list contains a space, then you should enclose it in double quotes. 18
For example, I am adding a new element to the list and that new element contains a space MX Linux. 19
I'm running the script again. 20
Let's move on and see how to iterate over a range of numbers. 21
To do that will use what is called a sequence expression, which defines the start and the end point 22
of the range like this: for num in a pair of curly braces, the start double dots and the end of the 23
range, do and done and between do and done echo " num is $num". 24
I'm saving and running it. 25
Starting with Bash version 4, it's also possible to specify an increment for the range. 26
Like this, 10..100..5, these will iterate over the range of numbers from 10 to 100 27
in increments of 5. 28
And in the next example, I'll show you how to iterate over the output of a command. Using a for loop 29
I'll display the contents of each file in the current directory. 30
I'm going to create a new script. 31
So for item in ./* ./* means any file in the current directory, 32
the current directory being the single dot do and done 33
Now, I want to test if the variable called item is a regular file. 34
So if -7$ item, then and fi so echo "Displaying the contents of $item" 35
sleep 1 cat $item 36
And for readability, I'm printing a series of hash size. 37
I'm saving the file, setting the execution mode, and I'm running it. 38
Displaying the contents of each file in the current directory. 39
Very nice! 40
And another example, I'm creating a script that will rename all files with the .txt extension from the 41
current directory by inserting a custom string at the beginning of the file name. 42
So vim rename_files.sh 43
So for file in *.txt that means that it will iterate over the txt files in the current directory 44
do and done and mv "$file" it's renaming each file to another filing, for example, 45
"renamed_by_script_$file" 46
So chmod +x rename_files.sh 47
I'm creating a few txt files in the current directory and then running the script, so touch abc.txt 48
123.txt the script will rename only these files. 49
And the files were renamed. 50
And finally, in the last example, I'll show you the Bash For Loop using the C++ or Java syntax. 51
However, this is not very common in Bash. 52
vim c_style_for_loop.sh 53
So for ((i = 0 ; i <= 50 ; 54
i++)) do and done and between do an d done echo "i = $i" 55
I'm saving the script, 56
setting the execution mode and running it. 57
And it printed out all values from 0 to 50. 58
Let's take another look at the for loop. 59
This for loop consists of 3 parts: the initialization, the test and the step parts. The loop 60
starts by initializing a temporary variable, i with value 0, and before each iteration it checks 61
if i is less than 50. 62
If that's true, it prints out the current value of i and increments the variable by 1. 63
This is the i++ part. 64
When i is not less than or equal to 50, the loop terminates.