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

Exiting the loop with break and continue

This gives us some nice opportunities, such as infinite loops:

while true ; do echo "Hello" ; done

Since true always evaluates as true, the condition is always verified so we have an infinite execution of the do/done clause; press Ctrl+ C to exit from the loop. An infinite loop looks like something nasty, but it opens a new scenario for our scripts, since we can make them run or wait for something for as long as we want. Actually, if we do not use a couple of loop control commands: break will exit the loop and continue will restart it, jumping over the remaining commands. Let's see an example of creating a hypothetical backup program menu:

#!/bin/bash
while true
do
clear
cat <<MENU
BACKUP UTIL v 1.0
------------------
1. Backup a file/directory
2. Restore a file/directory
0. Quit
------------------
MENU
read ...
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