Книга: Fedora™ Unleashed, 2008 edition

Positional Parameters

Positional Parameters

It is possible to pass options from the command line or from another shell script to your shell program.

These options are supplied to the shell program by Linux as positional parameters, which have special names provided by the system. The first parameter is stored in a variable called 1 (number 1), and you can access it by using $1 within the program. The second parameter is stored in a variable called 2, and you can access it by using $2 within the program, and so on. One or more of the higher numbered positional parameters can be omitted while you're invoking a shell program.

Understanding how to use these positional parameters and how to access and use variables retrieved from the command line is necessary when developing more advanced shell programs.

A Simple Example of a Positional Parameter

For example, if a shell program mypgm expects two parameters — such as a first name and a last name — you can invoke the shell program with only one parameter, the first name. However, you cannot invoke it with only the second parameter, the last name.

Here is a shell program called mypgm1 , which takes only one parameter (a name) and displays it on the screen:

#!/bin/sh
#Name display program
if [ $# -eq 0 ] then
 echo "Name not provided"
else
 echo "Your name is "$1
fi

If you execute mypgm1 , as follows

$ bash mypgm1

you get the following output:

Name not provided

However, if you execute mypgm1, as follows

$ bash mypgm1 Andrew

you get the following output:

Your name is Andrew

The shell program mypgm1 also illustrates another aspect of shell programming: the built- in variables provided to the shell by the Linux kernel. In mypgm1, the built-in variable $# provides the number of positional parameters passed to the shell program. You learn more about working with built-in variables in the next major section of this chapter.

Accessing and Retrieving Variables

Using positional parameters in scripts can be helpful if you need to use command lines with piped commands requiring complex arguments. Shell programs containing positional parameters can be even more convenient if the commands are infrequently used. For example, if you use your Fedora system with an attached voice modem as an answering machine, you can write a script to issue a command that retrieves and plays the voice messages. The following lines convert a saved sound file (in RMD or voice-phone format) and pipe the result to your system's audio device:

#!/bin/sh
# play voice message in /var/spool/voice/incoming
rmdtopvf /var/spool/voice/incoming/$1 | pvfspeed -s 8000 |
pvftobasic >/dev/audio

A voice message can then easily be played back if you use this script (perhaps named pmm):

$ pmm name_of_message

Shell scripts that contain positional parameters are often used for automating routine and mundane jobs, such as system log report generation, file system checks, user resource accounting, printer use accounting, and other system, network, or security administration tasks.

Оглавление книги


Генерация: 1.178. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз