Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [17]

By Root 866 0
commands are just that: commands that the shell itself executes. Some commands are built-in from necessity, such as cd to change the directory, or read to get input from the user (or a file) into a shell variable. Other commands are often built into the shell for efficiency. Most typically, these include the test command (described later in Section 6.2.4), which is heavily used in shell scripting, and I/O commands such as echo or printf.

Shell functions are self-contained chunks of code, written in the shell language, that are invoked in the same way as a command is. We delay discussion of them until Section 6.5. At this point, it's enough to know that they're invoked, and they act, just like regular commands.

External commands are those that the shell runs by creating a separate process. The basic steps are:

Create a new process. This process starts out as a copy of the shell.

In the new process, search the directories listed in the PATH variable for the given command. /bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin might be a typical value of PATH. (The path search is skipped when a command name contains a slash character, /.)

In the new process, execute the found program by replacing the running shell program with the new program.

When the program finishes, the original shell continues by reading the next command from the terminal, or by running the next command in the script. This is illustrated in Figure 2-1.

Figure 2-1. Program execution

That's the basic process. Of course, the shell can do many other things for you, such as variable and wildcard expansion, command and arithmetic substitution, and so on. We'll touch on these topics as we progress through the book.

Variables

A variable is a name that you give to a particular piece of information, such as first_name or driver_lic_no. All programming languages have variables, and the shell is no exception. Every variable has a value, which is the contents or information that you assigned to the variable. In the case of the shell, variable values can be, and often are, empty—that is, they contain no characters. This is legitimate, common, and useful. Empty values are referred to as null, and we'll use that term a lot in the rest of the book.

Shell variable names start with a letter or underscore, and may contain any number of following letters, digits, or underscores. There is no limit on the number of characters in a variable name. Shell variables hold string values, and there is also no limit on the number of characters that they may hold. (The Bourne shell was one of the few early Unix programs to follow a "no arbitrary limits" design principle.) For example:

$ myvar=this_is_a_long_string_that_does_not_mean_much

Assign a value

$ echo $myvar

Print the value

this_is_a_long_string_that_does_not_mean_much

As you can see, variables are assigned values by writing the variable name, immediately followed by an = character, and the new value, without any intervening spaces. Shell variable values are retrieved by prefixing the variable's name with a $ character. Use quotes when assigning a literal value that contains spaces:

first=isaac middle=bashevis last=singer Multiple assignments allowed on one line

fullname="isaac bashevis singer" Use quotes for whitespace in value

oldname=$fullname Quotes not needed to preserve spaces in value

As shown in the previous example, double quotes (discussed later in" Section 7.7) aren't necessary around the value of one variable being used as the new value of a second variable. Using them, though, doesn't hurt either, and is necessary when concatenating variables:

fullname="$first $middle $last" Double quotes required here

Simple Output with echo

We just saw the echo command for printing out the value of myvar, and you've probably used it at the command line. echo's job is to produce output, either for prompting or to generate data for further processing.

The original echo command simply printed its arguments back to standard output, with each one separated from the next by a single space and

Return Main Page Previous Page Next Page

®Online Book Reader