The Sun Observer, Volume 8 No.8
Peter V. Radatti
radatti@cyber.com
CyberSoft
August 10 1995
Notice: Copyright June 25, 1995 by Peter V. Radatti, All rights reserved.
Welcome back to the fourth in this series of articles. You may remember in my continuing adventures that the Editor was going to send me to Hollywood to see the stars! Dreams of famous actors and actresses danced in my head. I arrived in Hollywood and was escorted directly to the posh hotel where the Sun Observer Cast and Crew post production party had already started! The "Sun Observer, The Movie", will be hitting theaters everywhere in December. Look for it! Oh yes, I did break into the movies, but they caught me and I had to buy a ticket!
Last month you learned about command parameters, the "find" command and scripts. This month we will continue learning about scripts. There are basically three command script languages. The are, bourne (pronounced born), c-shell (written csh), and korn (pronounced corn). The bourne shell runs on every Unix system. It is used extensively by the operating system itself. The csh was made popular because it had many nice features and it was the standard user shell on many systems including systems made by Sun Microsystems. The korn shell is new and is now being delivered on many systems. The korn shell is gaining ground but the csh is still the most popular. This article will therefore be written for the csh.
The system needs to know which script language a program is written in prior to executing it. The way the system knows is that you tell it. I always reserve the first line of a script program to tell the computer that the program is written in csh. The way I do this is by insuring that the first character of the first line is a hash mark, "#". In addition, just to insure that there is no mistake I fully define that the csh is to be used and that the hash mark is not intended as just a commend. I do this by always making my first line read, "#/bin/csh -f". I suggest that you also follow this convention. Normally many scripts run without any human intervention, however sometimes it's necessary to have the program ask questions and receive answers. The csh language provides for this need using the "echo" and "set" commands. The echo command can be used to display the question followed by the set command to read the answer. I can even get fancy and tell the echo command not to print a carriage return and line feed after printing the questions. This is done by giving the "echo" command the "-n" option. Assume that we need to print lots of files where I need to view the filenames available for print, select one then print it. This is a good job for a script program. First I would use the "ls" command to display the files contained in the current working directory, ask the user to select one for printing, read in their answer then print the file followed by showing it in the print queue. A program to do this is shown in figure 1.
Figure 1
#/bin/csh -f # we are using the csh shell clear # clear the screen ls # display the local filenames echo -n "Select a file: " # ask the user for a file to print set fn = $< # get the users answer and store it in "fn" l pr -Pprinter1 $fn # print the file on the printer named printer1 lpq -Pprinter1 # show the user the queue for printer1
The program shown in figure 1 was easy to write and simple to use. Lets take a look at a script program that CyberSoft uses every day to solve a complex problem. CyberSoft manufactures an anti-virus product for Unix. It is necessary to send customers files and updates and many of CyberSoft's customers have Internet access with email. Since Internet email is very fast and inexpensive compared to overnight couriers the desire exists for the customer and for CyberSoft to provide this service. The problem is that files can not be sent using email unless they are processed to allow it. This is a perfect job for a script program. There are a lot of steps involved and very little human action. In fact this program is very much like the script shown in figure 1. The user selects a file and the program processes it. In this case the program is more complex because we need to enter a filename, the Internet email address of the person to receive the program, record that the file was sent in a database and modify the email message to give the receiver instructions on how to convert the email message back into the original file. Finally, since this script may be run by other scripts we need to be able to accept this input as command parameters or ask the user for it, if not supplied on the command line.
The program starts off telling the system that it is a csh script. It removes a scratch file then displays some messages to the screen. The next step is to read the command parameters. If no command parameters were given then the variables used to read them will be empty. To determine if the variables are empty so we can ask the user for the information. We can use a logical operator called an "if" structure. If something is true then do something else. This is presented as "if (something) then; dosomething; endif". Notice that it is written almost as it is said. The "endif" modifier is necessary to tell the computer you are at the end of the "dosomething" since it can be multiline. In this case the code reads:
if ("$file" == "") then echo -n "Enter a file name to send: " set file = $< endif
Notice that the $file variable is contained in double quotes. The double quotes convert whatever is in the $file variable to characters for the purpose of the compare. The two double quote marks to the right of the double equal sign signify the empty set. In this case if I supplied a filename as a command parameter then the if-structure would be false and the command after the endif would be executed next. Since I didn't provide a filename the equation is true and the program prompts for the answer.
After the command parameters we write some messages to a file for inclusion with the email. The messages tell the user what the contents of the email message is and how to convert it back into the original file. Next we use the "cat" command to send a copy of the file to the "uuencode" command. The "uuencode" command prepares a file to be sent over email. Enter the command "man uuencode" at your Unix command line prompt for more information about uuencode. The output of uuencode is sent to standard output where it is appended to the messages we wrote in the scratch file. The total of the scratch file with the uuencoded file appended to it is sent to the standard input of the email command with a variable telling email the Internet email address of the receiver.
The next few lines updates a database of files sent by recording the filename, receiver Internet email address and the date and time to a file contained in my home directory. Finally the program cleans up by erasing the scratch file and displaying a message to the user that is done.
This is a complex program. Don't be upset if some of it is hard to understand. Type it in and work with it. Change some of the commands and see if you can make it work better. Play with it and you will learn a great deal. If you want a copy of the program by Internet email send me a message requesting it.
Warning! All email messages can be easily faked. You can receive an email message that looks like it is from me or anyone else and it may not be. Never execute any program sent to you by email unless you asked for the program and was expecting it. If you do, you can destroy your system by infecting it with a virus, Trojan horse or any of many other forms of malicious software. Be careful! If you received anonymous software and you think it may be an infected Unix program you can contact CyberSoft (+1 610/825-4748) and they will examine it for you.
Next month we continue learning the script language and look at more useful programs written in script. Additionally, my editor will be sending me on an all expense paid cruse aboard a luxury liner. I can hardly wait! See you next month.
Pete Radatti is the founder and CEO of CyberSoft, Inc. CyberSoft manufactures, VFind the 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.
Figure 2 CyberSoft Email Program
#/bin/csh -f /bin/rm -f scratch.send echo "CYBERSOFT EMAIL FILE SENDING PROGRAM VERSION 1.01" echo "PLEASE USE A SIMPLE FILENAME WITHOUT DIRECTORY NAMES." echo "AN EXAMPLE IS: sun4.tar not /net/vfind/sun4.tar"
set file = $1 set user = $2
if ( "$file" == "" ) then echo -n "Enter a file name to send: " set file = $< endif
if ( "$user" == "" ) then echo -n "Enter a user to send to: " set user = $< endif
echo "SEND: $file TO $user"
echo "Subject: CYBERSOFT UPDATE" > scratch.send echo " " >> scratch.send echo "EMAIL ENCAPSULATED FILE FROM CYBERSOFT" >> scratch.send echo "File sent: $file" >> scratch.send echo "File sent to: $user" >> scratch.send
echo " " >> scratch.send echo "The file contained in this email message was encoded using the" >> scratch.send echo "uuencode command. To restore the file, save this message to a file." >> scratch.send echo "We suggest a filename of xxx. Then use the uudecode command to restore the" >> echo scratch.send echo "original file. You must be aware of the rules of email and uudecode to" >> scratch.send
echo "to this easily. If the filename ends in 'tar' then you must execute " >> scratch.send echo "the tar command to finish extract the files. " >> scratch.send echo "Example: uudecode xxx [this creates the file sun3.tar]" >> scratch.send echo " tar xvf sun3.tar [this creates the directory vfind]">> scratch.send echo " " >> scratch.send
echo " " >> scratch.send echo " " >> scratch.send echo -n "THIS MESSAGE AND IT'S ENTIRE CONTENTS ARE COPYRIGHT BY CYBERSOFT " >> echo scratch.send date >> scratch.send echo " " >> scratch.send echo "NOTICE: CyberSoft can and will press charges against anyone who copies," >> scratch.send echo "tampers, destroys, diverts or modifies this message except for the" >> scratch.send echo "intended recipient" >> scratch.send echo " " >> scratch.send
cat $file |uuencode $file |cat scratch.send - |mail $user
# echo "Updating email database" echo " " >> ~radatti/email.database echo -n "$file sent to $user " >> ~radatti/email.database date >> ~radatti/email.database echo "MAIL SENT: END OF JOB" /bin/rm -f scratch.send echo " "
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.






