PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : PDF2Mail Probleme - Änderung bei vorhandenem Script



heatwalker
27.11.08, 10:28
Vielleicht kann mir hier mal jemand die Bäume vor den Augen weg räumen. :rolleyes:

Ich habe ein, bei openSuSE vorhandenes Script geändert um PDFs über den virtuellen Drucker per Mail zu versenden.

Ursprungsscript war smbprngenpdf von SuSE.

Als erstes habe ich die Zeilen aus "Einfügung Teil 2" eingefügt.
Das funktioniert auch soweit. Eine Mail wird versandt.

Nun habe ich leider ein Programm das den Dateinamen in etwa so übergibt.

\\INSERVE4\DAVID\ARCHIVE\COMMON\M5C9C5C1.pdf

Da hier die Dateiübergabe wegen der Slash's nicht funktioniert, habe ich mir die "Einfügung Teil 1" überlegt.

Wenn ich das so in der bash direkt eingebe funktioniert es. Halt nur nicht in zusammenhang mit dem Script.

Vielleicht kann mir ja einer dazu helfen.


Originalteil:

#! /bin/bash
# Copyright (c) 2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# $Id: smbprngenpdf,v 1.3 2003/07/30 16:30:40 lmuelle Exp $
#
# Author: Martin Rode, Programmfabrik GmbH, http://www.programmfabrik.de/
# <martin.rode@programmfabrik.de>
# Lars Mueller <lmuelle@SuSE.de>, 2003
#
# Based on Martin Rode's work as published in the iX magacine 2003-03, 139 ff.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GPL.
#

BASENAME=$( basename $0)
PATH="/usr/bin:/bin"

# decide if we use stdout or logger for the debug messages
if [ /dev/stdout -ef /dev/null ]; then
debug_cmd="logger -t ${BASENAME} -- \$*"
else
debug_cmd="echo \$2"
fi

# set a default debug level
declare -i DEBUG=2
function debug()
{
# return if the message's debug level is greater than the current
test $1 -gt ${DEBUG} && return

eval ${debug_cmd}
return
}

function backup()
{
# if no such file exits or no backups are wanted return
if [ ! -e "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.$1" ] || [ ${BACKUP_AMOUNT} -eq 0 ]; then
return
fi
# check if we must shift the backup files
declare -i filenum=0 maxfilenum=0
IFS='
'
for file in $( find ${DOCUMENT_DEST_DIR} -type f -regex "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-[0-9]+.$1"); do
rest=${file#${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-}
filenum=$( echo ${rest%.$1} | egrep "[0-9]+")
debug 7 "${LINENO} file:<${file}> rest:<${rest}> filenum:<${filenum}>"
test ${filenum} -gt $maxfilenum && maxfilenum=${filenum}
done
debug 5 "${LINENO} backupamount:<${BACKUP_AMOUNT}> maxfilenum:<${maxfilenum}>"
test ${maxfilenum} -ge ${BACKUP_AMOUNT} && maxfilenum=$[BACKUP_AMOUNT-1]
while [ ${maxfilenum} -ge 1 ]; do
test -e "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-${maxfilenum}.$1" && \
mv "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-${maxfilenum}.$1" "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-$[maxfilenum+1].$1"
maxfilenum=$[maxfilenum-1]
done
mv "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.$1" "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}-backup-1.$1"
}

function usage()
{
cat <<EOM
${BASENAME} is thought to convert a SMB print spool file to a PDF.

By default the destionation directory is a sub directory of the users home
directory, ~/PDF/. Due to privacy the create mode setting of the Samba share is
taken in account while creating the sub diretory and the document file. The sub
directory could be changed with the -D option.

An alternate destionation directory could be set with -p. Then no default sub
directory is set.

As an intermediate step a PostScript file is created. You could decide if this
file should be kept with the -k option.

${BASENAME} options:

-D destination sub directory
-J job name as provided by the %j macro
-b amount of backups kept; default are 10
-c print job page count as provided by the %c macro
-d set a debug level to write messages to the syslog
-k keep the PostScript file
-p absolute path to the destination directory
-h this text
-s spool file name as provided by the %s macro
-u user name as provided by the %u macro
-z spool file size as provided by the %z macro
EOM
exit $1
}

# set defaults
declare -i BACKUP_AMOUNT="5"
KEEP_PS_FILE=""
SPOOL_FILE_NAME=""

while getopts :D:J:b:c:d:ihkp:s:u:z: opt ; do
case ${opt} in
\:|\?) case ${OPTARG} in
D) debug 1 "-D requires a sub directory"
;;
J) debug 1 "-J requires a job name"
;;
b) debug 1 "-b requires a numerical value for the backup amount"
;;
c) debug 1 "-c requires the ammount of pages"
;;
d) debug 1 "-d requires a numerical debug level"
;;
p) debug 1 "-p requires a directory name"
;;
s) debug 1 "-s requires a spool file"
;;
u) debug 1 "-u requires a user name"
;;
z) debug 1 "-z requires the spool file size"
;;
*) echo "Unknown option: -${OPTARG}"
;;
esac
exit 1
;;
D) DEST_SUB_DIR=${OPTARG}
;;
J) JOB_NAME=${OPTARG}
;;
b) if echo ${OPTARG} | egrep "[^0-9]" >/dev/null; then
debug 1 "-b requires a numerical value not <${OPTARG}> for the backup amount"
exit 1
fi
BACKUP_AMOUNT=${OPTARG}
;;
c) PAGE_COUNT=${OPTARG}
;;
d) if echo ${OPTARG} | egrep "[^0-9]" >/dev/null; then
debug 1 "-d requires a numerical debug level not <${OPTARG}>"
exit 1
fi
DEBUG=${OPTARG}
;;
k) KEEP_PS_FILE="1"
;;
p) DOCUMENT_DEST_DIR=${OPTARG}
;;
h) usage
;;
s) SPOOL_FILE_NAME=${OPTARG}
;;
u) USER_NAME=${OPTARG}
;;
z) SPOOL_FILE_SIZE=${OPTARG}
;;
esac
done
debug 10 "${LINENO} s:<${SPOOL_FILE_NAME}> J:<${JOB_NAME}> c:<${PAGE_COUNT}> z:<${SPOOL_FILE_SIZE}> u:<${USER_NAME}>"

