4.10 Internet4 Labs4.8 First Shell Script4.9 Shell Script #2

4.9 Shell Script #2

Shell Scripts are like DOS batch files. All systems have a way of automating the command line interface. Some work better than others. UNIX/Linux systems provide multiple ways of automating the command line interface. The programs that do this are called shells. On this system the following are listed in the file /etc/shells.
# /etc/shells: valid login shells
/bin/ash
/bin/bash
/bin/csh
/bin/sh
/usr/bin/es
/usr/bin/ksh
/usr/bin/rc
/usr/bin/tcsh
/usr/bin/zsh

There are other shells and other ways of automating program execution on UNIX/Linux systems. The programming language Perl is a good example.
  1. Create a shell script with the editor to compile and run the PASCAL program from your previous labs Lab 4.5: "Enter a PASCAL program" and Lab 4.6: "Pascal program compile and run". UNIX/Linux shell scripts use a # at the beginning of the line to indicate a comment. The first two characters in the file are special. If the characters are #!, then the line indicates which program will be used to run the script. Use the file name: pascal.
    #!/bin/bash
    # pascal - your name - today's date 
    #
    gpc -o trip trip.pas 
    trip << endofdata
    2100
    70
    40
    1.69
    endofdata
    
    A few notes explaining the shell script. First, the script is going to use the bash shell. And its executable is located in the file !/bin/bash. gpc is the Pascal compiler. The output of the compiler is directed to the file trip. The line "trip << endofdata" will run the program using input from the shell script down to the line containing "endofdata". The four lines of data will go into the program as miles, speed, miles per gallon, and price per gallon just as if they were entered from the keyboard. The output will still go to the screen.
  2. Next change the permissions on the file pascal so that you can execute it.
    chmod a+x pascal
    
  3. Then execute the shell script.
    pascal
    
    It should display on your terminal the results of the trip calculations. When you have this working, re-run the program sending the output to the printer.
    pascal  | lpr
    
Turn in the printout from section 4.9 marked with:
Your-Name
Lab 4.9: Shell Script #2
TABER CIS135

Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site ** The Mad Dr. G.'s home page on phred.

4.10 Internet4 Labs4.8 First Shell Script4.9 Shell Script #2