open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com

Unix Tip: Adding lines to the ends of files

ITworld.com 12/12/2006

Sandra Henry-Stocker, ITworld.com

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?

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Sun Microsystems' - FREE 60 DAY TRIAL OFFER!
Test Sun's Newest Servers BEFORE YOU BUY. Plug Them In With Access To Full Technical Support.
100% Web Based Help Desk Software
Easy to use, customizable to meet your needs, powerful and scalable. Free online demo. Try it today!
Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Used and Refurbished HP ProCurve Switches
Lifetime Warranties, Professional Testing & Shipping on all HP Equipment Purchases!
» Buy a link now

Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
 Home   Open source  Operating systems  Unix
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.