Datasheet
Task 1.9: Write Basic Scripts
47
you can install in Linux as a command—even another script. The following list includes a few
commands that are commonly used in scripts:
Normal file-manipulation commands The file-manipulation commands, such as ls, mv, cp,
and rm, are often used in scripts. You can use these commands to help automate repetitive file
maintenance tasks.
grep This command is described earlier, in “Piping Data between Programs” in Task 1.7. It
locates files that contain specific strings.
find Where grep searches for patterns within the contents of files, find does so based on file-
names, ownership, and similar characteristics. This command is described earlier, in Task 1.4.
cut This command extracts text from fields in a file. It’s frequently used to extract variable
information from a file whose contents are highly patterned. To use it, you pass it one or more
options that control what it cuts followed by one or more filenames. For instance, users’ home
directories appear in the sixth colon-delimited field of the /etc/passwd file. You could there-
fore include cut -f 6 -d “:” /etc/passwd in a script to extract this information.
sed This program provides many of the capabilities of a conventional text editor but via
commands that can be typed at a command prompt or entered in a script.
echo Sometimes a script must provide a message to the user; echo is the tool to accomplish
this goal. You can pass various options to echo or just a string to be shown to the user. For
instance, echo “Press the Enter key” causes a script to display the specified string.
mail The mail command can be used to send email from within a script. Pass it the -s subject
parameter to specify a subject line and give it an email address as the last argument. If it’s used at
the command line, you would then type a message and terminate it with a Ctrl+D. If it’s used from
a script, you might omit the message body entirely or pass it an external file as the message using
input redirection. You might want to use this command to send mail to the superuser about the
actions of a startup script or a script that runs on an automated basis.
Many of these commands are extremely complex. You can consult their man
pages for more information.
Even if you have a full grasp of how to use some key external commands, simply executing
commands you might type at a command prompt is of limited utility. Many administrative
tasks require you to modify what you type at a command, or even what commands you enter,
depending on information from other commands. For this reason, scripting languages include
additional features to help you make your scripts useful.
Using Variables
Variables can help you expand the utility of scripts. A variable is a placeholder in a script for
a value that will be determined when the script runs. Variables’ values can be passed as param-
eters to scripts, generated internally to the scripts, or extracted from the scripts’ environments.
83484.book Page 47 Monday, September 18, 2006 8:58 AM










