Datasheet
46
Phase 1
Working on the Command Line
When you’re done writing the shell script, you should modify it so that it’s executable.
You do this with the chmod command, as described in Task 1.2. Specifically, you use the +x
option to add execute permissions, probably in conjunction with a to add these permissions
for all users. For instance, to make a file called my-script executable, you’d issue the fol-
lowing command:
$ chmod a+x my-script
You’ll then be able to execute the script by typing its name, possibly preceded by ./ to tell
Linux to search in the current directory for the script. If the script is one you run regularly, you
may want to move it to a location on your path, such as /usr/local/bin. When you do that,
you won’t have to type the complete path or move to the script’s directory to execute it; you
can just type my-script.
Using Commands
One of the most basic features of shell scripts is the ability to run commands. You can use
both shell internal commands and external commands. Most of the commands you type in
a shell prompt are in fact external commands—they’re programs located in /bin, /usr/bin,
and other directories on your path. You can run such programs, as well as internal com-
mands, by including their names in the script. You can also specify parameters to such pro-
grams in a script. For instance, suppose you want to start a script that launches two xterm
windows and the KMail mail reader program. Listing 1.2 presents a shell script that accom-
plishes this goal.
Listing 1.2: A Simple Script That Launches Three Programs
#!/bin/bash
/usr/bin/xterm &
/usr/bin/xterm &
/usr/bin/kmail &
Aside from the first line that identifies it as a script, the script looks just like the commands
you might type to accomplish the task manually except for one fact: The script lists the com-
plete paths to each program. (You may need to modify the path to kmail; it’s not always
stored in /usr/bin.) This is usually not strictly necessary, but listing the complete path
ensures that the script will find the programs even if the $PATH environment variable changes.
Also, each program-launch line in Listing 1.2 ends in an ampersand (&). This character tells the
shell to go on to the next line without waiting for the first to finish. If you omit the ampersands
in Listing 1.2, the effect will be that the first xterm will open but the second won’t open until
the first is closed. Likewise, KMail won’t start until the second xterm terminates.
Although launching several programs from one script can save time in startup scripts and
some other situations, scripts are also frequently used to run a series of programs that manip-
ulate data in some way. Such scripts typically do not include the ampersands at the ends of the
commands because one command must run after another or may even rely on output from the
first. A comprehensive list of such commands is impossible because you can run any program
83484.book Page 46 Monday, September 18, 2006 8:58 AM










