UNIX Hints & Hacks

ContentsIndex

Appendix A: Basic Scripting Concepts

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

Building a Script

 

   

 

Recursive Scripts

 

 

 

 

 

Appendix A
Basic Scripting Concepts

Writing scripts allows a system administrator to manipulate UNIX to perform various tasks and functions and even fix problems. This appendix does not go deeply into all the aspects of writing scripts. You can get a wealth of information from specific shell programming books available on the market today. If you have never seen a shell script before, then this provides you with just enough to get you going to run all the scripts described in this book.

Shell scripts can be as easy to create and they are to execute. Scripts can be set up to execute command that monitor anything from processes to log file. Executed scripts can be set up to execute other scripts. You can even add logic to a script with regular expressions to do various tasks, when certain conditions have been met. You can design a script with a unique new command that consists of a series of UNIX commands that you devised.

Once you have given the script a name, open it up in an editor. You will see that scripts are made up of a several pieces: a defining shell, a series of UNIX commands, and if needed other reserved commands that are available to the shell you have chosen to use. Once the script is written, if you modify the permissions to be executable, the script is ready to run.

Building a Script

Here is a very simple script to start out with. The script is called ll and it merely performs a long listing of all the files in the current directory. Create a file using the editor of your choice (I like vi) with the filename ll.

% vi ll

In the file, insert the two lines. The first line defines the shell that is to be used. This is a pound and a exclamation mark ( #!) followed by the path of the shell that will be used. This can be the path to any shell that the script understands. The second line runs the ls command. Exit the editor when you have typed these two commands.

#! /bin/sh
ls -al

Line 1: Define the name type of the shell to run the script in. If a known definition is defined, then the script runs under the current shell of the person trying to execute the script. This should be defined, because those who execute the script might all have different working shells.

Line 2: This simply does a long listing of the directory that the person executing the script is currently in.

What you currently have is a file with two lines of information in it.

% ls -al ll
-rw-r---r--    1 ugu staff      17 Dec 18 19:30 ll

To turn this normal flat ASCII file into a script, merely change the permissions of the file so the it has executable permissions on it with the chmod command.

% chmod u+x ll
% ls -al ll
-rwxr---r--    1 ugu staff      17 Dec 18 19:32 ll

The u+x option within chmod changes the permissions for the owner of the file to be the only person who can execute the file as a script. For you, it is a script; for everyone else, it is just a regular text file.

To run your script, enter the name at a shell prompt. In this case the script is called ll.

% ./ll
-rwxr--xr-x 1 ugu staff 228 Sep 12 09:10 ffind* -rwxr---r-- 1 ugu staff 17 Dec 18 19:32 ll* -rw-r--r-- 1 ugu staff 7198 Nov 13 12:55 top.html drwxr--xr-x 1 ugu staff 512 Dec 6 11:12 perl/ -rw-r---r-- 1 ugu staff 2354 Oct 14 19:47 pinger

The results of the ll script should output a listing of the current directory to the display. The period-slash ( ./) prior to the command tells UNIX not to look anywhere else but in this current directory. If the period-slash were not there, the system would first check the aliases table for any possible aliases, and then check for files called ll in the paths defined in your list of environment variables.

You can and use this script like a command and pipe it through to other commands. If you want to see a long list of files in the current directory that were last modified in December, pipe the ll command through to grep.

% ll | grep "Dec"
-rwxr---r--    1 ugu staff      17 Dec 18 19:32 ll*
drwxr--xr-x    1 ugu staff     512 Dec  6 11:12 perl/

If you want to get a count of the number of files in the directory, you can pipe the ll command script through to wc.

% ll | wc -l
     5

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.