Tải bản đầy đủ (.pdf) (3 trang)

Self-Linked Scripts

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (58.05 KB, 3 trang )

87
■ ■ ■
CHAPTER 13
Self-Linked Scripts
T
he technique I am about to discuss allows you to have a single script that can be called
in several ways by different names. The script itself contains all the code necessary to
perform a number of tasks, but you may want to determine the specific task to be per-
formed at runtime by calling the script by a specific name. This lets users invoke an
individual task by name without having to learn specialized command-line switches for
the options they want.
There are two of ways of specifying the precise behavior of a script. One way is to have
the script accept command-line options telling it how to act. This method is covered
in Chapter 5, which dealt with the use of getopts. This chapter illustrates the second
method: calling a script by giving it multiple names.
Sometimes you want to have multiple scripts perform tasks that are related in some
way but have slight differences. An example, albeit a silly one, is a script that monitors disk
consumption of the / file system. The script will determine the disk utilization percentage
from the output of the df command. The following is a sample of df output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda2 18334940 13019804 4383768 75% /
/dev/hda1 256666 25241 218173 11% /boot
none 257300 0 257300 0% /dev/shm
/dev/hdb1 16577308 11732468 4002760 75% /snapshot
For illustrative purposes, our script doesn’t do the obvious and use the Use% value from
the df output; it instead calculates disk consumption directly from the 1K-blocks and Used
values. So now you have a script that can display the percent utilization of the / file sys-
tem. Let’s call this root_check.
#!/bin/sh
fs="/"
fs_total=`df -k $fs | tail -n 1 | awk '{print $2}'`


fs_used=`df -k $fs | tail -n 1 | awk '{print $3}'`
percent_used=$((100*$fs_used/$fs_total))
echo "$fs is at ${percent_used}% capacity"
Now suppose you want to have a similar script, called boot_check, that will perform the
same file-system-capacity check on the /boot partition. First we create a soft link (ln -s)
88
CHAPTER 13

SELF-LINKED SCRIPTS
called boot_check that points to the original root_check script. The following shows the
modification to the root_check code that will allow the script to determine how it was
called:
#!/bin/sh
whatami=`basename $0`
The basename command strips off a file’s leading path elements. In this case, basename
returns the positional parameter $0 containing the name used to invoke the script. The
script could be called with its fully qualified name or with a relative path; the basename
command gives you only the name of the script without those variable items.

Note
If the script is being run in Setuid
1
mode, the shell will drop the environment variables for security
reasons, and the value of $0 will become the name of the shell that is running the script.
This case structure is what allows multiple command names to work. If the name of the
command is root_check, we set the fs variable to the / file system. If it is boot_check, we
set the fs variable to /boot, and so on. The following example sets only one value in each
case, but it could just as easily change many more elements, depending on your needs:
case $whatami in
root_check)

fs="/"
;;
boot_check)
fs="/boot"
;;
snap_check)
fs="/snapshot"
;;
*)
echo "Don't know what to do. Exiting"
;;
esac
Once the fs variable is set, all the rest of the code is the same as before.
fs_total=`df -k $fs | tail -n 1 | awk '{print $2}'`
fs_used=`df -k $fs | tail -n 1 | awk '{print $3}'`
percent_used=$((100*$fs_used/$fs_total))
echo "$fs is at ${percent_used}% capacity"
1. Setuid is a permission setting that allows a program to be run as the owner of the program instead of as
the user who is running the utility.
CHAPTER 13

SELF-LINKED SCRIPTS
89
One interesting item to note with this example is that the Use% column displayed
by the df -k command output is significantly different from this script’s output, even
though they are both supposedly based on the same numbers. The df command is likely
performing its calculations with some rounding involved for a quick view of the per-
centage used instead of the exact value. The df output tends to report a larger value than
does the calculated output. The actual difference in calculation wouldn’t be known
without consulting the source code for the df command.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×