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:

Loop using select

To exit from the script, press Ctrl + C.

The while loop

The while loop allows you to do repetitive tasks until the condition is true. The syntax is very similar to what we have in the C and C++ programming language, which is as follows:

while [ condition ]
do
   # Task to perform
done

For example, read the name of the application and display pids of all the running instances of that application, as follows:

#!/bin/bash
# Filename: while_loop.sh
# Description: Using while loop to read user input

echo "Enter application name"
while read line
do
  echo -n "Running PID of application $line :"
  pidof $line
done

The output after running this script is as follows:

Enter application name
firefox
Running PID of application firefox : 1771
bash
Running PID of application bash : 9876 9646 5333 4388 3970 2090 2079 2012 1683 1336
ls
Running PID of application ls: 
systemd
Running PID of application systemd : 1330 1026 1

To exit from the script, press Ctrl + C.

The until loop

The until loop is very similar to the while loop, but the only difference is that it executes code block until the condition executes to false. The syntax of until is as follows:

until condition
do
     # Task to be executed
 done

For example, consider that we are interested in knowing pid of an application whenever any instance of it is running. For this, we can use until and check pidof of an application at a certain interval using sleep. When we find pid, we can exit from the until loop and print pid of the running instance of the application.

The following shell script demonstrates the same:

#!/bin/bash
# Filename: until_loop.sh
# Description: Using until loop to read user input

echo "Enter application name"
read app
until  pidof $app
do
  sleep 5
done
echo "$app is running now with pid 'pidof $app'"

The output after executing this script is as follows:

Enter application name
firefox
1867
firefox is running now with pid 1867
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.163.206