8- BASH Scripting – FOR Conditional Statement
Hello, Linux-fanatics, in our earlier tutorial we discussed advanced used of if-then-else conditional statements. In this tutorial, we will discuss for-conditional statement.
for statement is used where we require a set of commands to repeated over & over again until our conditions are all met. Like for example, we need to change some information for all users on system,
Syntax for for conditional statement
In this script, when for loop finishes last value of variable i.e. daniel, will be retained but we can reassign a new value i.e. monti.
Also, you should remember when using words like won’t or don’t , you should either escape those characters or use double quotation otherwise they will not be interpreted properly. For example: won\’t don\’t or “won’t” “don’t”
Now let’s take a look at another example
So in above script we escaped don’t & used double quotations with North Korea, now the output for the script will show proper result.Try yourself to write above script with & without escaping the characters & using double quotation. You will notice the difference yourself.
Also, if we want that for statement reads from a variable or use output of a command as list, we can also do that
Here we defined a varaible “name” which we then used in for statement, also further in next line we added another namme to variable.
Now, let’s create a file named “name.txt” in “/home/dan” & lets write up some name there. We will now use the file as our input for list in for statement
IMPORTANT NOTE: When you created your file, you might have listed names either by typing one name per line, or using single space between names or using tab as space between names.
Note that if you have commas to separate than all the names would be interpreted as single name, why it is so. That’s because of environmental variable known as IFS or internal field separator and IFS interprets space, tab, newline as separators.
There might be a need when we don’t want space or tabs to used as separators or we might want to add “,” comma as a separator. It can be done by using following in your script
So, now only new line, comma & semi colon will be separators for the rest of the script.
Using for command in C language style
We can also for statement as is used in C programming language i.e.
for (( i=0; i=20; i++ ))
this will provide output of “I” starting from 1 until its equal to 20 with increment of one.
With this approach you can also use multiple variable in single for statement
Both ways of using for statement has merits and we can use them both depending on our need
Now that we have learned for conditional statement, let’s write a script with all conditional statement we have learned till now i.e. for-if-then-else
So, with that script this tutorial is now complete. We are now entering into deep territories of scripting . Remember don’t try to over-learn in a single day, be patient & practice as much as you can.
And leave your questions/queries down below. ADIOS for now !!!