Title: Bash Scripting-7- Using FOR loop in shell script
1Bash Scripting-7- Using FOR loop in shell script
Posted by Prince DAN January 13, 2020 in DevOps
Use of FOR loop in shell script is done when we
need to run a command or a set of
instructions/commands over over until a
condition has been met. For example, changing the
name of some files or changing some information
about users on the system etc. Personally, I
have used this many times but most recently
created a script to automate AMI backup for an
AWS account (there are other ways as well but i
prefer my bash). Recommended Read Bash
Scripting-8- Taking user input with READ command
in shell script Also Read Scheduling CRON Jobs
with Crontab for Beginners
Syntax for using FOR loop in shell script
for var ( var_name) in list (parameters)
2do command done It seems straight forward and
simple, now lets discuss some examples for using
for loop in shell script. Example 1-
!/bin/bash for name in Dan Susan Daniel
do echo Welcome name done So the name of
the variable is name list of parameters is
dan susan daniel. So the output of the
following script would be, Welcome Dan Welcome
Susan Welcome Daniel We can also modify the
script to read from a variable or use the output
of a command as a list. Lets see an
example, !/bin/bash nameDan Susan Daniel
namenamemonti for member in name do echo
Welcome again member done
3Now the output would be, Welcome Dan Welcome
Susan Welcome Daniel Welcome monti We can also
create a file with a list instead of writing a
list inside a script, we can just mention the
path of the file that has the list. Lets see an
example, !/bin/bash file/home/ec2-user/list.tx
t for member in (cat file) do echo Welcome
member done /home/ec2-user/list.txt is the
list we enter names or other information either
one per line, or using a single space or a tab
between info. But we cant use commas to separate
them, caused by default space, tab newlines
are interpreted as separators. Another thing
that I would like to discuss is the use of escape
character while using for loop in shell script.
While using words like cant, dont, wont, we
either need to use double quotations or
escape symbol \ in, like Wont don\t cant
Using C language style for loop in shell script
Above mentioned way is not the only method that
we use for loop in shell script. We can also use
the for loop C language style. Lets see the
syntax, for ((i0 i10 i)) So here, we
mentioned to start loop at 0 it can go upto 10
with an increment of one. So lets say we need
to check about system ram continuously for 10
minutes, every sixty second, then we can use
this C style for loop to achieve this,
4Example !/bin/bash for ((i0 i600 ii10))
do echo RAM stats are free -m done Now lets
check another example where we are also utilizing
other conditional statements, !/bin/bash for
file in /home/ec2-user/ do if -d file
then echofile is a directory elif -f
file echo file is a file not a
directory fi done
So this is it for using for loop in shell script.
Please feel free to send in any request,
suggestion, or questions using the comment box
below.
5If you think we have helped you or just want to
support us, please consider these- Connect to
us Facebook T witter Linkedin TheLinuxGURUS
are thankful for your continued support.
TheLinuxGURUS.com