Shell scripting, specifically Bash scripting, is a great way to automate repetitive or tedious tasks on your systems. Why type when you can schedule and run scripts in a hands-free manner? One of the many scripting constructs is the loop. A loop is a section of code that picks up data or generates data and then performs some operation on the data and then begins the process over again until some condition is met, the script is disrupted, or when input data is exhausted. When you use Bash scripting, sometimes it is useful to perform actions using some form of loops.
[ Readers also liked: Five ways to use redirect operators in bash ]
The basic loop commands in Bash scripts are for
and while
.
for
loops are typically used when you have a known, finite list, like a series of numbers, a list of items, or counters.while
loops can be used with lists but are also useful for conditions that do not have a known limit. You can use it to run commands while a condition is true (or invert the logic and run it while the condition is false).
So, while (pun intended) there are different ways to perform loops, in this article, I focus on examples using while
loops.
1. A simple while loop
Imagine that you're installing a huge application, and you're concerned that it may eventually use too much space on the file system.
Instead of running df
in a separate terminal, you can run a simple command to monitor the disk utilization every second. This allows you to interrupt the installation process if you see that it will hit that limit.
while true
do
df -k | grep home
sleep 1
done
In this case, you're running the loop with a true condition, which means it will run forever or until you hit CTRL-C. Therefore, you need to keep an eye on it (otherwise, it will remain using the system's resources).
Note: If you use a loop like this, you need to include a command like sleep
to give the system some time to breathe between executions. Running anything non-stop could become a performance issue, especially if the commands inside the loop involve I/O operations.
2. Waiting for a condition to become true
There are variations of this scenario. For example, you know that at some point, the process will create a directory, and you are just waiting for that moment to perform other validations.
You can have a while
loop to keep checking for that directory's existence and only write a message while the directory does not exist.
If you want to do something more elaborate, you could create a script and show a more clear indication that the loop condition became true:
#!/bin/bash
while [ ! -d directory_expected ]
do
echo "`date` - Still waiting"
sleep 1
done
echo "DIRECTORY IS THERE!!!"
3. Using a while loop to manipulate a file
Another useful application of a while
loop is to combine it with the read
command to have access to columns (or fields) quickly from a text file and perform some actions on them.
In the following example, you are simply picking the columns from a text file with a predictable format and printing the values that you want to use to populate an /etc/hosts
file.
Here the assumption is that the file has columns delimited by spaces or tabs and that there are no spaces in the content of the columns. That could shift the content of the fields and not give you what you needed.
Notice that you're just doing a simple operation to extract and manipulate information and not concerned about the command's reusability. I would classify this as one of those "quick and dirty tricks."
Of course, if this was something that you would repeatedly do, you should run it from a script, use proper names for the variables, and all those good practices (including transforming the filename in an argument and defining where to send the output, but today, the topic is while
loops).
#!/bin/bash
cat servers.txt | grep -v CPU | while read servername cpu ram ip
do
echo $ip $servername
done
[ Learn the basics of using Kubernetes in this free cheat sheet. ]
Wrap up
while
loops are useful in scripts that depend on a certain condition to be true or false, but you can also use them interactively to monitor some condition.
The important things to remember are:
Always be clear about what is the condition to end the loop. If that is something that you expect to do yourself with a CTRL-C, then keep an eye on your command. Think about how fast you want each loop cycle to be repeated. If you do any operation that involves I/O inside the loop, that becomes a critical aspect of planning.
With that in mind, have fun while
you can.
Sobre o autor
Roberto Nozaki (RHCSA/RHCE/RHCA) is an Automation Principal Consultant at Red Hat Canada where he specializes in IT automation with Ansible. He has experience in the financial, retail, and telecommunications sectors, having performed different roles in his career, from programming in mainframe environments to delivering IBM/Tivoli and Netcool products as a pre-sales and post-sales consultant.
Roberto has been a computer and software programming enthusiast for over 35 years. He is currently interested in hacking what he considers to be the ultimate hardware and software: our bodies and our minds.
Roberto lives in Toronto, and when he is not studying and working with Linux and Ansible, he likes to meditate, play the electric guitar, and research neuroscience, altered states of consciousness, biohacking, and spirituality.
Navegue por canal
Automação
Últimas novidades em automação de TI para empresas de tecnologia, equipes e ambientes
Inteligência artificial
Descubra as atualizações nas plataformas que proporcionam aos clientes executar suas cargas de trabalho de IA em qualquer ambiente
Nuvem híbrida aberta
Veja como construímos um futuro mais flexível com a nuvem híbrida
Segurança
Veja as últimas novidades sobre como reduzimos riscos em ambientes e tecnologias
Edge computing
Saiba quais são as atualizações nas plataformas que simplificam as operações na borda
Infraestrutura
Saiba o que há de mais recente na plataforma Linux empresarial líder mundial
Aplicações
Conheça nossas soluções desenvolvidas para ajudar você a superar os desafios mais complexos de aplicações
Programas originais
Veja as histórias divertidas de criadores e líderes em tecnologia empresarial
Produtos
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Red Hat Cloud Services
- Veja todos os produtos
Ferramentas
- Treinamento e certificação
- Minha conta
- Suporte ao cliente
- Recursos para desenvolvedores
- Encontre um parceiro
- Red Hat Ecosystem Catalog
- Calculadora de valor Red Hat
- Documentação
Experimente, compre, venda
Comunicação
- Contate o setor de vendas
- Fale com o Atendimento ao Cliente
- Contate o setor de treinamento
- Redes sociais
Sobre a Red Hat
A Red Hat é a líder mundial em soluções empresariais open source como Linux, nuvem, containers e Kubernetes. Fornecemos soluções robustas que facilitam o trabalho em diversas plataformas e ambientes, do datacenter principal até a borda da rede.
Selecione um idioma
Red Hat legal and privacy links
- Sobre a Red Hat
- Oportunidades de emprego
- Eventos
- Escritórios
- Fale com a Red Hat
- Blog da Red Hat
- Diversidade, equidade e inclusão
- Cool Stuff Store
- Red Hat Summit