An Introduction to unix Scripts

unix scripts are also know as shell programs. A script is a file made up of shell commands. Such files are also known as batch files in some systems. You put the commands you want executed into the file, then give the file name to execute the commands in order -- as if you had typed them in from the system prompt.

This page will have a few hints and, I hope, enough information to get you started. The best thing to do is to try making some simple scripts to get a feel for this kind of work, then look at examples of scripts that do work to get more ideas of what you can do.

I don't pretend to be an expert in unix and/or c. I have done a lot of c programming, but don't do it regularly and need to refresh my memory when things come up that I have not worked on for a while. If you ask a question and I don't know the answer right away, I will find it (or at least help you find it.)

Flavors of unix

There are variations on unix and several different shells are usually available on a given system. If you see an example that works, and it does not work for you, you may be using a different shell. The following table is taken from the book unix shell programming by Lowell Jay Arthur. 2nd edition. John Wiley Publisher.
Function Bourne Korn c
Availability most least Berkeley unix, some System V
Variables local
global(export)
local
global(export)
local (set)
global (setenv)
Control if-then-else-fi if-then-else-fi if-then-else-end-if
Commands case-esac

for-do-done
xargs
while-do-done
until-do-done
case-esac
select
for-do-done
xargs
while-do-done
until-do-done
switch-case-endsw

foreach-done
repeat
while-end

Conditional Evaluation test, expr direct direct
Interactive
history history
Aliasing functions functions alias
Signals trap trap
Efficiency fast medium medium

The unix man page on the c shell contains a lot of information on commands available in the c shell.

The sed command is the stream editor. It allows modification of a string of text. For example,

sed -e "s/htm/html/g" mypage.html>newpage.html
substitutes html for each htm in the file mypage.html and puts the result into a file named newpage.html. The delimiter / can be changed to any other character you wish. The g indicates global substitution.

Look at the script /cgi-bin/cgi-bin/gentest and /cgi-bin/cgi-bin/gen.pl for fairly simple examples of script files at work.

-------------More later ----------------


[ Villanova Homepage | CSC Homepage | CSC Faculty ]
Lillian N. (Boots) Cassel
Department of Computing Sciences
Villanova University
Villanova, PA 19085-1699
+1 610 519 7341

cassel@vill.edu