Unless otherwise noted, articles © 2005-2008 Doug Spencer, SecurityBulletins.com. Linking to articles is welcomed. Articles on this site are general information and are NOT GUARANTEED to work for your specific needs. I offer paid professional consulting services and will be happy to develop custom solutions for your specific needs. View the consulting page for more information.


Learn Scripting

From SecurityBulletins.com

Jump to: navigation, search

Learn scripting and make your life easier

Effectively using scripting can turn work that once took hours or days to complete into jobs that take minutes. Scripting is more consistent than manual labor for repetitive tasks. To effectively write scripts requires a bit of creativity and insight into the operating system and the language you use. The items in this article are geared toward Linux and Unix administration. Windows NT administrators are more limited in functionality, but can use Perl or Python to reap some of the benefits.

Let's start with an example that may come up if you are using Samba to serve files to Windows users in the company. A manager asks if you could archive all Word documents in a directory tree that contain the phrase "Customer Proposal" and that remain unchanged for at least 30 days.

If you didn't know better, you might start up Open Office, and look through the 8,000 documents individually for the phrase, check the date, keep track of the list of files, and archive each of them individually. Being the enlightened Linux Administrator, you know a better way. You download and install AntiWord (available at http://www.winfield.demon.nl/ ) and simply whip up the following:

find /share/WordDocs -type f -name '*.doc' -ctime +30 -exec sh -c 'antiword {} \ 
| grep "Customer Proposal" >/dev/null && mv {} /archive' \; -print 2>/dev/null > ~/MovedDocs.txt

This one liner accomplishes quite a bit. First, it begins finding files in /share/WordDocs. The options serve to restrict which files are returned. The -type f option tells find you are only interested in files and to not return directory names. -name '*.doc' tells it the filename must end in .doc. -ctime +30 returns only files changed thirty or more days ago.

When all those conditions are true, the -exec option starts up a shell. The shell runs AntiWord with the full pathname and filename substituted for {}. The output of AntiWord is piped to grep, where "Customer Proposal" is searched for. We discard the output from grep, since we're only interested in the true or false result from it.

If the required phrase is found, the first part of the logical AND (&&) condition is satisfied and the move command is allowed to run with the path to the file substituted for {}. This results in a move command similar to the following:

mv /share/WordDocs/cust1/propsal32.doc /archive

We then tell find to print out the name and path of the file that was just moved. Error output is discarded with 2>/dev/null. The list of moved files is saved to MovedDocs.txt, located in our home directory.

This is just one of many examples where scripting is useful. System administration is full of mundane tasks that can be automated. It may take a while to get good at scripting, but the dividends it pays are enormous. It becomes even more enjoyable when you learn efficient methods of scripting, such as the one liner presented in this article. A script doesn't have to be several pages long to be useful or impressive.


Copyright (c) 2000 by Doug Spencer spencer@securitybulletins.com


About the author

Doug Spencer has been highly involved in developing certification exams for BrainBench ( http://www.brainbench.com ) . He has received top scores in Linux Administration and Internet Security, and is a member of the BrainBench Most Valuable Professional teams for Linux Administration and Internet Security.

Doug is currently a consultant who regularly uses his skill in Linux, Unix, scripting, and computer security to finish projects before their deadlines.

Personal tools