# if called without an argument show the help and exit
if [ -z ${SPOOL_FILE_NAME} ]; then
debug 1 "No spool file provided with -s spool_file_name; use -h for help"
exit 1
fi

# set DEST_SUB_DIR to default if no DOCUMENT_DEST_DIR was set by -p
if [ -z ${DOCUMENT_DEST_DIR} ] && [ -z ${DEST_SUB_DIR} ]; then
DEST_SUB_DIR="PDF"
fi

if [ ${DOCUMENT_DEST_DIR} ]; then
DOCUMENT_DEST_DIR=$( eval echo ${DOCUMENT_DEST_DIR})
test ${DEST_SUB_DIR} && DOCUMENT_DEST_DIR=${DOCUMENT_DEST_DIR}/${DEST_SUB_DIR}
debug 5 "${LINENO} u:<${USER_NAME}> s:<${SPOOL_FILE_NAME}> documentdestdir:<${DOCUMENT_DEST_DIR}>"
else
USER_HOME_DIR=$( eval echo ~${USER_NAME})
test ${USER_HOME_DIR} || USER_HOME_DIR="/var/tmp"
DOCUMENT_DEST_DIR=${USER_HOME_DIR}/${DEST_SUB_DIR}
debug 5 "${LINENO} u:<${USER_NAME}> s:<${SPOOL_FILE_NAME}> homedir:<${USER_HOME_DIR}> dessubdir:<${DEST_SUB_DIR}>"
fi


if [ ${JOB_NAME} ]; then
echo ${JOB_NAME} | egrep -i "^[A-Za-z0-9_]*://" && \
DOCUMENT_NAME=${JOB_NAME} || \
DOCUMENT_NAME=${JOB_NAME%.*}
debug 7 "${LINENO} j:<${JOB_NAME}> documentname:<${DOCUMENT_NAME}>"
else
DOCUMENT_NAME=$( egrep -i "^%%Title: " ${SPOOL_FILE_NAME} | \
head -n 1 | \
sed -e 's/^%%Title: //g' \
-e 's/^(//g' \
-e 's/\(.*\)\..*/\1/g' | \
tr -d "\r\n")
DOCUMENT_NAME=$( echo -e -n ${DOCUMENT_NAME})
debug 7 "${LINENO} documentname:<${DOCUMENT_NAME}>"
fi

# remove leading URL:// and replace laft '/' with '_'
DOCUMENT_NAME=$( echo ${DOCUMENT_NAME} | \
sed -e "s/^[A-Za-z0-9_]*:\/\///g" -e "s/\/$//g" -e "s/\//_/g")
debug 7 "${LINENO} documentname:<${DOCUMENT_NAME}>"

rc=0
# create destination directory if it not exists
if [ ! -d "${DOCUMENT_DEST_DIR}" ]; then
mkdir -p "${DOCUMENT_DEST_DIR}" || rc=1
chown ${USER_NAME}: "${DOCUMENT_DEST_DIR}" || rc=1
chmod --reference=${SPOOL_FILE_NAME} "${DOCUMENT_DEST_DIR}" || rc=1
chmod a+x "${DOCUMENT_DEST_DIR}" || rc=1
test ${rc} -gt 0 && debug 2 "${LINENO} can't create destdirectory:<${DOCUMENT_DEST_DIR}>"
fi

test ${KEEP_PS_FILE} && backup ps
# create new file with permissions of the spool file
touch "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"
chmod --reference=${SPOOL_FILE_NAME} "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"
# extract PostScript part from the spool file
awk '/^%!PS-Adobe/,/^%%EOF/' ${SPOOL_FILE_NAME} >"${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"

backup pdf
# create new file with permissions of the spool file
touch "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf"
chmod --reference=${SPOOL_FILE_NAME} "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf"
# create PDF file from PostScript
gs -q -dPARANOIDSAFER -dCompatibilityLevel=1.3 -dNOPAUSE -dBATCH \
-sDEVICE=pdfwrite -sOutputFile="${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf" \
-c save pop -f "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"


Einfügung Teil 1 (welche nicht funktionert)

for PDF_IN in $(find "${DOCUMENT_DEST_DIR}/" -type f); do DOCUMENT_NAME=$(echo "${PDF_IN}" | awk -F \\ '{print $7}'); mv "${PDF_IN}" "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}"; done


Einfügung Teil 2 (funktioniert grundsätzlich)

sh_username=$(grep $USER_NAME /tmp/mail | awk -F \= '{ print $1 }')
echo "Hier ist die PDF-Datei:" | mail -s "Die neue PDF-Datei ist fertig..." -a \
"${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf" ${sh_username}@inserve.de
rm "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.pdf"


Restlicher Originalteil

# handle ps file
if [ ${KEEP_PS_FILE} ]; then
debug 0 "${LINENO} keep intermediate PostScript file"
else
rm -f "${DOCUMENT_DEST_DIR}/${DOCUMENT_NAME}.ps"
fi

# handle smbprn spool file
if [ ${DEBUG} -gt 2 ]; then
debug 3 "${LINENO} s:<${SPOOL_FILE_NAME}> not removed"
else
rm -f ${SPOOL_FILE_NAME}
fi

heatwalker
27.11.08, 19:55
Hab es jetzt erst einmal anders gelöst.

Da ich nicht in der Lage bin, die Slashs vernünftig rauszubekommen und den
Namen dementsprechend zu kürzen,
habe ich das so angepasst, das die Dokumentnamen nicht abgefragt werden,
sondern automatisch mit Datum und Uhrzeit versehen werden.

Das es nur eingehende Faxe (Faxware5 unter Win2003Srv) sind, reicht mir das. (-;

Original:

if [ ${JOB_NAME} ]; then
echo ${JOB_NAME} | egrep -i "^[A-Za-z0-9_]*://" && \
DOCUMENT_NAME=${JOB_NAME} || \
DOCUMENT_NAME=${JOB_NAME%.*}
debug 7 "${LINENO} j:<${JOB_NAME}> documentname:<${DOCUMENT_NAME}>"
else
DOCUMENT_NAME=$( egrep -i "^%%Title: " ${SPOOL_FILE_NAME} | \
head -n 1 | \
sed -e 's/^%%Title: //g' \
-e 's/^(//g' \
-e 's/\(.*\)\..*/\1/g' | \
tr -d "\r\n")
DOCUMENT_NAME=$( echo -e -n ${DOCUMENT_NAME})
debug 7 "${LINENO} documentname:<${DOCUMENT_NAME}>"
fi


Neu

if [ ${JOB_NAME} ]; then
echo ${JOB_NAME} | egrep -i "^[A-Za-z0-9_]*://" && \
DOCUMENT_NAME=${JOB_NAME} || \
DOCUMENT_NAME=${JOB_NAME%.*}
debug 7 "${LINENO} j:<${JOB_NAME}> documentname:<${DOCUMENT_NAME}>"
else
# DOCUMENT_NAME=$( egrep -i "^%%Title: " ${SPOOL_FILE_NAME} | \
# head -n 1 | \
# sed -e 's/^%%Title: //g' \
# -e 's/^(//g' \
# -e 's/\(.*\)\..*/\1/g' | \
# tr -d "\r\n")
DOCUMENT_NAME=$( date +%X-%d.%m.%y)
# DOCUMENT_NAME=$( echo -e -n ${DOCUMENT_NAME})
debug 7 "${LINENO} documentname:<${DOCUMENT_NAME}>"
fi