UNIX Hints & Hacks

ContentsIndex

Appendix A: Basic Scripting Concepts

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

Building a Script

 

   

 

Recursive Scripts

 

 

 

 

 

Recursive Scripts

You have just seen how a script not only executes UNIX commands, but also can be turned into a command itself. Suppose you created a script that monitored the system with a few UNIX commands.

% vi monsys

#! /bin/sh
hostname
tail -10  /var/adm/messages
last root | head -3
ls -al /etc/passwd
ps -ef | grep sendmail

Line 1: Define the shell

Line 2: Get the hostname

Line 3: Show the last three lines of the messages log

Line 4: Show the last three people who logged in as root

Line 5: Display time stamp on the password file

Line 6: Check the process table and display the sendmail process

Change the permissions on the ASCII file to executable, so that it can be run and the commands can be processed.

% chmod u+x monsys

Once this is done the script can be executed to display some critical areas that can be monitored.

rocket
Jan 18 18:39:19 6B:rocket Xsession: mike: login Jan 18 18:39:20 6B:rocket access control disabled, clients can connect from any host Jan 18 19:44:53 3D:rocket automount[185]: pluto: exports: Port mapper failure
root ttyq0 pluto Mon Jan 18 19:44 - 19:45 (00:00) -rw-r--r-- 1 root sys 1779 Dec 24 10:37 /etc/passwd
root 421 1 0 Jan 16 ? 0:34 /usr/lib/sendmail -bd -q15m root 822 816 1 20:25:51 pts/0 0:00 grep sendmail

A security administrator comes along and asks you for a monitoring tool that keeps an eye on the root login attempts into the system and if the password changes. Because you know your script already outputs this information, you can call the monsys script from another script.

% vi securinfo
#! /bin/sh monsys | egrep '(^root|passwd)

Line 1: Define the shell to be used.

Line 2: Execute monsys, and only display when root logged into the system and when the password file was last changed.

% chmod u+x securinfo

After the permissions have been changed, executing the securinfo script, in turn, executes the monsys script and you get the information you are looking for.

% securinfo
root ttyq0     pluto            Mon Jan 18 19:44 - 19:45  (00:00)
-rw-r--r--    1 root     sys        1779 Dec 24 10:37 /etc/passwd

This gets you started writing basic scripts and allows you to have some understanding as to what you are doing when you start entering all the little hints and hacking scripts that are provided in this book.

1.Create a file in an editor.

2.Define the shell to be used.

3.Build the script with UNIX commands functions and reserved commands for that particular shell.

4.Exit the shell.

5.Change the file permissions to make it executable.

6.Execute the new script.

UNIX Hints & Hacks

ContentsIndex

Appendix A: Basic Scripting Concepts

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

Building a Script

 

   

 

Recursive Scripts

 

 

 

 

 

© Copyright Macmillan USA. All rights reserved.