Book Image

Linux Shell Scripting Essentials

By : Sinny Kumari, Khem Raj
Book Image

Linux Shell Scripting Essentials

By: Sinny Kumari, Khem Raj

Overview of this book

Table of Contents (15 chapters)
Linux Shell Scripting Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The select, while, and until loops


The select, while and until loops are also used to loop and iterate over each item in a list or till the condition is true with slight variations in syntax.

Loop using select

The select loop helps in creating a numbered menu in an easy format from which a user can select one or more options.

The syntax of the select loop is as follows:

select var in list
do
   # Tasks to perform
done

The list can be pre-generated or specified while using the select loop in the form [item1 item2 item3 …].

For example, consider a simple menu listing the contents of '/' and asking a user to enter an option for which you want to know whether it is a directory or not:

#!/bin/bash
# Filename: select.sh
# Description: Giving user choice using select to choose

select file in 'ls /'
do
   if [ -d "/"$file ]
   then
     echo "$file is a directory"
   else
     echo "$file is not a directory"
  fi
done

The following is the screenshot of the output after running the script:

To exit from the...