Thursday, April 18, 2024
HomeJavaMethods to use for loop in Linux? Looping by Listing, values, and...

Methods to use for loop in Linux? Looping by Listing, values, and vary in Bash with Instance


If you know the way to make use of for loop in bash shell or Linux, you possibly can velocity up a
lot of job, saving treasured time whereas doing assist or troubleshooting in
Linux. One in every of my favourite use of for loop is repeating identical job amongst
a number of host. For instance, If I’ve to wash up some recordsdata from 10 hosts
within the community, as a substitute of manually going and eradicating recordsdata from particular person
host, you should use for loop and
ssh command
to robotically take away previous log recordsdata from all of the host, with out manually
logging into them. This can be a super time saving trick if you’re working
for an utility which is operating on a couple of host. It is a life-saver
in case your utility is operating on 30 to 40 hosts. 

Right here is an instance of how you should use for loop to take away previous log recordsdata from
a number of hosts in Linux with out manually logging into every of them. 

$ for host in 1000 1001 1002 1003 1004 1005; 
do ssh "nys${host}" "rm -rf /app/knowledge/logs/*22032017*.log"; 
performed

This command will execute
SSH command
for every of those hosts and take away any log file with date 22032017. This
means you are able to do a job in 2 minutes which normally require greater than 15
minutes for those who attempt to do it manually. 

You need to use for loop to put in writing bash scripts however you may as well use it immediately
from command line for some repetitive job. Within the subsequent paragraph, I am going to present
you easy methods to execute for loop command in Linux in a single line proper from the
command immediate. 

Methods to loop by a listing in Linux? Instance

Let’s examine a few extra examples of for loop in bash shell or Linux. The
for loop is much like every other programming language e.g. you should use it
for repetition however it’s not index based mostly, as a substitute it’s checklist based mostly. That is
very a lot much like the enhanced for loop of Java 1.5. The assertion
written between do and performed key phrase are executed as soon as for every worth. You
can assume it as physique of for loop. 

right here is an instance of for loop in Linux to print every quantity within the checklist

$ for n in 1 2 3 4 5; do echo $n; performed;
1
2
3
4
5

Right here n is the variable, whereas 1 2 3 4 5 is the checklist. The physique of the for
loop i.e. the assertion between do and performed is simply an echo assertion to
print every quantity within the checklist. If you use for loop from command-line make
positive you sort all the pieces in a single line and use ; to segregate header and physique
of for loop.

Although, Linux for loop is not only restricted to static values utilized in the identical
line, you may as well use use vary values as we’ll see in subsequent instance. 

Methods to for loop values in vary in Linux

You are able to do by specifying vary in for loop as {1..10} which suggests the loop
will go from one to 10. that is how one can print values from one to 10 in
Linux utilizing for loop

$ for n in {1..10}; do echo $n; performed;
1
2
3
4
5
6
7
8
9
10

A extra sensible instance is once more going to a number of host and doing stuff.
For instance, to illustrate you might have 5 hosts whose numbers begins from 1000 to
1005, as a substitute of writing every of them as checklist in for loop header, you are able to do
following to get the identical consequence as we bought in first instance on this
tutorial.

for host in {1000..1005}; 
do ssh "nys${host}" "rm -rf /app/knowledge/logs/*22032017*.log"; 
performed

That is extra terse and you cannot solely avoid wasting keystrokes but additionally you
can scale to extra values simply. 

Methods to step by a listing by skipping

For instance you might have a listing of 100 servers and also you solely wish to login into
each second server, you are able to do that by skipping in for loop as proven
under. 

How to use for loop in Linux? Looping through List, values, and range in Bash with Example

The C or Java Fashion for loop in Linux

Though frequent utilization of for loop as proven in first instance is extra
widespread on bash scripts and amongst different Linux customers, you possibly can nonetheless write a C
or Java fashion basic for loop in Linux as proven under:

$ for ((i=0; i<10; i++)); do echo $i; performed;
0
1
2
3
4
5
6
7
8
9

Simply keep in mind that we now have two brackets
(( … ))
as a substitute of 1 in C or Java. The physique of for loop stays unchanged as you
nonetheless want do and performed as a substitute of start and finish and the code which can want
to repeated will go between them. 

That is all about fundamental examples of for loop in bash shell or Linux.
It is likely one of the great command which might simplify a variety of repetitive
job and might save tons of time, particularly if it’s a must to repeat the identical
factor throughout a number of hosts or file. You need to use for loop to login to a listing
of host and examine their stats or you should use for loop to repeat a listing of
recordsdata from one listing to a different.

Different Linux command Tutorials for Freshmen and Skilled

  • 10 examples of the xargs command in Linux (examples)
  • Methods to get an IP deal with from the hostname and vice-versa in Linux (command)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of date command in Linux (examples)
  • 10 Greatest Linux Programs for Programmers and Builders (Programs)
  • 10 Books each Linux Energy consumer ought to learn (books)
  • 5 Free Programs to be taught Linux for Freshmen (Free programs)
  • 10 examples of lsof command in Linux (examples)
  • 5 examples of type command in Linux (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 examples of chmod command in UNIX (examples)
  • 6 Free Programs to be taught Bash scripting in-depth (free programs)
  • 10 examples of curl command in Linux (examples)
  • Methods to create, replace and delete smooth hyperlink in UNIX (command)
  • 10 examples of reduce command in Linux (examples)
  • 10 examples of Vim in UNIX (examples)

Thanks for studying this text to date. For those who like this Linux for
loop  tutorial and examples, then please share it with your pals and
colleagues. In case you have any questions or suggestions, then please drop a be aware.

 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments