Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to create printers on unix within a function

Status
Not open for further replies.

grapes12

Technical User
Mar 2, 2010
124
ZA
I need assistance on creating ,deleting printers within a function herwith is what i have
Code:
#!/bin/ksh
BIN=/usr/lbin
LOCAL_FILE=`$BIN/lpstat -v >> sun5-printers.txt
REMOTE_FILE=`rsh sun8 /usr/lbin/lpstat -v >> sun8-printers.txt
awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ }}{ if (!($0
 in arr ) ) {print} }' sun8-printers.txt

#Amend,create or delete Printer

prExists=$?
function printerExists()
{
prName=" "
prAddress=" "
prLocation=" "
prdescrip=" "
prfilter=" "
prport=" "
prdriver=" "
   if [ $prExists -eq 1 ]; then
        echo "\n\t Printer already exists. "
   else
        echo
        $BIN/lpadmin -x $Prname
        echo "\n\t Deleting printer $Prname !!!"
   fi
  # Add Printer Command 
  lpadmin -p "${prName}" -E -D "${prDescript}" -L "${prLocation}" -i /u1/cups/mac/"$prFilter_filter -v \
  "$Prdriver"://"${prAddress}":"Prport"
fi

Any assistance will be highly appreciated
 
made some adjustments but need debugging on my code
Code:
#!/bin/ksh
BIN=/usr/lbin/
LOCAL_FILE=`$BIN/lpstat -v >> sun5-printers.txt
REMOTE_FILE=`rsh sun8 /usr/lbin/lpstat -v >> sun8-printers.txt
DIFF_FILE=/usr/local/bin/diff.txt

awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ }}{ if (!($0
 in arr ) ) {print} }' sun8-printers.txt >> diff.txt

while read printer
do
printer="$(echo $printer | cut -d: -f1)"
echo $printer
done < diffs.txt
exit 0

#Amend,create or delete Printer

f_Delete()
{
 if [ $printer -eq 1 ]; then
      echo "\n\t printer to be deleted"
      else 
      $BIN/lpadmin -x $Printer
      echo "\n\t Deleting printer $Printer !!!"
 fi
}
f_Create()
{
  if [ $printer -eq 0 ]; then
  echo "Enter Printer Name: \c"
  read printer
  echo "Enter IP Address  : \c"
  read ip
  echo "Enter Location    : \c"
  read loc
  echo "Enter Description : \c"
  read desc
  echo "Enter Filter      : \c"
  read filt
  echo "Enter Port        : \c"
  read port
  echo "Enter Driver      : \c"
  read driv
  echo "\n\t Printer does not exists. "
  else 
  $BIN/lpadmin -p "$Printer" -E -D "$Desc" -L "$Loc" -i /u1/cups/mac/"$Filt"_filter -v \
  "$driv"://"$IP":"Port"
  echo "\n\t Printer $printer created"
 fi
}
 
Please assist..with debugging code????
 
What's the bug? Is it not working? Not doing what you want it to do? Generating error messages?

Annihilannic.
 
Not doing what i want it to do!
Its not running my functions so that i could fix any errors..
herewith is the script:
Code:
#!/bin/ksh
BIN=/usr/lbin/
LOCAL_FILE=`$BIN/lpstat -v > sun5-printers.txt`
REMOTE_FILE=`rsh sun8 /usr/lbin/lpstat -v > sun8-printers.txt`
#DIFF_FILE=/usr/local/bin/diffs.txt

awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ } } { if (!($0 in a
rr ) ) { print } }' sun8-printers.txt > diffs.txt

while read printer
do
printer="$(echo $printer | cut -c 0-20)"
echo $printer
done < diffs.txt


#Amend,create or delete Printer
f_get_ptr()
{
  echo
  echo "              Enter printer: \c"
  echo
}

f_Delete()
{
printer=' '
f_get_ptr
read printer

r=$?

 if [ $printer -eq 1 ]; then
      echo "\n\t $printer to be deleted"
      else
      $BIN/lpadmin -x $Printer
      echo "\n\t Deleting printer $Printer !!!"
 fi
}

f_Create()
{
  if [ $printer -eq 1 ]; then
  echo "Enter Printer Name: \c"
  read printer
  echo "Enter IP Address  : \c"
  read ip
  echo "Enter Location    : \c"
  read loc
  echo "Enter Description : \c"
  read desc
  echo "Enter Filter      : \c"
  read filt
  echo "Enter Port        : \c"
  read port
  echo "Enter Driver      : \c"
  read driv
  echo "\n\t Printer does not exists. "
  else
  $BIN/lpadmin -p "$Printer" -E -D "$Desc" -L "$Loc" -i /u1/cups/mac/"$Filt"_filt
er -v \
  "$driv"://"$IP":"Port"
  echo "\n\t Printer $printer created"
 fi
}

if [[ -n $printer ]];then
f_delete
fi

if [[ -n $printer ]];then
f_create
fi

