Reader small image

You're reading from  Mastering Bash

Product typeBook
Published inJun 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781784396879
Edition1st Edition
Languages
Right arrow
Author (1)
Giorgio Zarrelli
Giorgio Zarrelli
author image
Giorgio Zarrelli

Giorgio Zarrelli is a passionate GNU/Linux system administrator and Debian user, but has worked over the years with Windows, Mac, and OpenBSD, writing scripts, programming, installing and configuring services--whatever is required from an IT guy. He started tinkering seriously with servers back in his university days, when he took part in the Computational Philosophy Laboratory and was introduced to the Prolog language. As a young guy, he had fun being paid for playing games and write about them in video game magazines. Then he grew up and worked as an IT journalist and Nagios architect, and recently moved over to the threat intelligence field, where a lot of interesting stuff is happening nowadays. Over the years, he has worked for start-ups and well-established companies, among them In3 incubator and Onebip as a database and systems administrator, IBM as QRadar support, and Anomali as CSO, trying to find the best ways to help companies make the best out of IT. Giorgio has written several books in Italian on different topics related to IT, from Windows security to Linux system administration, covering MySQL DB administration and Bash scripting.
Read more about Giorgio Zarrelli

Right arrow

Let's do something while, until…

The for loop is a great option to enumerate the contents provided by the user, but it is not so handy when it comes to handling a number of options whose number is not known beforehand. In this case, we would find more interesting kinds of loops, which would allow us to cycle until a certain condition is met or while a certain situation persists, for instance, while the user inputs something or until a threshold is met. So, let's see which constructs can help us:

while condition
do
command_1
command_2
command_n
done

At a first glance, the difference between the while and for loops is evident: the latter is based on a placeholder that each time takes a value from a list and we work on that value, the former is triggered while conditions last. Let's make an example starting with a for loop:

#!/bin/bash
for i in 1 2 3 4 5
do
...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Mastering Bash
Published in: Jun 2017Publisher: PacktISBN-13: 9781784396879

Author (1)

author image
Giorgio Zarrelli

Giorgio Zarrelli is a passionate GNU/Linux system administrator and Debian user, but has worked over the years with Windows, Mac, and OpenBSD, writing scripts, programming, installing and configuring services--whatever is required from an IT guy. He started tinkering seriously with servers back in his university days, when he took part in the Computational Philosophy Laboratory and was introduced to the Prolog language. As a young guy, he had fun being paid for playing games and write about them in video game magazines. Then he grew up and worked as an IT journalist and Nagios architect, and recently moved over to the threat intelligence field, where a lot of interesting stuff is happening nowadays. Over the years, he has worked for start-ups and well-established companies, among them In3 incubator and Onebip as a database and systems administrator, IBM as QRadar support, and Anomali as CSO, trying to find the best ways to help companies make the best out of IT. Giorgio has written several books in Italian on different topics related to IT, from Windows security to Linux system administration, covering MySQL DB administration and Bash scripting.
Read more about Giorgio Zarrelli