Iterative and Conditional Syntax

Iterative and conditional statements include the following:

  • for…do…done statements

  • while…do…done statements

  • until…do…done statements

  • select item in itemlist…do…done statements

  • if…elif…else…fi statements

  • case statements

For the syntax of these commands, have a look at the online bash2 documentation (see "To open command documentation").

Here's a bash script that uses interactive user input and an infinite while loop to sum as many integers as the user wants:

 #! /bin/bash # Interactive user input sum=0 while : do echo -n "Add another number (y/n):" read onward if [ $onward = n ] ; then break fi if [ $onward != y ] ; then echo '"y" or "n" please!'; continue fi echo -n "Enter a number to add to the sum:" read newnum sum=$(($sum ...

Get Red Hat Linux 6: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.