- [Man] Field separators and iterators.
In the previous video, we've learned how to run
a command until it succeeds.
In this video, we'll learn about
field separators and iterators.
Internal field separator, IFS,
is an important concept in shell scripting.
It's very useful while manipulating text data.
We'll now discuss delimiters that separate
different data elements from a single data stream.
An internal field separator is a delimiter
for a special purpose.
An internal field separator is an environment
variable that stores delimiting characters.
It's the default delimiter string used
by running shell environment.
Consider the case where we need to iterate through words
in a string or comma separated values, CSV.
In the first case, where we use
IFS equals quotes.
In the second IFS equals comma.
Let's see how to do it.
Consider the case of CSV data.
The output is as.
The default value of IFS is,
a space component, new line, tab or space character.
When IFS is set as the shell interrupts the comma
as a delimiter character, therefore the dollar
item variable takes sub strings separated by a comma
as its value during iteration.
If IFS is not set as comma, then it would print
the entire data as a single string.
Let's go through another example usage of IFS
by taking the slash etc slash
password file into consideration.
In the slash etc slash password file,
every line contains items delimited by colon.
Each line in the file corresponds to an attribute
related to a user.
Consider the input, root colon ex colon zero colon zero
colon root colon slash root colon slash bin slash bash.
The last entry on each line
specifies the default shell for the user.
To print users and their default shells,
we can use the IFS hack as.
The output will be.
Loops are very useful in iterating
through a sequence of values.
Bash provides many types of loops.
Let's see how to use them.
Using a for loop.
We can generate different sequences easily.
Echo curly bracket one dot dot five zero curly brackets
can generate a list of numbers from one to 50.
Echo curly bracket a dot dot zed or
a dot dot zed or a dot dot h
can generate lists of alphabets.
Also by combining these we can concatenate data.
In this code, in each iteration,
the variable I will hold a character in the range a to z.
The for loop can also take the format of the for loop in c.
For example.
Using a while loop.
For an infinite loop, use true as the condition.
Using an until loop.
A special loop called until is available with bash.
This executes the loop until
the given condition becomes true.
For example.
Awesome.
We've learned about field separators and iterators.
In the next video we'll learn about comparisons and tests.