set myname='Andrew Hudson' | tcsh |
Accessing Variable Values
You can access the value of a variable by prefixing the variable name with a $
(dollar sign). That is, if the variable name is var
, you can access the variable by using $var
.
If you want to assign the value of var
to the variable lcount
, you can do so as follows:
Command | Environment |
---|---|
lcount=$var | bash |
set lcount=$var | tcsh |
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 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.
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.
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.
Using a Simple Script to Automate Tasks
You could use a simple script to examine your system log for certain keywords. If the script is run via your system's scheduling table, /etc/crontab
, it can help automate security monitoring. By combining the output capabilities of existing Linux commands with the language facilities of the shell, you can quickly build a useful script to perform a task normally requiring a number of command lines. For example, you can create a short script, named greplog
, like this:
#!/bin/sh
# name: greplog
# use: mail grep of designated log using keyword