System Variables

 < Day Day Up > 



You can see all the system variables by typing in set at the shell. You will see a rather long list like the one shown in Figure 20.7.

click to expand
Figure 20.7: The system variables.

Table 20.2 lists some of the more commonly used system variables and their functions.

Table 20.2: System Variables

System Variable

Meaning

BASH=/bin/bash

The name of the shell

BASH_VERSION=1.1

The shell version name

COLUMNS=80

The number of columns wide the screen should be

HOME=/home/ceasttom

The home directory

LINES=25

The number of vertical rows the screen should be

LOGNAME=ceasttom

The login name

OSTYPE=Linux

The operating system

PATH=/usr/bin:/sbin:/bin:/usr/sbin

The path settings

PS1=[\u@\h \W]\$

The prompt settings

PWD=/home/students/

The current working directory Common

SHELL=/bin/bash

The shell name

USERNAME=jsmith

Username currently logged in to this PC

You can use the echo command to print any of these variables from inside a script. The echo command means to display to the screen. This command also works in Windows batch files. Let’s look at a script that displays some of the system variables to the screen.

Type the following into your favorite text editor and save it as scriptthree.sh:

  #!/bin/bash   echo $USERNAME   echo $HOME   echo $SHELL

You should then go to the shell and enter bash scriptthree.sh. You will see something similar to what is shown in Figure 20.8. Of course, your display will be slightly different because your username and home directory are different.

click to expand
Figure 20.8: Displaying system variables.

So you can see that writing a script to display the system variables is not particularly difficult. Before we continue too far, we should examine this echo command a bit more closely. In addition to using echo to display system commands to the screen, you also can use it to echo simple text. You can use this text to inform the user of anything you want. Let’s look at the previous script, written to use the echo command to clarify the data we are displaying.

  #!/bin/bash   echo Hey this script will display your current    echo home directory, user name, and shell   echo Home directory is $HOME   echo Username is $USERNAME   echo shell is $SHELL   exit 0

If you save this as scriptfour.sh and run it from the shell, you should see something like what is shown in Figure 20.9.

click to expand
Figure 20.9: Adding to the script with echo.

As you can see, this script is more user friendly that the first one. Instead of displaying a list of values, it tells the user what it intends to display, and then it labels each value. You will find your scripts more user friendly if you use the echo command to provide the user with some additional information.

User-Defined Variables

As we mentioned, a user-defined variable is, as its name suggests, a variable you create for some specific purpose in your script. You will find that most scripts of any complexity at all will require variables. The generic syntax for defining a user-defined variable is shown here:

  variable name=value

This means you provide some name for your variable, followed by an initial value to put in that variable. For example, you might have a variable that is designed to hold a person’s age. You would declare that in the following manner:

  age=20

Of course, you can store more than numbers in a variable. Perhaps you want a variable to store last names. You would declare that variable in this manner:

  lastname=Smith

You have a great deal of flexibility in naming variables, but there are some rules that must be obeyed. Variable names must begin with an alphanumeric character or underscore character (_), followed by one or more alphanumeric characters. The following are all examples of valid variable names:

  lastname   _lastname   variable7

The second rule to remember regards the use of the equals sign (=) to assign values to your variables. You must remember to not put spaces on either side of the equals sign when assigning value to a variable. For example, the following variable declaration is correct:

  no=20

The following variable declarations are all in error:

  age =20   age= 20   age = 20

The third rule for you to remember is that variable names, like everything in Linux, are case sensitive. The following creates three variables named age, Age, and AGE, each with an initial value of 20.

  age=20   Age=20   AGE=20

This actually creates three variables named age, each with an initial value of 20.

The fourth rule regarding variable declaration regards null variables. A null variable is defined as one without any initial value. You can define a null variable by not assigning it any value after the equals sign, as you see here:

  age=   lastname=

Creating a variable is not particularly difficult. Let’s examine a brief script that creates a couple of variables and prints their values to the screen.

  #!/bin/bash   lastname=Easttom   firstname=Chuck   echo My name is $firstname $lastname   exit 0

Save this script as scriptfive.sh and run it from the shell. You should see what is shown in Figure 20.10.

click to expand
Figure 20.10: Using variables.

Now we see how to create variables, but they will not be of much use unless we can do two things. The first is to put data in them. This will often be data from the user, so we need to explore how to get data from the user. We will also need to manipulate data arithmetically. Both topics will be examined in the next two portions of this chapter.



 < Day Day Up > 



Moving From Windows to Linux
Moving From Windows To Linux (Charles River Media Networking/Security)
ISBN: 1584502800
EAN: 2147483647
Year: 2004
Pages: 247
Authors: Chuck Easttom

Similar book on Amazon

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