I am not totally sure what you want to do and maybe othere people did not quite understand it neither so that could be the reason because so many look in but do not answer.
Whatever, there are some lines, I see no sense in:
awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ }}{ if (!($0
in arr ) ) {print} }' sun8-printers.txt
#Amend,create or delete Printer
prExists=$?
You compare 2 files to get the difference and getting the difference, you do nothing with that list. The output is not used any further and also the $? is not worth anything since awk will nearly always give you a 0 back, no matter if there is a difference or not.
I recommend you do a simple diff instead and check it for it's $? to notice if there is a difference or not. If you really need to process the difference list, you might use a simple grep -f against those 2 files so you can a) check it's result with $? and b) if you need the output, process/parse it.
Also your function printerExists() is not closed with a } which will produce an error, maybe just a typo.
So far I got the impression you want to create those print queues that do not exist on some hosts yet.
Then you might want to write your printerExists() so that it can parse a) a printer name, that you might already have gotten by checking which do not exist, b) an action like [add|delete].
Also all those variable inside the function have not been filled with usable values, which could have happened outside this function or in another function so you can hand them over to printerExists() as parameters too.
Maybe get a clear picture of what you want to do 1st and write the script according to that and/or tell what exactly is not working currently.
By taking a glimpse at it there should be several things that might not work, like stated above.
The awk was too compare the two files from a local server and a remote server.Then i added this to get the printer names the two files
while read printer
do
printer="$(echo $printer | cut -d: -f1)"
echo $printer
done < diffs.txt
exit 0
The function was an example, i wanted to use in order to delete,amend or recreate the printers in that order, which i found when doing some research.
I am still stuck with the function to the above
Yes it is true that i want to delete,amend or recreate a printer that is not on the server.
The function I know its not correct as i am stil very much a junior when it comes to shell scripting, functions etc
But i do know the lpadmin commands i will use to create the printers
I agree, the script fragment contains too many syntax and apparent logic errors to follow what the O/P intended.
The (apparently pointless) function starts but does not end. There is no closing } . There is however a spurious "fi" on the last line.
function printerExists()
{
The function itself is never called and I can't see a place where a list of printers is processed.
On the logic front the script appears to be intended to delete all printers on the home machine and create all printers to match the remote computer. Was this the intention?
On the command logic don't you have to issue the sequence: lpshut (and wait until it stops), lpadmin -x, lpsched when deleting a printer?
An example of the printer lists from each computer showing which ones should be deleted and which ones should be created would be useful.
Ok here a basic snippet which you might just want to adopt into your script, supposing you already have the name of the printer to be created, deleted or altered etc.:
...
LOCAL_FILE=file1
REMOTE_FILE=file2
# Not sure which you want to add and which you want to delete, but you can pick it out by this I think:
ADD_QUEUE=`grep -f file1 file2`
DEL_QUEUE=`grep -vf file1 file2`
...
do_queue()
{
# $1 of this function is the queue name
# $2 the type of action
case $2 in
"create") here comes your command to create $1;;
"delete") here comes your comand to delete $1;;
esac
#those commands could be place in another separate function themselves which could be called with parameters of course too, if you like
}
if [[ -n $ADD_QUEUE ]]; then
# first comes the function name, and then it's parameters
do_queue $ADD_QUEUE create
fi
if [[ -n $DEL_QUEUE ]]; then
# first comes the function name, and then it's parameters
do_queue $DEL_QUEUE delete
fi
...
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
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.
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!
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:
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
#!/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..
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.
Adjusted script to run functions.these are the errors i am receiving
+ 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
This seems to make no sense in my eyes, ie. parsing seems not correct:
I guess you want something more like:
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.
No offense taken.Yes i am new when using functions-but how else will i learn.
and yes i want the output like
but my cut seems to be giving me the wrong output.
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
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..
+ 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