[olug] Fw: Saving code snippets

Mike Peterson mpeterson at mail.charlesfurniture.com
Thu Jul 3 14:55:40 UTC 2003


This looked like an appropriate informational topic to forward to the group.
I trimmed all the Sponsor advertising from the message.
Let me know what you think.

> 
> UNIX IN THE ENTERPRISE --- July 03, 2003
> Published by ITworld.com -- changing the way you view IT
> http://www.itworld.com/newsletters 
> Saving code snippets
> By Sandra Henry-Stocker
> 
> One of the little habits that has saved me considerable time over the
> years is my predilection for saving snippets of code in a directory that
> I name howto or HowTo.  Whenever I'm writing a new script to tame some
> part of my busy servers, this directory serves as my own personal
> "scripting by example" directory.  The obscure commands that I find
> myself using no more frequently than every few years seem less
> intimidating once I've captured some particularly helpful examples of
> their use in a file.  When someone comes to my office and asks if I know
> how to write a script that will ftp a file without requiring a user to
> be present to type a password, I can show them a simple, clear example. 
> When I need a script that I am writing to prompt a user for a number
> which must have exactly four digits, I don't have to rediscover how this
> is done.
> 
> Code snippets aren't just a good idea.  Code snippets are a great idea. 
> Of the people who come to my office or send me email asking how one can
> best solve a particular scripting problem, almost all of them want not
> an explanation of how a command works, but an example.  Maintaining my
> own set of examples saves me time, makes me look smart, and takes no
> more effort than an occasional cut and paste.
> 
> Take this example of an in-place edit in Perl.  Instead of using cat to
> pipe a file to a sed command, and then send the output to a temporary
> file which is then copied back to the original file name, we can make
> the change in a single command.  The Perl snippet to do this looks like
> this:
> 
> perl -i -p -e 's/whatever\s+yes/whatever\tno/' $file
> 
> This particular command is looking for a string which contains the word
> "whatever" followed by some white space and a "yes".  It turns it into a
> string which contains the word "whatever" followed by some white space
> and the word "no".  Fast and fairly readable, this snippet avoids any
> issues we might run into with constructing temporary file names and
> looks very tidy.
> 
> Another useful snippet checks a number entered by a user to be sure that
> he has entered the proper number of digits.  This snippet is a Bourne
> shell loop that prompts for a 4-digit number until the user gives in and
> enters one:
> 
> NUM=0
> 
> while [ `expr $NUM : '^[0-9]*$'` != 4 ]
> do
>     echo "Please enter a 4-digit number> \c"
>     read NUM
> done
> 
> While I may not remember off the top of my head how to use the expr
> command to test the length of a matched expression, this little example
> is all I need to avoid figuring out the solution for the fifteenth time.
> 
> Next, here is an example unmanned ftp:
> 
> ftp -n -v << EOF > .ftplog
> open $ftpServer
> user $user $password
> ls
> ascii
> put $file $newname
> quit
> EOF
> 
> While inserting a password into a script is a very poor idea for
> security reasons, simple scripts like this sometimes come in very handy
> and an example of how this is done is a good thing to keep around.  By
> the way, we will look at a considerably more secure way to do unmanned
> file transfers in another week or two.
> 
> Finally, the command to renice a process might be easy enough to
> construct by reading the man page, but what is easier than an example of
> this sort?:
> 
> # lower a process priority by 20.
> renice -n 20 $pid
> 
> Scripting snippets are not just great time savers; they also serve to
> add some regularity to my scripting style.  While I might easily solve
> the same scripting problem in half a dozen different ways, anyone who
> later wants to make sense of my scripts would probably benefit from
> seeing the same basic syntax used from one script to the next.
> 
> Care to share your best snippets for a follow-on column on "Favorite
> snippets"?  Please sent them to mailto:sstocker at itworld.com.
> 
> 
> About the author(s)
> -------------------
> Sandra Henry-Stocker has been administering Unix systems for nearly 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 mailto:sstocker at itworld.com.
> 
> NEWSLETTER FAQS
> For commonly asked newsletter questions, go to: 
> http://www.itworld.com/response/site_support.html
> 
> 



More information about the OLUG mailing list