Writing Linux Script Programs

An example script:

#!/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.
  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

A makefile script example

Scripting that checks a file and modifies it if required. Since it is from a makefile, this file is similar to a normal script but is slightly modified with lines that end with ";\" characters. This section demonstrates modification of the rc.local file from a makefile so a script program, "printst", may be run in the background when the system starts. The variable "BINLOC" is defined earlier with the line "BINLOC = /usr/local/tmp". See the section on "Making Packages" under the header "Making makefiles".

# Add the path for $(BINLOC) to the /etc/rc.d/rc.local file
	@PATHIN=`grep "path=" /etc/rc.d/rc.local | grep "$(BINLOC)"`; \  # See if the BINLOC path is set in the rc.local file
	HEADER=""; \
	if \
	   [ -z "$$PATHIN" ]; \
	then \
	   echo Adding $(BINLOC) to path in /etc/rc.d/rc.local; \
	   echo >> /etc/rc.d/rc.local; \
	   echo "###### Added by Print Service Install on `date` by `whoami`" >> /etc/rc.d.rc.local; \
	   HEADER=Y; \
	   echo "export PATH=\$$PATH:$(BINLOC)" >> /etc/rc.d.rc.local; \
	else \
	   echo $(BINLOC) already set in path in file /etc/rc.d/rc.local; \
	fi; \
	PRINTST=`grep -i "printst &" /etc/rc.d/rc.local`; \  # See if the string printst is in the rc.local file
	if \
	   [ -z "$$PRINTST" ]; \
	then \
	   echo Adding printst command to /etc/rc.d/rc.local file; \
	   if \
	      [ -z "$$HEADER" ]; \
	   then \
	      echo ɬ> /etc/rc.d.rc.local; \
	      echo "###### Added by Print Service Install on `date` by `whoami`" >> /etc/rc.d.rc.local; \
	      HEADER=Y; \
	   fi; \
	   echo "printst &" >> /etc/rc.d.rc.local; \
	else \
	   echo Already have printst command in /etc/rc.d/rc.local file; \
	fi; \
	if \
	   [ -n "$$HEADER" ]; \
	then \
	   echo "###### End of Print Service Install Section ######"  >> /etc/rc.d.rc.local; \
	   echo >> /etc/rc.d.rc.local; \
	fi;
Linux Howtos Contents Page