Datasheet

Task 1.9: Write Basic Scripts
49
For instance, consider Listing 1.4, which displays the current IP address of the computer on
which it runs. This script uses the variable $ip, which is extracted from the output of ifconfig
using grep and cut commands. (The trailing backslash on the second line of the script indicates
that the following line is a continuation of the preceding line.) When assigning a value to a vari-
able from the output of a command, that command should be enclosed in back-quote characters
(`), which appear on the same key as the tilde (~) on most keyboards. These are not ordinary sin-
gle quotes, which appear on the same key as the regular quote character (") on most keyboards.
Listing 1.4: Script Demonstrating Assignment and Use of Variables
#!/bin/sh
ip=`ifconfig eth0 | grep inet | cut -f 2 -d ":" | \
cut -f 1 -d " "`
echo "Your IP address is $ip"
Listing 1.4 relies on the networking command ifconfig, which is described in more detail
in Phase 6. You can type ifconfig by itself to see what its output includes. The second line
of Listing 1.4 uses grep and cut to isolate the IP address from the rest of the ifconfig output.
Scripts like Listing 1.4, which obtain information from running one or more commands,
are useful in configuring features that rely on system-specific information or information that
varies with time. You might use a similar approach to obtain the current hostname (using the
hostname command), the current time (using date), the total time the computer’s been run-
ning (using uptime), free disk space (using df), and so on. When combined with conditional
expressions (described shortly), variables become even more powerful because then your
script can perform one action when one condition is met and another in some other case. For
instance, a script that installs software could check free disk space and abort the installation
if there’s not enough disk space available.
One special type of variable was mentioned earlier in this chapter: environment variables.
Environment variables are assigned and accessed just like shell script variables. The difference
is that the script or command that sets an environment variable uses the export command to
make the value of the variable accessible to programs launched from the shell or shell script
that made the assignment. Environment variables are most often set in shell startup scripts, but
the scripts you use can access them. For instance, if your script calls X programs, it might
check for the presence of a valid $DISPLAY environment variable and abort if it finds that this
variable isn’t set. By convention, environment variable names are all uppercase, whereas non-
environment shell script variables are all lowercase or mixed case.
Using Conditional Expressions
Scripting languages support several types of conditional expressions. These enable a script to
perform one of several actions contingent on some condition—typically the value of a variable.
One common command that uses conditional expressions is if, which allows the system to take
one of two actions depending on whether some condition is true. The if keyword’s conditional
expression appears in brackets after the if keyword and can take many forms. For instance,
-f file is true if file exists and is a regular file; -s file is true if file exists and has a size
greater than 0; and string1 = string2 is true if the two strings have the same values.
83484.book Page 49 Monday, September 18, 2006 8:58 AM