I need to combine two scripts, into one script. One is to delete something, the to recreate. Here is the scripts:
BIN=/usr/lbin
LINE='device for 0101a01: lpd://172.25.41.111:515'
while read LINE
do
prt=`echo $LINE | awk '{print $3 }'| cut -c 1-7`
echo $prt
#drv=`echo $LINE | awk -F":" '{print $2}'`
#echo $drv
#IP=`echo $LINE | awk -F"/" '{print $3}' | cut -d":" -f1`
#echo $IP
#port=`echo $LINE | awk -F":" '{print $4}'`
#echo $port
if [ "${prt}" = "0117bd1" ]; then
echo "\n\t Deleting printer $prt !!!"
$BIN/lpadmin -x ${prt} 2> /dev/null
fi
done < diffs.txt
recreate statement:
#!/bin/ksh -xv
BIN=/usr/lbin
LINE='device for 0101a01: lpd://172.25.41.111:515'
while read LINE
do
prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7`
echo $prt
drv=`echo $LINE | awk -F":" '{print $2}'`
echo $drv
IP=`echo $LINE | awk -F"/" '{print $3}' | cut -d":" -f1`
echo $IP
port=`echo $LINE | awk -F":" '{print $4}'`
echo $port
if [ "${prt}" = "0117bd1" ]; then
echo " Adding printer $prt !!!!"
$BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
echo " Printer $prt:$drv:$IP:$port added!!!!"
fi
done< diffs.txt
The variables are the same...the text file has three printers that need to be deleted and recreated...
Consolidate your variable declarations and then either wrap the differing behaviors into separate sections of a case..esac statement, or wrap them into individual function() structures. Either can/should be approached based on $1 value of delete/recreate to the script itself.
BIN=/usr/lbin
while read filler filler prt drv
do
prt=${prt%%:*}
IP=${drv##*/}
IP=${IP%%:*}
port=${drv##*:}
echo "prt=[$prt] IP=[$IP] port=[$port]"
if [ "${prt}" = "0117bd1" ]; then
echo "\n\t Deleting printer $prt !!!"
$BIN/lpadmin -x ${prt} 2> /dev/null
fi
if [ "${prt}" = "0117bd1" ]; then
echo " Adding printer $prt !!!!"
$BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
echo " Printer $prt:$drv:$IP:$port added!!!!"
fi
done <diffs.txt
Jean-Pierre.
Will give it a shot.thanks
---------- Post updated 05-28-10 at 09:51 ---------- Previous update was 05-27-10 at 15:51 ----------
Gave it a bash, and it worked fine for deleting 1 printer at a time. But if i need to delete printers in my text file can i do the following:
#Delete or Create Printer
if [ -z "${prt}" ]; then
echo "\n\t Deleting printer ${prt} !!!"
$BIN/lpadmin -x ${prt} 2> /dev/null
fi
if [ -z "${prt}" ];then
echo "\n\t Adding printers $prt !!!!"
$BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
echo "\n\t Printer $prt:$drv:$IP:$port created"
fi
done < diffs.txt
using the -z or what other alternatives do i have?????
You can do something like that (not tested):
BIN=/usr/lbin
while read filler filler prt drv
do
prt=${prt%%:*}
IP=${drv##*/}
IP=${IP%%:*}
port=${drv##*:}
echo "prt=[$prt] IP=[$IP] port=[$port]"
[ -z "${prt}" ] && continue
echo "\n\t Deleting printer $prt !!!"
if ! $BIN/lpadmin -x ${prt} 2> /dev/null
then
echo "Failed to delete printer!"
continue
fi
echo " Printer ${prt} deleted."
echo " Adding printer $prt !!!!"
if ! $BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
then
echo "Failed to add printer!"
continue
fi
echo " Printer $prt:$drv:$IP:$port added!!!!"
done <diffs.txt
~
Jean-Pierre.