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:
- 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
- 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.
- 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
|