Выбрать главу

$ env

...

VENDOR=intel

MACHTYPE=i386

HOSTTYPE=i386-linux

HOST=werewolf.hudson.com

On the other hand, bash might provide these variables or variables of the same name with a slightly different definition, such as these:

$ env

...

HOSTTYPE=i386

HOSTNAME=werewolf.hudson.com

Although the behavior of a shebang line is not defined by POSIX, variations of its use can be helpful when you are writing shell scripts. For example, as described in the want man page, you can use a shell to help execute programs called within a shell script without needing to hard-code pathnames of programs. The want command is a windowing Tool Control Language (tcl) interpreter that can be used to write graphical clients. Avoiding the use of specific pathnames to programs increases shell script portability because not every UNIX or Linux system has programs in the same location.

For example, if you want to use the want command, your first inclination might be to write the following:

#!/usr/local/bin/wish

Although this works on many other operating systems, the script fails under Linux because want is located under the /usr/bin directory. However, if you write the command line this way

#!/bin/sh

exec wish "$@"

the script always finds the correct binary.

Using Variables in Shell Scripts

When writing shell scripts for Linux, you work with three types of variables:

► Environment variables — Part of the system environment, you can use them in your shell program. New variables can be defined, and some of them, such as PATH, can also be modified within a shell program.

► Built-in variables — These variables, such as options used on the command (interpreted by the shell as a positional argument), are provided by Linux. Unlike environment variables, you cannot modify them.

► User variables — Defined by you when you write a shell script. You can use and modify them at will within the shell program.

A major difference between shell programming and other programming languages is that in shell programming, variables are not typed — that is, you do not have to specify whether a variable is a number or a string, and so on.

Assigning a Value to a Variable

Assume that you want to use a variable called lcount to count the number of iterations in a loop within a shell program. You can declare and initialize this variable as follows:

Command Environment
lcount=0 bash
set lcount=0 tcsh
NOTE

Under bash, you must ensure that the equal sign (=) does not have spaces before and after it.

To store a string in a variable, you can use the following:

Command Environment
myname=Andrew bash
set myname=Andrew tcsh

Use the preceding variable form if the string doesn't have embedded spaces. If a string has embedded spaces, you can do the assignment as follows:

Command Environment
myname="Andrew Hudson" bash
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 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: