[SunHELP] find in file script..

Sandwich Maker adh at an.bradford.ma.us
Thu Jun 19 10:45:56 CDT 2003


"if is not working tell me because I've put the script without test ...
"Adrian.
"
"On Tue, 10 Jun 2003 Enjoyer at aol.com wrote:
"
">        I'm trying to find a "printer" in /etc/hosts... remove it... then run
">        lpadmin -x "printer",
"
"
"From: Adrian.Florea at alcatel.ro
"
"try this:
"
"--- begin ----
"
"#!/bin/sh
"PRINTERNAME=`awk '{if ($1 ~ /printer_ip/) { print $2; exit}}' /etc/hosts`
"if sed -e "/$PRINTERNAME/d" /etc/hosts >/etc/hosts.new; then
"	cp /etc/hosts.new /etc/hosts
"fi
"lpadmin -x $PRINTERNAME
"
"--- end ----

trivially simplifiable to

--- begin ----

#!/bin/sh
PRINTERNAME=`awk '{if ($1 ~ /printer_ip/) { print $2; exit}}' /etc/hosts`
sed -e "/$PRINTERNAME/d" /etc/hosts >/etc/hosts.new &&
       cp /etc/hosts.new /etc/hosts
lpadmin -x $PRINTERNAME

--- end ----

or, in ksh:

--- begin ----

#!/usr/bin/ksh
# handles any number of network printers at one go.

(( $# )) || { print -u2 "no arg!  specify an IP."; exit 1; }

exec 3</etc/hosts 4>/etc/hosts.new

while read -u3 HOSTLINE
do
	set -A HOSTWORD $HOSTLINE
	PRINTERNAME=""
	for PRINTER_IP in "$@"
	do
		[ "${HOSTWORD[0]}" = "$PRINTER_IP" ] &&
			PRINTERNAME=${HOSTWORD[1]}
	done
	if	[ -n "$PRINTERNAME" ] 
	then	lpadmin -x $PRINTERNAME
	else	print -u4 "$HOSTLINE"
	fi
done
cp /etc/hosts.new /etc/hosts

--- end ----

that's enough triviality for now...
________________________________________________________________________
Andrew Hay                                  the genius nature
internet rambler                            is to see what all have seen
adh at an.bradford.ma.us                       and think what none thought



More information about the SunHELP mailing list