Access system information using standard shell variables.
Using Predefined System Variables
Question: How do I access system information using standard shell variables in a Unix Shell Script?
There are a number of standard shell variables that can be used to access system information in a Unix shell script. These variables are set by the operating system and contain information such as the system name, hostname, kernel version, and current user.
Some of the most commonly used standard shell variables for accessing system information include:
$HOSTNAME: The hostname of the system.
$OSTYPE`: The operating system type.
$KERNEL_VERSION`: The kernel version.
$USER`: The current user.
$PWD`: The current working directory. To access these variables in a shell script, you can simply use the dollar sign () followed by the variable name.
For example, to print the hostname of the system, you would use the following code:
echo "The hostname of the system is $HOSTNAME"
This code would print the hostname of the system to the console.
Here is a list of some of the other standard shell variables that can be used to access system information:
$MACHTYPE: The machine type.
$ARCH: The architecture.
$RELEASE: The operating system release.
$VERSION: The operating system version.
$DIST: The distribution name.
Here is an example of a shell script that uses standard shell variables to access system information:
#!/bin/bash
# Print the hostname of the system
echo "The hostname of the system is $HOSTNAME"
# Print the operating system type
echo "The operating system type is $OSTYPE"
# Print the kernel version
echo "The kernel version is $KERNEL_VERSION"
# Print the current user
echo "The current user is $USER"
# Print the current working directory
echo "The current working directory is $PWD"
To run this script, you would save it as a file with the .sh extension and then run it from the command line.
For example, if you saved the script as system-info.sh, you would run it by typing the following command:
bash system-info.sh
This would print the system information to the console.
Shell Script execution Environment
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 ];
then
…
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.