How to use a Bash script for-loop for system administration in Linux

Bash scripting is each individual Linux administrator’s Swiss Military knife. Understand how using a for-loop in conjunction with Bash scripts can produce highly effective success.

Impression: jivacore/Shutterstock

Bash scripting continues to be a staple of my endeavors in Linux as a method administrator, and I use it every single day to relieve my workload. 1 of my most generally applied form of shell scripts consists of a for-loop which processes and applies commands to servers based mostly on a textual content file I’ve designed containing the concentrate on host names. 

SEE: Kubernetes: A cheat sheet (free of charge PDF)  (TechRepublic)

These scripts can duplicate data files to servers or log into these methods via ssh to operate many instructions. This system is most effective utilized with
SSH host keys in position

to permit passwordless accessibility to target systems or else you can have to sort a password in for each and every technique to which your script will connect. You must have the ideal permissions to run these instructions as properly.

It can not be overstated that your list of focus on hosts need to be precise and only incorporate methods on which these instructions should really be operate, in any other case you can incredibly speedily wreak havoc by deleting data files or rebooting hosts which should not be on this list. Rebooting production programs can be what we in IT call a CEE—career-ending occasion.

Notice that these scripts need to comprise a .sh extension and be set to executable employing chmod +x (script name). If the file identify made up of the listing of servers is not in the identical directory you might be running the script from you can want to specify the whole route in the script. I generally run my scripts from a subfolder underneath “root.”

In this article is an case in point of one particular of my basic scripts, “” This script is supposed to start the besclient provider (BigFix, made use of for process patching) on all the hosts shown in a text file called “servers”:

for l in `cat servers` do echo $lssh -q $l service besclient restartecho $l done

The script utilizes “for I” as in a lowercase L (not to be perplexed with i or I), meaning to cat the servers text file and return just one-by-just one each and every host name to use to carry out the commands pursuing the semi-colon. “I” simply just refers to each host title entry. Every semi-colon separates the instructions in the chain.

Be aware that ` is not a single estimate it is the “again quotation” character and shares the exact same vital as the tilde (~) in the upper left of a regular keyboard.

“do echo $l” will output the host identify ahead of it operates the next command.

Now arrives the nitty-gritty, where ssh -q $l tells the script to ssh to the concentrate on host. It can be now on the remote box and can kick off the “provider besclient restart” command.

The “echo $I” shows the hostname included as a way of confirming the command ran productively. 

SEE: From start to complete: How to deploy an LDAP server (TechRepublic Top quality)

Finally, “done” finishes the command chain and moves on to the subsequent process, or finishes running the script if there are no a lot more methods to work on.

Let’s say your servers file is made up of these merchandise:


Right after the script operates you would see output related to the adhering to, assuming the method could link to all the targets.


If you see a solitary entry for a host identify, check out to make certain connectivity to that host is performing and consider all over again. 

Right here is an additional sample script which connects to a technique, stops a support, deletes a file, copies a new file to the system by using scp, then begins the associated provider.

for l in `cat servers` do echo $lssh -q $l provider besclient stopecho $l finished 
for l in `cat servers` do echo $lssh -q $l rm -rf /var/decide/BESClient/besclient.configecho l done 
for l in `cat servers` do scp besclient.config $l:/var/opt/BESClient/echo $l performed 
for l in `cat servers` do echo $lssh -q $l services besclient startecho $l performed

In no unique get, listed here are the principal scripts I rely on for day by day responsibilities:

This script can prevent a support, eliminate the linked rpms and then delete the app directory to clean up goal devices:

for l in `cat servers` do echo $lssh -q $l support besclient stopecho $l completed 
for l in `cat servers` do echo $lssh -q $l rpm -e BESAgent- $l completed 
for l in `cat servers` do echo $lssh -q $l rpm -e BESAgent- $l performed 
for l in `cat servers` do echo $lssh -q $l rm -rf /and many others/opt/BESClient /var/choose/BESClient /decide/BESClientecho $l finished 

This script gathers some components aspects from the concentrate on hosts which can be handy for inventorying devices:

 for l in `cat servers` do echo $lssh -q $l dmidecode | grep -i serialecho $l accomplished
 for l in `cat servers` do echo $lssh -q $l lscpu | grep "CPU(s):"echo $l accomplished
 for l in `cat servers` do echo $lssh -q $l lscpu | grep "Core(s) per socket:"echo $l performed
 for l in `cat servers` do echo $lssh -q $l lscpu | grep "Socket(s):"echo $l carried out 
 for l in `cat servers` do echo $lssh -q $l no cost -hecho $l accomplished 

This script copies another script to goal devices /and many others/ folders so these scripts will be executed each individual 24 several hours:

 for l in `cat servers` do ssh -q $l /etcetera/ completed 

This script sets the operator of a sure file on target systems to be “prod1”:

 for l in `cat servers` do echo $lssh -q $l chown prod1:prod /fileData/fileroot/certs/interac.jksecho $l completed 

This script tells all concentrate on hosts to run a yum update and not prompt to put in offers:

 for l in `cat servers` do ssh -q $l yum -y update echo $l finished 

This script backs up the /and many others/shadow file on all goal hosts to the root directory:

 for l in `cat servers` do echo $lssh -q $l cp /and so on/shadow /root/shadow20210604echo $l performed 

This script reboots all servers detailed in the servers file:

 for l in `cat servers` do ssh -q $l shutdown -r now echo $l done 

This script can occur in useful to unmount an present NFS volume then update /etcetera/fstab with a new NFS mount title and remount all volumes:

 for l in `cat servers` do echo $lssh -q $l umount /cloverecho $l finished 
 for l in `cat servers` do echo $lssh -q $lsed -r -i 's|vnx-corp-nfs1|unity-corp-nfs01|' /and so forth/fstabmount -aecho $l      carried out 

This script can insert a host file entry to goal devices

 for l in `cat servers` do ssh -q $l 'echo "  github.devops.area" >> /etcetera/hosts' echo $l performed 

At last, this script can tell you the present-day uptime on all goal hosts:

 for l in `cat servers` do echo $lssh -q $l uptimeecho $l finished 

As you can see, the operation of these scripts is restricted only by your creativity. I hope these are practical and that they ease your administrative workload.

Also see

Fibo Quantum