Help with script to create printers on unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script to create printers on unix
# 8  
Old 05-07-2010
Quote:
Originally Posted by ggoliath
what about debugging????

Use set -x and set +x or echo variables explicit which you want to inspect.
If something particular does not work, feel free to post the error and it's description Smilie

At a first glance, you implemented functions different as in my example. You don't hand over any parameter and there is still missing some logic that calls your functions. The read for so many variables would have to be supplied by input on the shell - is that what you want?

Check my example again or at least implement something to call your functions.
# 9  
Old 05-10-2010
ok will try, doing it your way!
the only problem is that, my awk statement gives me the difference between the local and prod server printer names.
I must update the local server with the differences!
and take the printer names to do the following:
  • delete printer if an amendment is to be made
  • re-create printer
So that the printers on the local and prod server are in sync!Smilie

Once the printer is deleted, the creation of the printer requires so many variables. E.g printer-name,IP address,port,filter,location etc

---------- Post updated at 09:43 ---------- Previous update was at 09:00 ----------

would my debugging be like:
Code:
if [[ -n $printer ]];then
f_delete
fi
if [[ -n $printer ]];then 
f_create
fi



---------- Post updated at 13:31 ---------- Previous update was at 09:43 ----------

Ok made changes, calling functions
is there something amiss
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 arr ) ) {print} }' sun8-printers.txt > diffs.txt
while read printer
do
printer="$(echo $printer | cut -d: -f1)"
echo $printer
done < diffs.txt
exit 0
#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()
{
printer=' '
f_get_ptr  
read printer
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"_filter -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

Any help will be highly appreciated

---------- Post updated at 13:56 ---------- Previous update was at 13:31 ----------

Did you see changes i made...your assistance will be appreciated..

Last edited by ggoliath; 05-10-2010 at 04:10 AM..
# 10  
Old 05-10-2010
Sorry, I will not debug your script all the time - not sure if other people are up to it. I suggest you change the commands in it that really execute some changes to your system with an echo in front of them to see the output of your script, so you can test it without danger if it is working as you intended.
# 11  
Old 05-11-2010
Adjusted script to run functions.these are the errors i am receiving
Code:
+ 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

any adviceSmilie
# 12  
Old 05-11-2010
This seems to make no sense in my eyes, ie. parsing seems not correct:
Quote:
Originally Posted by ggoliath
printer=device for 5001bl1:
I guess you want something more like:
Code:
printer=5001bl1

?

You get test errors because one side of the parts to be compared is empty. Place echoes on the variables before you test them to make sure they have actually some value in them.

As I already tried to explain - if you have functions you should hand over parameters to them so they can work with them.
You call f_Delete() but without any parameter which could be $printer, if I am not wrong.

Also it makes no sense to me in f_Delete() to set printer=' ' while you could have handed it over when calling it like I described just up there, then you call f_get_ptr() which does nothing but 3 echos and then read $printer again from the user input... Why did you check out $printer then before when working your list???
After read printer you do a r=$? which makes no sense for me at all.

Advice:
  • If you want to use functions - you don't have to if you don't like - try a simple script without doing anything actually to your system but just printing out some echoes, to learn how to hand over parameters to functions etc.
  • Get a clear plan what you want to do and try to complete every step of it one after another, not all of them at once, if you are not familiar with it, no offence.
  • Use some more indention to see what parts belong together in your script. You have an indention of 2 spaces inside the functions, but none at all for themselves nor for the while loop. Makes it a bit cleaner to look at and you and other get a quicker understanding of which blocks belong together.
# 13  
Old 05-11-2010
No offense taken.Yes i am new when using functions-but how else will i learn.
and yes i want the output like
Quote:
printer=5001bl1
but my cut seems to be giving me the wrong output.
Code:
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

I should have posted the newest functions i have, as i made some adjustments
Code:
f_Delete()
{

#read $printer

 if ["$printer" -eq 1 ]; then
      echo "\n\t $printer to be deleted"
      $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
}



---------- Post updated at 14:53 ---------- Previous update was at 11:25 ----------

Still stuck on the same errors..
Code:
+ 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[2]: 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

So any assistance will be highly appreciated
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a UNIX script file with multiple commands

Hi Good morning all, I want to create script file with multiple commands. For ex: pmrep connect is one of the command to connect to repository. pmrep objectexport is another command to export objects to a file. These commands should run sequentially.But when i try to execute this, the first... (4 Replies)
Discussion started by: SekhaReddy
4 Replies

2. Shell Programming and Scripting

Need a script to add mutiple printers in solaris box

Hi , i need to configure around 80 printers in a server. can someone please help me with the script. i have a file that has printer name and its ip. like. printer1 1.1.1.1 printer2 0.0.0.0 and so on.. can some one please help me to do it via script. i am using solaris 10 ... (0 Replies)
Discussion started by: chidori
0 Replies

3. UNIX for Dummies Questions & Answers

Unix script to create a backup on a remote computer

Hello, I have a problem - I attended a UNIX course a couple of years back but, unfortunately, I don't remember how to write scripts for shell commands. Now I want to make a script that makes a backup of a folder on a remote computer and I have no idea how to begin. :D So the idea is that I want... (3 Replies)
Discussion started by: Benedit
3 Replies

4. AIX

AIX Bunch of printers queue creation script - HELP

I'd seek for help on how to create a bunch of printers in AIX 6.x or equal or above in one go – say like I have 35 printers to create in 4 different AIX Nodes every month – I currently create it manually like below:- How can I automatic this creation on all the 4-5 Nodes – not actually automatic... (3 Replies)
Discussion started by: shiv2001in
3 Replies

5. Shell Programming and Scripting

How to create a file with full permission in unix script

In my script, I am creating a file ----> then writting one line (i.e. Timestamp) ----> then FTP'ing. The same script can be executed by many other users. While other users executing this script, they couldn't Over write this one line (i.e. Timestamp) My expectation So I wanted to create a... (2 Replies)
Discussion started by: sbmk_design
2 Replies

6. Shell Programming and Scripting

How to create xls file using unix script

Hi All, I want to create an xls file using shell script. I mean that I have written one script. It is having sql output. So I want to save that sql output into one xls file and I want to send that file as a attachment to my team MAIL_IDs. Can You any guy help me in this? Thanks in... (3 Replies)
Discussion started by: raghu.iv85
3 Replies

7. Shell Programming and Scripting

Script for reporting network printers

Does anyone have a script that would detect (and create a flat-file) of all printing devices (printers/plotters) attached on a network? I need a single script or tool that would allow me to collect a data file for our asset management tool. Example format: manufacturer, model name, serial... (0 Replies)
Discussion started by: glefko
0 Replies

8. UNIX for Advanced & Expert Users

Password protect UNIX printers?

Is there a way to password protect a printer that is on a LAN network? Our security officer said our UNIX printers need to be password protected. Is this possible? I am running Solaris 7 and 8... Thanks in advanced for any suggestions. (4 Replies)
Discussion started by: rtoba
4 Replies

9. Shell Programming and Scripting

How To create Flat Files using Unix Shell Script?

Hi all, How to create Flat Files using Unix Shell Script. The Script is supposed to be sheduled to run at a particular time? Thanks in advance Appu (4 Replies)
Discussion started by: Aparna_k82
4 Replies

10. UNIX for Dummies Questions & Answers

quering unix boxes for attaced printers

Greetings All, I was wanting some info seeing i am a ms geek looking to go into UNIX if i want to query a UNIX box for info regarding printers, LPDs and such source how would i go about this !!!! All comments that would point me in the right direction would be most useful. Cheers (1 Reply)
Discussion started by: seekerO
1 Replies
Login or Register to Ask a Question