Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

The select Statement (pdksh)


You use the select statement to generate a menu list if you are writing a
shell program that expects input from the user online. The format of the
select statement is as follows:


Click here to view code image
select item in itemlist
do
Statements
Done


itemlist is optional. If it isn’t provided, the system iterates through the
item entries one at a time. If itemlist is provided, however, the system
iterates for each entry in itemlist, and the current value of itemlist is
assigned to item for each iteration, which then can be used as part of the
statements being executed.


If you want to write a menu that gives the user a choice of picking a
Continue or a Finish, you can write the following shell program:


Click here to view code image
#!/bin/ksh
select item in Continue Finish
do
if [ $item = "Finish" ]; then
break
fi
done


When the select command is executed, the system displays a menu with
numeric choices—in this case, 1 for Continue and 2 for Finish. If the
user chooses 1 , the variable item contains the value Continue; if the user
chooses 2 , the variable item contains the value Finish. When the user
chooses 2 , the if statement is executed, and the loop terminates.


The shift Statement


You use the shift statement to process the positional parameters, one at a
time, from left to right. Recall that the positional parameters are identified as
$1, $2, $3, and so on. The effect of the shift command is that each
positional parameter is moved one position to the left, and the current $1
parameter is lost.


The shift statement is useful when you are writing shell programs in which

Free download pdf