185
■ ■ ■
CHAPTER 28
Free-Format Output Using cat
T
here are many ways to send script output to the screen or to a file. The technique dem-
onstrated in this chapter for creating preformatted output is simple to code. Many times I
have coded scripts that write entries to a log, create a configuration file, or generate for-
matted usage output one line at a time by redirecting the output of echo statements. This
works perfectly well, but the code looks a bit ugly and becomes tedious; it is also laborious
to make changes because each echo statement has to be formatted individually.
The original version of the gold Linux build script in Chapter 38 was written in that way.
It created a temporary file used to partition a hard disk by outputting each partition table
entry individually. The script has since been updated to use the technique in this chapter.
Here are some sample lines of the original code:
echo "# partition table of /dev/hda" > $PARTTAB
echo "unit: sectors" >> $PARTTAB
echo >> $PARTTAB
echo "/dev/hda3 : start=0,size=0,Id=0" >> $PARTTAB
echo "/dev/hda4 : start=0,size=0,Id=0" >> $PARTTAB
Note that if the partition file already exists, the first command overwrites the file using
a single greater-than symbol (>), and if the file does not exist, the file is simply created. All
subsequent echo statements append their output to the file. With this method, there is the
danger of using the incorrect redirect (overwrite > or append >>). As an example, if the last
line in the four lines of the code here had a single redirect > instead of a double redirect >>,
the output of all the lines previous to the error would be eliminated. Troubleshooting this
error is somewhat difficult since both versions work, are very similar in appearance, and
differ by only a single character.
In contrast, the technique described in this chapter uses the cat utility in a form that is
not necessarily intuitive. Instead of echoing lines one at a time, you create the formatted
text all at once and then output it to the screen directly using a here-document. Alterna-
tively, you can redirect it to an output file either in append or overwrite mode.
This method uses double input-redirect characters (<<), followed by a unique
delimiter, in this case SOMETAG, but it could be anything and does not matter if it is all
uppercase. The delimiter immediately precedes the preformatted text to mark its start.
On the line following the last line of the text, a matching end delimiter is issued to close
186
CHAPTER 28
■
FREE-FORMAT OUTPUT USING CAT
the here-document. With this technique, you can create free-format output of as many
lines as you like in the exact format you want. Once the here-document is closed, the
output stream is terminated and the command completes.
This code is functionally the same as the first code segment in this chapter, except that
the code has been modified to use cat instead of echo:
cat > $PARTTAB <<SOMETAG
# partition table of /dev/hda
unit: sectors
/dev/hda3 : start=0,size=0,Id=0
/dev/hda4 : start=0,size=0,Id=0
SOMETAG
This example is much cleaner and easier to read, since nearly all the echo statements,
quotes, and redirection syntax have been removed. Also, commented (#), blank, and
quoted lines can be entered without further issues arising, as they won’t be evaluated by
the shell.
Some characters may need to be escaped if you want them to be included in your out-
put. The dollar sign ($) and the back-tick or back-quote (`) need to be escaped with a
backslash (\) because otherwise the shell will evaluate them and attempt to use them with
their normal meanings.
This slight modification of the here-document adds the hyphen (-) following the initial
redirection:
cat > $PARTTAB <<-SOMETAG
# partition table of /dev/hda
unit: sectors
/dev/hda3 : start=0,size=0,Id=0
/dev/hda4 : start=0,size=0,Id=0
SOMETAG
This strips any leading tab characters from the preformatted text, allowing the inden-
tation of the code to look more readable and be viewed in more of a code block.
You can see a few examples of this technique at work in this book. You can find the
here-document technique in Chapter 22, where a default user-configuration file is cre-
ated if one doesn’t already exist. Additionally, as I already mentioned, Chapter 38 details
the partition layout file to build a Linux gold system.
The method of opening a file handle for arbitrary input as a replacement for a file can
be used for more than just output using cat. Any utility that you might redirect a file into
can use this technique. Some examples can be found in Chapters 10 and 25, where the
need for a temporary file is removed.