Access system information using standard shell variables.
Using Predefined System Variables
Every shell script is executed in an environment that includes the operating system, the user settings, the resources available, the network settings, and other information. Depending on the purpose of the script, some of these details may be relevant to how a script operates.
Shell scripts can access many different pieces of information about their environment. Many of these are related to UNIX process information or to commands.
This information, when accessed by a script, can help a shell script determine which actions to take.
Checking Number of command line Arguments
One type of information that can be accessed by the script is the number of parameters on the command line.
For example, if the task you have designed for your script to complete requires that the user supply two parameters on the command line, you can test the value of the $# variable to see how many parameters were supplied when the script was launched.
The $# variable holds the number of parameters on the command line that started the script. The test might look like this command (followed by other commands acting on this test):
if [ $# = 2 ];
The shell places each of the parameters from the command line into a positional variable as the script is launched.
You can then access these variables (such as $1, $2, etc.) in your script.
Checking system variables
Another type of information that can be accessed by the script is the UNIX process ID number.
If you need to know the UNIX process ID number of the shell script (perhaps to use it as a unique temporary filename), you can reference the $$ variable.
The shell script can also access system variables.
Many of the special
that are set by the shell when you run a script are like environment variables, but they are used to hold information about your computer system rather than information about specific programs.
One example is the OSTYPE variable.
By testing the value of this variable with an if/then statement, your shell script can determine where to look for files based on the operating system that the script is run on.
For example, if your script determines that the OSTYPE is Linux, the script might use different commands than if the script is being run on an HP/UX operating system.
The following MouseOver applet shows a small script that uses two different system variables to convert a database file using proprietary
programs called convertdb and filterdb. The script creates an intermediate temporary file using a system variable.
Process ID number: A unique number assigned by the UNIX kernel to each task (process) running on the system.