Send in your Unix questions today! |
See additional Unix tips and tricks
Let's look at a quick, clever way to add lines to the ends of files on a Unix system. This little Unix trick can be very useful if you are distributing installation directions to customers and want to limit the possible mistakes that they can make in updating important system files. The basis of this trick is the Unix here document -- a special form of I/O redirection that allows you to insert the content to be added between the redirect command and a special marker that is recognized as the end marker for the inserted text.
One useful application of the technique I am about to describes is to add lines to the /etc/system file on an Oracle server. These lines, required to increase the capacity of embedded messaging options in the Solaris kernel, are typically typed or cut and pasted into the end of the file.
The command:
set shmsys:shminfo_shmmax = 4294967295
for example, extends the amount of shared memory available on database serves. While you can depend on a knowledgeable sysadmin or Oracle DBA to update the /etc/system file correctly, how much easier and more reliable is it to make it possible for that person to add the required lines into the file using a prepared command or script?
The key to using the here document approach to updating files is a command that looks like this:
cat <<! >> /etc/system
This command is using input redirection (<<) to read the subsequent lines and output redirection (>>) to append those lines to the end of the /etc/system file. The exclamation point (!) is included to indicate that it is the input terminator. In other words, the cat command instructs the shell to read all of the text on the following lines until it encounters another exclamation point.
Follow this command by all of the text you want to add to the /etc/system file or to whatever file you have specified with the cat command and then by the terminator on a line by itself and you will have a command that adds all of the specified lines to the specified file.
Here's an example showing Oracle kernel parameters being added to /etc/system.
cat <<! >> /etc/system
* Oracle kernel values
set shmsys:shminfo_shmmax=4294967295
set shmsys:shminfo_shmmin=1
*set shmsys:shminfo_shmmni=100
set shmsys:shminfo_shmseg=200
*set semsys:seminfo_semmns=1024
*set semsys:seminfo_semmni=200
*set semsys:seminfo_semmsl=256
set semsys:seminfo_semopm=100
set semsys:seminfo_semvmx=32767
* Override values
set semsys:seminfo_semmns=4006400
set semsys:seminfo_semume=10
set semsys:seminfo_semmnu=4006400
set semsys:seminfo_semmni=8244
set semsys:seminfo_semmsl=480
set shmsys:shminfo_shmmni=1560
set msgsys:msginfo_msgmni=7480
set msgsys:msginfo_msgmap=100
set msgsys:msginfo_msgmax=2048
set msgsys:msginfo_msgmnb=4096
set msgsys:msginfo_msgssz=8
set msgsys:msginfo_msgtql=1024
set msgsys:msginfo_msgseg=2200
!
The terminator for a here document doesn't have to be an exclamation point, of course. Many sysadmins like to use EOF for "end of file" or EOT for "end of text", but the choice is arbitrary. Just be sure that the terminator in your cat command and your final line match. And, while you certainly could add the same text to the same file using a series of commands such as those shown below, look how much easier it is to construct the commands when you don't have to worry about whether the asterisks are going to be interpreted or that each of your lines of text begins and ends in a double quote.
echo >> /etc/system
echo " * Oracle kernel values" >> /etc/system
echo " set shmsys:shminfo_shmmax=4294967295" >> /etc/system
echo " set shmsys:shminfo_shmmin=1" >> /etc/system
...
Inserting the text in a here document of this sort is as simple as pasting it between these two lines:
cat <<! >> /etc/system
!
If you send your customers a command like that shown above, they can cut and paste the entire set of lines, beginning with the cat command and ending with the exclamation point on a line by itself, into their shell windows or, better yet, you can give them a script to run that contains the prepared here document. In either case, you've made an easy job out of what could have been an exercise of tedious typing as well as discovered an easy way to distribute file updates. How easy can it get?