Concept #3 - scripts:

 

For anybody familiar with writing a batch file under DOS, you are already familiar with writing scripts. The most basic of scripts is just a collection of commands that you could have typed in on the command line. There are many reasons to put these "simple" commands into a script:

 

  1. although the commands are simple, they may be organized in a unique or clever way
  2. a script will always work the same way - things can’t be skipped, forgotten or mis-typed
  3. scripts can be reviewed to see what they did

 

One of the topics that comes up when discussing scripts is the difference between a "script" and a "program." Writing a script is a form of programming, but what happens after the writing is finished is how I differentiate between a "script" and a "program." A script is ready to run as soon as you finish writing it, while a program requires additional steps (compiling, linking, etc.) before there is something that can be run. This has a number of benefits:

    1. writing a script is faster, because time is not spent compiling an executable
    2. if you have a script, you have the "source" code - if you have an executable, you may or may not have the source and therefore may not be able to modify it

 

The first two characters of a script should be the magic cookie "#!". A magic cookie is one of the ways that a UNIX system knows what to do with a file / knows what kind of a file it is. There is a file named /etc/magic where the known identifiers are stored. If you have ever used the UNIX file command, then you have consulted the magic file. For those who have never used it, try the command "file /bin/sh." This should tell you that /bin/sh is an executable, which means that normal humans cannot read the contents of that file: it has been compiled into a format that only the computer can understand. Next try "file /usr/sbin/dfspace." The answer should be "commands text" which means that if you look at the contents of the file you should be able to read it (you may not understand what you read, but that is different :)

 

If the first line of a file is #!/bin/sh this tells the computer two things: first, it tells the computer that this is a script (commands, text) and second, the "/bin/sh" tells it what types of commands - Bourne shell commands. When you log into a UNIX machine, you are running a shell - that is what understands what to do when you type in "cd .." or any other command. There are dozens of shells available, all with their own differences. I prefer to write Bourne shell scripts, because it is standard on every UNIX box, and is the basis for most every other shell.

 

From a programming "style" standpoint, I like the next (second) line of every script to be a comment, containing the name of the file. A comment line starts with an octothorpe (pound sign, #). This comment helps you find the original file if you ever print it out, and cannot remember where it came from.

 

The simplest type of script contains a list of commands, just as they would have been typed in at the command prompt. Here’s an example (type this into a file, then change the permissions on that file to make it executable, and you have a script):

 

#!/bin/sh

#startall.sh

cd /omega/limbs/bin

./StartOrder &

./StartSales &

./StartBilling &

 

To make this script a little more flexible, we can use a variable to store the location of the files. A variable is just a place to store something. To store a value into a variable, just use variable=value (no spaces around the equal sign and no dollar sign in front of the variable name). To use the variable, you place a dollar sign in front of the variable name.

 

#!/bin/sh

#startall.sh

DIR=/omega/limbs/bin

$DIR/StartOrder &

$DIR/StartSales &

$DIR/StartBilling &

 

Why would we want to do something like this? There are times when we need to run a script like this in more than one place, such as LIMBSBK1 and on the presentation machines. The start scripts are located in /limbsbk1/limbs/scripts on LIMBSBK1 and /omega/limbs/bin on the presentation machines. To make this script work in both environments, we need to add a conditional statement. A conditional is a way to make a decision: If something is true then do one thing, else it is false, do a second thing. The one part of an if statement that does not make obvious sense is that it needs to end with fi (which is just a backward if).

 

#!/bin/sh

#startall.sh

if [ `/usr/bin/uname` -eq "limbsbk1" ]; then

DIR=/limbsbk1/limbs/scripts

else

DIR=/omega/limbs/bin

fi

$DIR/StartOrder &

$DIR/StartSales &

$DIR/StartBilling &

 

Note that the spaces around the square brackets are required. More about the comparisons that can be used between the square brackets ("-eq" means the two strings are equal) can be found in the Nutshell book, in the section under "Bourne and Korn" shells, under the command "test." This version of the script also includes one other new concept -- that of quotes. The back quotes (`) (around /usr/bin/uname) mean "execute the command in the quotes and put the answer in place of the command and the quotes." The quotes around the limbsbk1 could be either single quotes (‘) or double quotes (") in this situation. Double quotes will cause variable substitution while single quotes will not. Variable substitution means just that: replace the variable with its value. On limbsbk1 the statement echo "$DIR" would print /limbsbk1/limbs/scripts while echo ‘$DIR’ will print $DIR (the echo command is a way to print output to the screen.)

 

This script does the same thing three times, starting three programs. Doing the same kind of things multiple times is the perfect time to use a loop. Each time through the loop you do the same thing, but with a different value stored in a variable. In this script, there are three different program names. To use a loop, one of these names would get placed in a variable each time through the loop, and that variable would be used to execute the program.

 

#!/bin/sh

#startall.sh

if [ `/usr/bin/uname` -eq "limbsbk1" ]; then

DIR=/limbsbk1/limbs/scripts

else

DIR=/omega/limbs/bin

fi

for program in StartOrder StartSales StartBilling

do

$DIR/$program &

done

 

The new parts of the script are the line with the format for variable in item1 item2 item3 and the words do and done. The words "item1 item2 item3" make up a list containing three items. The things between the do and done will be executed as many times as there are items in the list, and each time through the loop the variable will contain a different item from the list.

PREV UNIX part 2       NEXT UNIX part 4