4.4 Command SubstitutionFrom the discussion so far, we've seen two ways of getting values into variables: by assignment statements and by the user supplying them as command-line arguments (positional parameters). There is another way: command substitution , which allows you to use the standard output of a command as if it were the value of a variable. You will soon see how powerful this feature is. The syntax of command substitution is: [11]
$( UNIX command ) The command inside the parenthesis is run, and anything the command writes to standard output is returned as the value of the expression. These constructs can be nested, i.e., the UNIX command can contain command substitutions. Here are some simple examples:
Command substitution, like variable and tilde expansion, is done within double quotes. Therefore, our rule in Chapter 1 and Chapter 3 , about using single quotes for strings unless they contain variables will now be extended: "When in doubt, use single quotes, unless the string contains variables or command substitutions, in which case use double quotes." You will undoubtedly think of many ways to use command substitution as you gain experience with the Korn shell. One that is a bit more complex than those mentioned previously relates to a customization task that we saw in Chapter 3 : personalizing your prompt string. Recall that you can personalize your prompt string by assigning a value to the variable PS1 . If you are on a network of computers, and you use different machines from time to time, you may find it handy to have the name of the machine you're on in your prompt string. Most newer versions of UNIX have the command hostname (1), which prints the network name of the machine you are on to standard output. (If you do not have this command, you may have a similar one like gethostname .) This command enables you to get the machine name into your prompt string by putting a line like this in your .profile or environment file:
PS1="$(hostname) \$ " (The second dollar sign must be preceded by a backslash so that the shell will take it literally.) For example, if your machine had the name coltrane , then this statement would set your prompt string to " coltrane $ ". Command substitution helps us with the solution to the next programming task, which relates to the album database in Task 4-1. Task 4.4
The cut (1) utility is a natural for this task. cut is a data filter: it extracts columns from tabular data. [12] If you supply the numbers of columns you want to extract from the input, cut will print only those columns on the standard output. Columns can be character positions or-relevant in this example-fields that are separated by TAB characters or other delimiters.
Assume that the data table in our task is a file called albums and that it looks like this:
Coltrane, John|Giant Steps|Atlantic|1960|Ja Coltrane, John|Coltrane Jazz|Atlantic|1960|Ja Coltrane, John|My Favorite Things|Atlantic|1961|Ja Coltrane, John|Coltrane Plays the Blues|Atlantic|1961|Ja ... Here is how we would use cut to extract the fourth (year) column:
cut -f4 -d\| albums The -d argument is used to specify the character used as field delimiter ( TAB is the default). The vertical bar must be backslash-escaped so that the shell doesn't try to interpret it as a pipe. From this line of code and the getfield routine, we can easily derive the solution to the task. Assume that the first argument to getfield is the name of the field the user wants to extract. Then the solution is:
fieldname=$1 cut -f$(getfield $fieldname) -d\| albums If we called this script with the argument year , the output would be:
1960 1960 1961 1961 ... Here's another small task that makes use of cut . Task 4.5The command who (1) tells you who is logged in (as well as which terminal they're on and when they logged in). Its output looks like this:
billr console May 22 07:57 fred tty02 May 22 08:31 bob tty04 May 22 08:12 The fields are separated by spaces, not TAB s. Since we need the first field, we can get away with using a space as the field separator in the cut command. (Otherwise we'd have to use the option to cut that uses character columns instead of fields.) To provide a space character as an argument on a command line, you can surround it by quotes:
With the above who output, this command's output would look like this:
billr fred bob This leads directly to a solution to the task. Just type:
$ mail $(who | cut -d The command mail billr fred bob will run and then you can type your message. Here is another task that shows how useful command pipelines can be in command substitution. Task 4.6
This task was inspired by the feature of the VAX/VMS operating system that lets you specify files by date with BEFORE and SINCE parameters. We'll do this in a limited way now and add features in the next chapter. Here is a function that allows you to list all files that were last modified on the date you give as argument. Once again, we choose a function for speed reasons. No pun is intended by the function's name:
function lsd { date=$1 ls -l | grep -i '^.\{41\}$date' | cut -c55- } This function depends on the column layout of the ls -l command. In particular, it depends on dates starting in column 42 and filenames starting in column 55. If this isn't the case in your version of UNIX, you will need to adjust the column numbers. [13]
We use the
grep
search utility to match the date given
as argument (in the form
Mon
DD
, e.g.,
Jan 15
or
Oct 6
, the latter having two spaces) to the output
of
ls -l
. This gives us a long listing of only those files
whose dates match the argument. The
-i
option to
grep
allows you to use all lowercase letters in the month name, while
the rather fancy argument means, "Match any line that contains 41
characters followed by the function argument."
For example, typing
lsd
The output of grep is piped through our ubiquitous friend cut to retrieve the filenames only. The argument to cut tells it to extract characters in column 55 through the end of the line. With command substitution, you can use this function with any command that accepts filename arguments. For example, if you want to print all files in your current directory that were last modified today, and today is January 15th, you could type:
$ lp $(lsd The output of lsd is on multiple lines (one for each filename), but LINEFEED s are legal field separators for the lp command, because the environment variable IFS (see earlier in this chapter) contains LINEFEED by default. |
|