From the functions its only giving me this when executing:
Code:
ksh -x resync-printers.ksh
/bin/pwd
2> /dev/null
PWD=/usr/local/bin
+ BIN=/usr/lbin/
+ + /usr/lbin//lpstat -v
+ 1> sun5-printers.txt
LOCAL_FILE=
+ + rsh sun8 /usr/lbin/lpstat -v
+ 1> sun8-printers.txt
REMOTE_FILE=
+ awk BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ } } { if (!($0 in
arr ) ) { print } } sun8-printers.txt
+ 1> diffs.txt
+ 0< diffs.txt
+ read printer
+ + cut -c 0-20
+ echo device for 0117bd1: lpd://172.25.29.60:515
printer=device for 0117bd1:
+ echo device for 0117bd1:
device for 0117bd1:
+ read printer
+ + cut -c 0-20
+ echo device for 2201bl7: socket://172.25.11.170:9100
printer=device for 2201bl7:
+ echo device for 2201bl7:
device for 2201bl7:
+ read printer
+ + cut -c 0-20
+ echo device for 5001bl1: lpd://172.25.11.203:515
printer=device for 5001bl1:
+ echo device for 5001bl1:
device for 5001bl1:
+ read printer
+ [[ -n  ]]
+ [[ -n  ]]

Where am i going wrong? assisstance will be highly appreciated!!!
 
Script is now calling my functions,
here are errors received, when running script using ksh -x scriptname:
Code:
device for 2201bl7:
+ read printer
+ + cut -c 0-20
+ echo device for 5001bl1: lpd://172.25.11.203:515
printer=device for 5001bl1:
+ echo device for 5001bl1:
device for 5001bl1:
+ read printer
+ f_Delete
resync-printers.ksh[5]: test: argument expected
lpadmin: Expected printer or class after '-x' option!

         Deleting printer  !!!
+ f_Create
resync-printers.ksh[2]: test: argument expected
lpadmin: The printer-uri must be of the form "ipp://HOSTNAME/printers/PRINTERNAME
".

         Printer  created

script requires test arguments as per errors.My $printer variable does not seem to be correct.?????
 
Don't have a lot of experience, but I see some strange things in your code:

You are getting all the differences in a txt file, and then you are reading this file and echoing all the contents, but you're not doing anything with the contents of the file.

Once you've read the whole file, you do 2 if statements.

First of all, maybe these statements should be anywhere else in the code, not here. Right now you are only executing these ifs with the latest content of "printer".

Also, you should use if [[ -n "$printer" ]] (check double quotes).
Another thing is that you have the same condition in the 2 ifs statements. Are you sure this is correct?

One last thing: in your cut -c you are picking "device for ZZZZZZZ:" and assigning it to printer. Are you sure this is the string you want to use?

 
WHEW!
you pick it all up in one go...

1. Yes the differences are written to a text, but i am struggling to use the differences effectively, or like i want to.How can i get it working?

2. The two IF statements I want too
2.1. delete the printer
2.2. recreate the printer

I am using functions so that i can achieve this, but no luck
NOT sure if the two IF statements are correct?????

lastly my cut is incorrect, that I know! I only want the" ZZZZZZ" to be displayed and use that "ZZZZZ" for the deletion and recreation.

ANY help will be highly appreciated.
 
1. I would read each printer in local file, and see if it's in the remote file. For each printer not in the remote file I'd call delete.
Then I would read each printer in remote file, and see if it's in the local file. For each printer not in local file I'd call create.

This makes 2 loops, and inside each loop a call to a function (delete or create).

To see if a printer in a file is in the other file maybe a grep should work.
 
So my awk statement displaying the differences, will not then work.My awk statement send the differences to a file called diffs.txt.
How can i use the diffs.txt file to delete or add the printers.
Here is my awk code
Code:
awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ } } { if (!($0 in a
rr ) ) { print } }' sun8-printers.txt > diffs.txt

and diffs.txt file
Code:
device for 0117bd1: lpd://172.25.29.60:515
device for 2201bl7: socket://172.25.11.170:9100
device for 5001bl1: lpd://172.25.11.203:515
[code]

All these the IP address needs to be changed, so deleting and recreating them is the only option.
 
It depends on what you need to do.

What do you want to do when:
- there is a printer in local file that is not in remote file?
- there is a printer in remote file that is not in local file?
- there is a printer in both files but with differences?

Maybe a simple diff would better fit your needs. In the diff result, a > or a < will indicate if it's in one file or the other.

 
I did run a diff and it displays exactly the same three printers, like i said the ip's need to be changed.
 
Well, in this case, for each printer call delete and create, from inside the loop.
 
So my function is just to delete the printers...
I ran the script again. and received the following errors
Code:
0117bd1 2201bl7 5001bl1
+ f_Delete
resync-printers.ksh[2]: 0117bd1^J 2201bl7^J 5001bl1: bad number

         Deleting printer  !!!

What is the bad number?????
 
What is 0117bd1 2201bl7 5001bl1?

Are these three printers?
Where do these numbers come from?

If these are printers you shouldn't make just one call to f_delete with the 3 printers, you should make three calls to f_delete, one for each printer.

 
yes these are three printers!
how do i do that?????
 
ok made amendments to the function!
Code:
f_Delete()
{
 if [ -z "$printer" ]; then
      echo "\n\t Printer to be deleted"
      return 1
   else
      for P in $printer; do
      echo "\n\t Deleting printer $Printer !!!"
      $BIN/lpadmin -x $P
      done
      return 0
 fi

Its giving me the following errors????
Code:
0117bd1 2201bl7 5001bl1
+ f_Delete

         Deleting printer  !!!
lpadmin: The printer or class was not found.

         Deleting printer  !!!

         Deleting printer  !!!

NOT PICKING UP MY THREE PRINTERS that must be deleted?????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top