1. Introduction

    System Information

  2. Inter-Process Communication
  3. Signals

    Programming in Various Environments

  4. Script Programming
    1. Script Variables
    2. Test Conditions
    3. Control and Iteration
    4. Commonly used Programs
    5. Shell Capabilities
    6. Example looping script
    7. Example using Variables
    8. Example working with files
    9. Example install script
  5. C and C++ Programming
    1. POSIX System Capabilities
    2. More POSIX
    3. Threads
    4. Mutexes
    5. An example viewmod program
    6. An example serial program
  6. X Programming
  7. Debugging
  8. Credits

An example that manages and reads files

#!/bin/bash
#
# printst     This file manages print files in subdirectories
#               under the "/var/spool/spooldata" subdirectory.
#               It loops through each subdirectory, looking
#               for files that have the sticky bit set.  If it
#               is set, the file will be sent to the printer,
#               and the sticky bit will be cleared.
#               Files are sent to the chosen printer based on
#               the name of the directory and the configuration
#               file.
#
if [ -d /var/spool/spooldata ]; then	# if this spool subdirectory exists
   for dfile in /var/spool/spooldata/*; do	# for all files in spooldata
      if [ -d $dfile ]; then		# if the file is a subdirectory
         fname=${dfile#/var/spool/spooldata/} # parameter expansion used to get filename without path
         # get the printername from the configuration file based on the subdirectory name
         prname=`grep -i ${fname} /usr/local/etc/spooldata.conf | cut -f2 -d" "` 
         for ifile in $dfile/*; do	   # for all files in the subdirectory
            if [ -k $ifile ]; then	   # if the file has the sticky bit set
               lpr -P$prname $ifile # print the file
               chmod -t $ifile          # clear the sticky bit
            fi
         done
      fi
   done
fi

Put this file in your cron schedule to be run about once per minute or encase it in the following statements

while [ 1 ]
do
   sleep 60
   .
   .
done

Be aware of several things in this file:

  1. Where the variable prname is set, the output is directed with ` rather than '. Using ' will not work!!! This is documented in the bash man page under command expansion. $(command) may also be used
  2. Parameter expansion is used to extract the print name.
    • The $ is used to indicate parameter expansion.
    • The {} are used to separate parameter expansion.
    • The first string or variable is the parameter and the second one is the expansion element
    • If a # is used to separate parameter from element, the part of the element not matching the beginning of the parameter is kept.
    • If a % is used the part of the element not matching the end of the parameter is kept.
  3. If you copy this file or write it using DOS or windows, be sure all carriage returns have been stripped from the script file or it will not run right. Linux does not use carriage returns in files and many programs will not operate correctly when running files that have carriage returns in them.

An example spooldata.conf file with the directory name in /var/spool listed first. The printer that the file is sent to is listed as the second argument on the line.

print1 lp1
print2 lp2
print3 lp3