|
The set CommandThe shell's set command is a dual-purpose command: it's used both to set various shell options as well as to reassign the positional parameters $1, $2, and so forth. The -x OptionThis option turns on trace mode in the shell. It does to the current shell what the command
did for the execution of the ctype program in Chapter 8, "Decisions, Decisions." From the point that the
command is executed, all subsequently executed commands will be printed to standard error by the shell, after filename, variable, and command substitution and I/O redirection have been performed. The traced commands are preceded by plus signs.
You can turn off trace mode at any time simply by executing set with the +x option:
You should note that the trace option is not passed down to subshells. But you can trace a subshell's execution either by running the shell with the -x option followed by the name of the program to be executed, as in
or you can insert a set -x command inside the file itself. In fact, you can insert any number of set -x and set +x commands inside your program to turn trace mode on and off as desired. set with No ArgumentsIf you don't give any arguments to set, you'll get an alphabetized list of all the variables that exist in your environment, be they local or exported:
Using set to Reassign Positional ParametersThere is no way to directly assign a value to a positional parameter; for example,
does not work. These parameters are initially set on execution of the shell program. The only way they may be changed is with the shift or the set commands. If words are given as arguments to set on the command line, those words will be assigned to the positional parameters $1, $2, and so forth. The previous values stored in the positional parameters will be lost forever. So
assigns a to $1, b to $2, and c to $3. $# also gets set to 3.
So after execution of the set, everything seems to work consistently: $#, $*, and the for loop without a list. set is often used in this fashion to "parse" data read from a file or the terminal. Here's a program called words that counts the number of words typed on a line (using the shell's definition of a "word"):
The program stores the line read in the shell variable line and then executes the command
This causes each word stored in line to be assigned to the positional parameters. The variable $# is also set to the number of words assigned, which is the number of words on the line. The -- OptionTry typing in a line to words that begins with a - and see what happens:
After the line was read and assigned to line, the command
was executed. After the shell did its substitution, the command line looked like this:
When set executed, it saw the - and thought that an option was being selected, thus explaining the error message. Another problem with words occurs if you give it a line consisting entirely of whitespace characters, or if the line is null:
To protect against both of these problems occurring, you can use the -- option to set. This tells set not to interpret any subsequent arguments on the command line as options. It also prevents set from displaying all your variables if no other arguments follow, as was the case when you typed a null line. So the set command in words should be changed to read
With the addition of a while loop and some integer arithmetic, the words program can be easily modified to count the total number of words on standard input, giving you your own version of wc -w:
After each line is read, the set command is executed to take advantage of the fact that $# will be assigned the number of words on the line. The -- option is supplied to set just in case any of the lines read begins with a - or consists entirely of whitespace characters. The value of $# is then added into the variable count, and the next line is read. When the loop is exited, the value of count is displayed. This represents the total number of words read.
(Our version is a lot slower than wc because the latter is written in C.) Here's a quick way to count the number of files in your directory:[1]
This is much faster than
because the first method uses only shell built-in commands. In general, your shell programs run much faster if you try to get as much done as you can using the shell's built-in commands. Other Options to setset accepts several other options, each of them enabled by preceding the option with a -, and disabled by preceding it with a +. The -x option that we have described here is perhaps the most commonly used. Others are summarized in Table A.9 in Appendix A. |
|
No comments:
Post a Comment