a much improved smbprint script.

Alfred Perlstein alfred at freebsd.org
Tue Dec 11 18:12:04 GMT 2001


* Jeremy Allison <jra at samba.org> [011209 12:37] wrote:
> On Mon, Dec 10, 2001 at 05:45:42AM +1030, Richard Sharpe wrote:
> > Richard Sharpe wrote:
> > 
> > > Alfred Perlstein wrote:
> > >
> > > OK, another problem I have is that ${param:+word} does not look portable.
> > 
> > Jeremy thinks that this is not very portable ... I want to change all 
> > use of ${param:+-l} ${param} into the following:
> > 
> > if [ "x$param" != "x" ]; then
> > 
> >   param="-l $param"
> > 
> > fi
> 
> Yes, that's much better.
> 
> 
> > ... ${param}
> > 
> > There is still the issue that getopts will not be portable as well :-(
> 
> Yes. Don't use getopts in a portpable script. Parse the args
> manually with shift etc.

Ok, I've fixed the nasty shell dependant var expansion (I think).

I'll leave the s/getopt/shift to you fellas, I'm about out of time
for this mini-project. :)

Here she is:

#!/bin/sh 

# This script is an input filter for printcap printing on a unix machine. It
# uses the smbclient program to print the file to the specified smb-based 
# server and service.
# For example you could have a printcap entry like this
#
# smb:lp=/dev/null:sd=/usr/spool/smb:sh:if=/usr/local/samba/smbprint
#
# which would create a unix printer called "smb" that will print via this 
# script. You will need to create the spool directory /usr/spool/smb with
# appropriate permissions and ownerships for your system.

# Set these to the server and service you wish to print to 
# In this example I have a WfWg PC called "lapland" that has a printer 
# exported called "printer" with no password.

#
# Script further altered by hamiltom at ecnz.co.nz (Michael Hamilton)
# so that the server, service, and password can be read from 
# a /usr/var/spool/lpd/PRINTNAME/.config file.
#
# Script further modified by Richard Sharpe to fix some things.
# Get rid of the -x on the first line, and add parameters
#
#    -t  now causes translate to be used when sending files
#
# Script further modified by Alfred Perlstein and Peter Swain to
# properly interact with smbclient (use -c instead of stdin to pass
# commands), a general clean up and the addition of more options
# such as debug output and the ability to set username and source IP.
#
# In order for this to work the /etc/printcap entry must include an 
# accounting file (af=...):
#
#   cdcolour:\
#	:cm=CD IBM Colorjet on 6th:\
#	:sd=/var/spool/lpd/cdcolour:\
#	:af=/var/spool/lpd/cdcolour/acct:\
#	:if=/usr/local/etc/smbprint:\
#	:mx=0:\
#	:lp=/dev/null:
#
# Should read the following variables set in the config file:
#   server	- Server offering print service.
#   service	- Sevice name of printer.
#   password	- Password. (optional)
#   username	- Username. (optional)
#   IP		- IP to use. (optional)
#   debug	- Turn on debugging? (optional)
#   debugfile	- File to send debug output into. (optional)

# The last parameter to the filter is the accounting file name.
#   Extract the directory name from the file name.
#   Concat this with /.config to get the config file.
#
TRANS=0
eval acct_file=\${$#}
spool_dir=`dirname $acct_file` 
config_file=$spool_dir/.config

. $config_file

if [ "x$password" = "x"] ; then
	password="-N"
fi

if [ "x$username" = "x" ] ; then
	username="$server";
fi

if [ "x$smbclient" = "x" ] ; then
	smbclient="/usr/pkg/bin/smbclient"
fi

if [ "x$smbconf" != "x" ] ; then
	smbconf_flag="-s"
	smbconf="$smbconf"
fi

if [ "x$IP" != "x" ] ; then
	IP_flag="-I"
	IP="$IP"
fi

while getopts t c; do
	case $c in
	t)
		TRANS=1
		;;

	'?')  # Bad parameters, ignore it ...
		;;
	esac
done

command="print - ;"
if [ $TRANS -eq 1 ]; then
	command="translate;$command";
fi

debugfile="/tmp/smb-print.log"
if [ "x$debug" = "x" -o $debug -eq 0 ] ; then
	debugfile=/dev/null debugargs=
else
	set -x; exec >>$debugfile 2>&1
	debugargs="$debugfile."
fi

if [ "x$debugargs" != "x" ] ; then
	debugargs_flag="-l"
	debugargs="$debugargs"
fi

$smbclient \
	"\\\\$server\\$service" \
	$password \
	$smbconf_flag $smbconf \
	$IP_flag $IP \
	$debugargs_flag $debugargs \
	-U $username \
	-P \
	-c "$command"




More information about the samba-technical mailing list