LPIT

#!/bin/sh
# This script is to prevent users from printing executable and postscript
# files.  This script uses the output of the command, `file` to determine
# if the named file can be printed.  The description returned by `file` is
# checked with grep.  the words that grep check for will need to be adjusted
# for each flavor of Unix.  The script will work on SGC, MIPS and SCO
# as is.  
# Directions for use:  Place lpit in a directory in you path ie. /usr/local/bin
# Create a function in .profile. ( I used lpr because it is not a valid
# command on my systems).
# lpr()
# {
# /usr/bin/lpit "$*"
# }
# for csh put this line in .cshrc file   alias lpr 'lpit \!*'
# Note: check the location of your lp command and adjust in script as needed.
# When printing use the command lpr [-option] file file file ...
# lpit will take all standard lp options (unix in a nutshell).
#
# Author Douglas R. Probst 
# lpit version 2.0 11/08/95
# THE USUAL DISCLAIMERS APPLY!

To see lpit  without the comments.

###############################################################################
# Function to display the correct usage of lpit if the user tries to use an
# unsupported option.  NOTE: functions must come before the call to the function
# in Bourne shell.  Functions are read into memory and are kept there until 
# they are needed.  
###############################################################################


usage()
{
echo " usage: $0 [-c] [-d dest] [-f name] [-H action] [-i IDs] [-m] "
echo "           [-n number] [-o options] [-P list] [-q n] [-r] [-s]"
echo "           [-S name] [-t title] [-T content] [-w] [-y mode] [filename] "
    exit 1
}

###############################################################################
# set LP shell variable to system lp path (system dependent so modify if needed
###############################################################################


LP=/bin/lp


###############################################################################
# get_opt determines the allowed options.
###############################################################################


set -- `getopt cd:f:H:i:lmn:o:P:q:rsS:t:T:wy: $@ 2>/dev/null`
if [ $? != 0 ]; then
   usage
fi


###############################################################################
# for loop  parses the command line for options and sets a variable accordingly 
# (works with get_opt).
###############################################################################

for i in $@
do
    case $i in
        -c) C="-c"
            shift 1;;
        -d) D="-d$2"
            shift 2;;   
        -f) F="-f$2"
            shift 2;;   
        -H) H="-H$2"
            shift 2;;   
        -i) i="-i$2"
            shift 2;;   
        -m) M="-m"
            shift 1;;   
        -n) N="-n$2"
            shift 2;;   
        -o) O="-o$2"
            shift 2;;   
        -P) P="-P$2"
            shift 2;;   
        -q) Q="-q$2"
            shift 2;;   
        -r) R="-r"
            shift 1;;   
        -s) S="-s"
            shift 1;;   
        -S) S2="-S$2"
            shift 2;;   
        -t) T="-t$2"
            shift 2;;   
        -T) T2="-T$2"
            shift 2;;   
        -w) W="-w"
            shift 1;;   
        -y) Y="-y$2"
            shift 2;;   
        --) shift 
            break;;
    esac
done


################################################################################
# If there is one or more  files on the command then process each file one at
# a time.  If the information to print is coming from stdin or via pipe
# don't use the for loop, just process (second to last line.)
###############################################################################

if [ $# -ge 1 ]; then 
    for i in $@        
    do

###############################################################################
# Below we have bunch of stuff.  
# TYPE is a shell variable that we are setting.
# First we run the unix "file" command on the first file that comes through the
# for loop ($i). The output of "file" is sent via | (pipe) to Awk. Awk returns
# a string to the variable "TYPE", which is the output of the file command
# minus the first word (the file name)
# The ` at the beginning ` at the end cause the command substitution. 
###############################################################################


        TYPE=`file $i | awk '{print $2, $3, $4, $5}'`


###############################################################################
# Now we echo $TYPE (which is a string like "commands text") and pipe it
# to egrep.  The -i options means ignore case.  If egrep finds the words 
# "text" or "/bin/sh" it returns a 0 (This may not be true on all systems!)
# any output from grep is sent to /dev/null ( nowhere land )
# The case statement checks $? (the exit status of the last statement executed)
# If it was 0 do another check, this one form postscript.
# If we find postscript we bail out, if not we print the file with any options
# that were set up top in the for loop.
# If we didn't find text|/bin/sh then bail out.
#
###############################################################################


        echo "${TYPE}" | egrep -i '(text|/bin/sh)'  > /dev/null 2>&1

        case $? in
            0) echo "${TYPE}" | egrep -i '(postscript)'  > /dev/null 2>&1
               if [ $? -eq 0 ]; then
                   echo "$i, is of the type, <${TYPE}>,  and can not be printed!!"    #return message and exit
                else 
                    $LP $C $D $F $H $I $M $N $O $P $Q $R $S $S2 $T $T2 $W $Y $i
                    if [ $? = 0 ]; then
                        echo "$i is printing!"    # display message
                    else
                        echo "The print command has failed! " 2>&1 
                    fi
                fi
                ;;  
         [1]*)echo "$i, is of the type, <${TYPE}>, and can not be printed!!";;
        esac
    
    done
        
else


##############################################################################
# Takes input from pipe or stdin NOTE: no check for file type.
##############################################################################


    $LP $C $D $F $H $I $M $N $O $P $Q $R $S $S2 $T $T2 $W $Y 
fi

Back to Unix page