-->
Previous | Table of Contents | Next |
bash, pdksh, and tcsh all support a command called shift. The shift command moves the current values stored in the positional parameters one position to the left. For example, if the values of the current positional parameters are
$1 = -r $2 = file1 $3 = file2
and you execute the shift command
shift
the resulting positional parameters are as follows:
$1 = file1 $2 = file2
You can also move the positional parameters over more than one place by specifying a number with the shift command. The following command will shift the positional parameters two places:
shift 2
This is a very useful command when you have a shell program that needs to parse command-line options. This is true because options are typically preceded by a hyphen and a letter that indicates what the option is to be used for. Because options are usually processed in a loop of some kind, you often want to skip to the next positional parameter once you have identified which option should be coming next. For example, the following shell program expects two command-line optionsone that specifies an input file and one that specifies an output file. The program reads the input file, translates all the characters in the input file into uppercase, then stores the results in the specified output file. The following example was written using bash, pdksh syntax.
while [ $1 ] do if [ $1 = -i ] then infile=$2 shift 2 elif [ $1 = -o ] then outfile=$2 shift 2 else echo Program $0 does not recognize option $1 fi done tr a-z A-Z nfile $outfile
pdksh offers one iteration statement that neither bash nor tcsh provides. This is the select statement. This is actually a very useful statement. It is quite a bit different from the other iteration statements because it does not actually execute a block of shell code repeatedly while a condition is true or false. What the select statement does is enable you to automatically generate simple text menus. The syntax for the select statement is
select menuitem [in list_of_items] do commands done
where square brackets are used to enclose the optional part of the statement.
When a select statement is executed, pdksh creates a numbered menu item for each element in the list_of_items. This list_of_items can be a variable that contains more than one item, such as choice1 choice2, or it can be a list of choices typed in the command. For example:
select menuitem in choice1 choice2 choice3
If the list_of_items is not provided, the select statement uses the positional parameters just as it does with the for statement.
After the user of the program containing a select statement picks one of the menu items by typing the number associated with it, the select statement stores the value of the selected item in the menuitem variable. The statements contained in the do block can then perform actions on this menu item.
The following example illustrates a potential use for the select statement. This example displays three menu items and when the user chooses one of them, it asks whether that is the intended selection. If the user enters anything other than y or Y, the menu is redisplayed.
select menuitem in pick1 pick2 pick3 do echo Are you sure you want to pick $menuitem read res if [ $res = y -o $res = Y ] then break fi done
A few new commands are introduced in this example. The read command is used to get input from the user. It stores anything that the user types into the specified variable. The break command is used to exit a while, until, repeat, select, or for statement.
tcsh has an iteration statement that has no equivalent in pdksh or bash. This is the repeat statement. The repeat statement executes a single command a specified number of times. The syntax for the repeat statement is the following:
repeat count command
The following is an example of the repeat statement. It takes a set of numbers as command-line options and prints that number of periods to the screen. This program acts as a very primitive graphing program.
# foreach num ($*) repeat $num echo -n . echo end
Note:
Any repeat statement can be rewritten as a while or for statement. The repeat syntax is just more convenient.
Previous | Table of Contents | Next |