Positional Parameters


You can pass options from the command line or from another script to your shell program. These options are called positional parameters and have special names provided by the system. You must understand how to use positional parameters and pull in variables retrieved from the command line to develop more advanced shell programs.

The names for positional parameters are really numbers; the first parameter is 1, the second 2, the third 3, and so on. Access the parameter using $1, $2, and so on. You cannot invoke 2 before 1, but you can omit 3 to get a result if 1 and 2 are present.

Let's say we have a shell program that expects two parameters a first name and a last name. If you have only a first name (1), that's OK. If you just have the last name (2), you'll get an error.

This shell program, myname1, takes one parameter (a name) and displays it onscreen:

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

Note

Notice that in shell scripting, every if statement must end with fi.


If you run myname1 like this:

bash myname1 

You'll get this result:

Name not provided 

If you run myname1 like this:

bash myname1 Mike 

This is the output:

Your name is Mike 

You may have wondered what that $# code was in myname1. This is a built-in variable provided by the kernel to the shell, returning the number of positional parameters passed to the shell program. There's more about these built-in variables in the next section.

Using Positional Parameters to Get Command-Line Variables

Positional parameters can be really useful if you have piped commands using complex arguments, especially if the commands aren't used frequently. For example, you can use your system with a voice modem to serve as an answering machine. If you do, you can write a script that retrieves and plays the messages. This script, called pvm, converts a saved sound file (in .rmd or voice-phone format) and pipes the sound to your audio device:

#!/bin/sh # play voice message(s) in /var/spool/voice/incoming rmdtopvf /var/spool/voice/incoming/$1 | pvfspeed -$ 0000 ; \ pvftobasic >/dev/audio 

Play the message by typing the following at the shell prompt:

pvm name_of_message 

Automating Tasks with Scripts

Shell scripts with positional parameters are often used for automating routine and mundane jobs, such as generating system log reports, checking the file system, user resource accounting, printer use accounting, and other system, network, or security administration tasks.

This script, if added to your /etc/crontab scheduling table, can help you monitor your system by running a keyword search on your logs for important information.

#!/bin/sh #    name:    greplog #     use:      mail grep of designated log using keyword # # author: bb # usage: greplog {keyword} {logpathname} # # bugs: does not check for correct number of arguments # build report name using keyword search and date log_/report=/tmp/$1.logreport. `date '+%m%d%y'` # build report header with system type, hostname, date and time echo "=============================================================" \     >$log_report echo "            S Y S T E M   M O N I T O R   L O G" >>$log_report echo uname -a >>$log_report echo "Log report for" `hostname -f`  "on" `date '+%c'`    >>$log_report echo "=============================================================" \     >>$log_report ; echo "" >>$log_report # record log search start echo "Search for->" $1 "starting" `date '+%r'` >>$log_report echo "" >>$log_report # get and save grep results of keyword ($1) from logfile ($2) grep -i $1 $2 >>$log_report # build report footer with time echo "" >>$log_report echo "End of" >>$log_report at `date '+%r'` >>$log_report # mail report to root mail -s "Log Analysis for $1" root <$log_report # clean up and remove report rm $log_report exit @ 

This script creates the $log_report variable, which stores the report. The $1 keyword and first argument on the command line is used as part of the filename, along with the current date. The report header has some formatted text, the system name (output of the uname command), the hostname, and date. The script records the start of the search, and any keyword matches in the log are added to the report. A footer containing the report name and time is added. The report is mailed to root, with the search term as the subject of the message, and the $log_report file is deleted.

Test the script by running it yourself. Make sure that the system is running the syslogd daemon. Feed it a keyword and a pathname to the system log /var/log/messages, like this:

greplog FAILED /var/log/messages 

If any login failures have occurred on your system, root should get an email message. If you have correctly configured your email services (see "Setting Up Your SUSE Linux Email Account" in Chapter 13, "Using the Internet: Browsing the Web and Writing Email"), the SuperUser should get that email.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net