Peter Radatti
Background Background Experience Ventures Published Works Patents

The Sun Observer, Volume 8 No.9

Peter V. Radatti

radatti@cyber.com

CyberSoft

September 10 1995

Notice: Copyright July 28, 1995 by Peter V. Radatti, All rights reserved.

Welcome to the fifth in the series of this column. I hope you are enjoying reading it as much as I am writing it. As you may remember from last month, my editor promised me an all expense paid cruse aboard a luxury liner. Once again I found myself looking for the hidden hook. Perhaps I will be staying in the boiler room? No, this time my Editor was true. I enjoyed an all expense paid cruse aboard a luxury liner. The cruse started in the Bronx, NY and sailed down the coast line to the Delaware River where it turned inland to our destination of Condemn, NJ. I didn't know it at the time but of course the cruse was during the storm season. Visibility was low but luckily the residents of Condemn tend to regularly perform their own urban renewal by setting fire to their city so there was no problem finding our destination. Unfortunately, no one would depart at Condemn so we had to pay a high fee to be brought back to the Bronx where we felt safe.

You may remember last week that I promised more tutorials on writing scripts. The ability to write scripts is one of the most important skills you can have as a Unix user. It allows you to make use of the power of the system without learning a formal programming language like "C". Due to this importance we will be concentrating on this subject and expanding your ability to write these scripts over the next few articles. One of the most important abilities a script must have in order to be versatile is the ability to perform boolean logical decisions. The two types of decisions that we will be covering this month are if-then-else and case structures. They are both related to each other and can be substituted for each other but each specialized in a different type of function.

An if-then-else structure can also be used as just an if-then without the else. The structure always starts with an "if" statement followed by a decision equation in brackets closed by a "then" statement. You can read the statement as "if something is true" then "do something". A real life example is if the traffic light is red then stop the car. If traffic lights only had two modes such as red and green then an if-then-else structure could process all of the possible modes. It would not be necessary to know if the light was green only if it was not red since the light has to be green if it is not red. This statement can be read as "if (the light is red) then stop the car else press on the gas pedal. Here is a small example program that will help you to understand this structure. The program will tell you if there are any print jobs of a specified name in the default print queue. You can modify the program to eliminate the print queue status and use it for anything. Type in the program then execute it from the command line giving the word you want to scan for as a command parameter. (Example: myprogram radatti) There is one part of this program that we have not already studied. That is the "$status" variable. This is a variable that is set by the operating system itself. It always has a value of true or false and is the result of the preceding command. If the command prior to the command in which the "$status" variable is being used executed correctly then the variable is set to false. The most common purpose of this variable is to intercept failures in scripts for branch processing. If my script depends upon a file being in a certain place and it's not there then I can use the "$status" variable to determine this and terminate the script.

#/bin/csh -f
set symb1 = "$1"
lpq |grep -s "$1"
if ($status) then
echo "No, $1 is not in the queue."
else
echo "Yes, $1 is in the queue."
endif
echo "Have a happy and productive week."

All of this has been instructive but there is still a problem. The problem with out example is that stop lights have a minimum of three modes, red, yellow and green. Some even have additional modes such as green arrows. We could next many if-then-else structures together to solve this problem but there is a simpler way. The case structure performs this task. The case structure can be expanded as much as we need in order to process all possible results. The structure starts with a test called a switch. This switch contains an equation or variable that provides the result that is evaluated. A case structure can be read as "switch upon the final status of the traffic light. If the light is green then go. If the light is yellow then go faster. If the light is red then stop. Here is an example of a case statement in a small test program. Enter the program into your computer and execute it. I am sure that you will understand what is happening very quickly.

#/bin/csh -f
echo "enter a test letter (a,b)"
set tst = $<
switch($tst)
case a:
echo "hello I am at label a"
breaksw
case b:
echo "hello I am at label b"
breaksw
default:
echo "hello I am at the default"
endsw

Next month we are going to explore a program called show and print that will tie many of the commands that you have learned together. It's also a fun little utility that you can use to impress your neighbors but otherwise doesn't do much. In addition, next month I get to visit NORAD where our nation tracks all of the junk in outer space. I bet there are some old mainframes up there just waiting for reentry.

Pete Radatti is the founder and CEO of CyberSoft, Inc. CyberSoft manufactures, VFind the an antivirus software product that executes under Unix and simultaneously scans for Unix, MS-DOS, Macintosh and Amiga destructive software. You can reach Pete at radatti@cyber.com. Thanks for all the nice letters!

View more of Pete's Published Works

Background Background Experience Ventures Published Works Patents



HomeNewsBackgroundAccomplishmentsAwardsVenturesPublished WorksPatentsGuestbook

Peter V. Radatti CEO CyberSoft, Inc 1958 Butler Pike, Suite 100 Conshohocken, PA
Voice: 610-825-4748 Fax: 610-825-6785 E-mail: radatti@cyber.com
Copyright 2004, 2005, 2006 Peter V. Radatti. All rights reserved.
PVR