error running shell script

how can i fix this error.

+ f_Delete
resync-printers.ksh[2]: test: argument expected
lpadmin: Expected printer or class after '-x' option!
resync-printers.ksh[7]: test: argument expected
+ f_Create
resync-printers.ksh[2]: [0:  not found
lpadmin: The printer-uri must be of the form "ipp://HOSTNAME/printers/PRINTERNAME
".
         Printer  created

trying to execute a function

Hi.

You need spaces either side of [ and ] in a test.

It would be easier if you posted the code instead of just the error.

ok here is code. calling a function

f_Delete()
{
 if ["$printer" -ne 1 ]; then
      echo "\n\t $printer to be deleted"
      return 0
   else
      $BIN/lpadmin -x $printer
      if [ $printer = " ${printer}" ]; then
      echo "\n\t Deleting printer $Printer !!!"
      return 1
      fi
 fi
}

I suspect that there is some syntax errors within this function but not sure where?

There is a missing space after [ in your first test :

 if [ "$printer" -ne 1 ]; then

I don't understand what you try to do with this test :

      if [ $printer = " ${printer}" ]; then

Jean-Pierre.

Hi.

As I said, you need spaces around [ and ]

if ["$printer" -ne 1 ]

What is

if [ $printer = " ${printer}" ]; then

supposed to be doing?

Good, added the space and it worked...but not doing the first function by deleting the printer with lpadmin -x command.
i did use the ticks as its a unix command

f_Delete()
{
 if [ "$printer" -ne 0 ]; then
      echo "\n\t Printer to be deleted"
      return 0
   else
      `$BIN/lpadmin -x $printer`
      echo "\n\t Deleting printer $Printer !!!"

Here is the current error received when running the script:

+ echo 0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
+ f_Delete
resync-printers.ksh[2]: 0117bd1^J 2201bl7^J 5001bl1: bad number
         Deleting printer  !!!

what is the bad number?????:confused:

$printer isn't a number?

Perhaps should be

if [ -z "$printer" ]; then
  ...
else
  $BIN/lpadmin .....
fi

(you don't need the backticks around $BIN/lpadmin)

what is $printer here ?

---------- Post updated at 02:43 PM ---------- Previous update was at 02:42 PM ----------

what is $printer ?

the printer we want to delete or re-create

---------- Post updated at 11:53 ---------- Previous update was at 11:15 ----------

the f-delete dunction not deleting the printers
should i have a three calls to delete the three printers????????:confused:

 
+ echo 0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
+ f_Delete
lpadmin: The printer or class was not found.
         Deleting printer  !!!

---------- Post updated at 12:00 ---------- Previous update was at 11:53 ----------

the f-delete dunction not deleting the printers
should i have a three calls to delete the three printers????????

f_Delete()
{
 if [ -z "$printer" ]; then
      echo "\n\t Printer to be deleted"
      return 0
   else
      $BIN/lpadmin -x $printer
      echo "\n\t Deleting printer $Printer !!!"
      return 1
 fi
}

Or a for-loop:

f_Delete()
{
 if [ -z "$printer" ]; then
      echo "\n\t Printer to be deleted"
      return 1
   else
      for P in $printer; do
        echo "Deleting printer $P..."
        $BIN/lpadmin -x $P
      done
      return 0
 fi
}

Not sure why you need the surrounding if-statement though

so that i could call the variable $printer...
will it effect my function in any way if i remove it?????

---------- Post updated at 12:19 ---------- Previous update was at 12:10 ----------

made adjustments as suggested, but its not deleting the three printers!!!!!!!

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
}

this is the errors!!!

+ echo 0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
+ f_Delete

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

         Deleting printer  !!!

         Deleting printer  !!!

Why is not picking up my three printers and deleting them????

Variables in UNIX, as in most languages are case-sensitive.

$Printer

is not the same as

$printer

In any case...

This:

echo "\n\t Deleting printer $Printer !!!"

Should be

echo "\n\t Deleting printer $P !!!"

Can you show that the printers were not deleted?

The error you got is because one of the printers you are trying to delete doesn't exist.

A small change:

f_Delete()
{
 if [ -z "$printer" ]; then
      echo "\n\t Nothing to do!"
      return 1
   else
      for P in $printer; do
        echo "\n\t Deleting printer $P !!!"
        $BIN/lpadmin -x $P 2> /dev/null
      done
      return 0
 fi
}

Hooray, one down one to go!:b:
ran the script, herewith is the output, just like i want it

+ echo 0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
+ f_Delete
         Deleting printer 0117bd1 !!!
         Deleting printer 2201bl7 !!!
         Deleting printer 5001bl1 !!!

Now to recreate the printers deleted!!!
My function to recreate the printers i have as follows:

f_Create()
{
  if [ -z "$printer" ]; then
       echo "\n\t $printer does not exists"
       return 1
    else
    for P in $printer; do
    echo "\n\t Adding printers !!!!"
    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
    $BIN/lpadmin -p "$printer" -E -D "$desc" -L "$loc" -i /u1/cups/mac/"$filt"_fi
lter -v "$driv"://"$ip":"$port"
    done
    echo "\n\t Printer $printer created"
 fi
}

The reason for so many variables, is because we have printers with different drivers, ports,descriptions,locations and filters.
or would it be possible to re-create just these three printers with the info we have in diffs.txt

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

---------- Post updated at 15:46 ---------- Previous update was at 13:56 ----------

script is hanging during execution of create function

#!/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
for i in `cat diffs.txt`
do
printer="$(awk -F: '{ print $1 }' | cut -c 11-18 diffs.txt)"
echo $printer
done
#done < diffs.txt
#Amend,create or delete Printer
f_Delete()
{
 if [ -z "$printer" ]; then
      echo "\n\t Nothing to be deleted!"
      return 1
   else
      for P in $printer; do
      echo "\n\t Deleting printer $P !!!"
      $BIN/lpadmin -x $P 2> /dev/null
   done
      return 0
 fi
}
f_Create()
{
  if [ -z "$printer" ]; then
       echo "\n\t $printer does not exists"
       return 1
    else
    for P in $printer; do
    echo "\n\t Adding printers !!!!"
    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
    $BIN/lpadmin -p "$printer" -E -D "$desc" -L "$loc" -i /u1/cups/mac/"$filt"_fi
lter -v "$driv"://"$ip":"$port"
    done
    echo "\n\t Printer $printer created"
  fi
}
f_Delete
f_Create

Is there problem with syntax within my script..any help will do?????:confused:

As already stated in your other thread for this script, use set -x and set +x for debugging. Place it in this case into your stuck function maybe.

This section of script contains many errors and always leaves $printer blank.

What is it intended to do?

running the script with "ksh -x resync-printers.ksh" from the command line not the same????

---------- Post updated at 07:36 ---------- Previous update was at 07:29 ----------

It cuts the printer name i require, for me to delete and recreate it if necessary
here is the diffs.txt file

If there are errors, any suggestions as to how i can improve it???

---------- Post updated at 09:21 ---------- Previous update was at 07:36 ----------

Can someone please assist me.?:slight_smile:

---------- Post updated at 09:37 ---------- Previous update was at 09:21 ----------

but i am running my script with ksh -x resync-printers.ksh

The -x option specified at the command level (has you did) or inside the script (at main level) has no effect inside functions.
If you want to debug a function, add the set -x command in the function body.

Jean-Pierre.

Where in the function
itried at the beginning of the function but receive a error

+ f_Create
psset: Unknown or ambiguous option `-x'.
psset: Try `--help' for more information.
         Adding printers !!!!
Enter Printer Name: 0117bd1

---------- Post updated at 11:06 ---------- Previous update was at 09:56 ----------

The pressure is mounting, i need some help.
the script just not seem to continue after entering the printer.It stays hung.Any assistance will be highly appreciated.
i have entered the set -x at the start of the function but it does not give me any joy
here is the function i am trying to execute

f_Create()
{
set -x
  if [ -z "$printer" ]; then
         echo "\n\t $printer does not exists"
       return 1
    else
       for P in $printer; do
        echo "\n\t Adding printers !!!!"
    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
    $BIN/lpadmin -p "$printer" -E -D "$desc" -L "$loc" -i /u1/cups/mac/"$filt"_fi
lter -v "$driv"://"$ip":"$port"
    done
    echo "\n\t Printer $printer created"
  fi
}
for i in `cat diffs.txt`
do
printer="$(awk -F: '{ print $1 }' | cut -c 11-18 diffs.txt)"
echo $printer
done 

Back to the beginning of the script. Correction to my previous post after trying a different shell and using your sample data.

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

The output is twelve identical lines:

0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1
0117bd1 2201bl7 5001bl1

The reason there are 12 lines is because $i contains the entire contents of diffs.txt without the line terminators. There are twelve distinct space-separated elements. The variable $i is not referred to at all in the script so all that happens is the "printer=" line is executed 12 times. I cannot explain how the "printer=" line outputs anything but when inside a shell script it does.

Anyway the "for" loop is not required or desirable.
If we remove the loop completely and make sure that awk has a filename parameter, it now works:

printer="$(awk -F: '{ print $1 }' diffs.txt | cut -c 11-18)"
echo $printer

0117bd1 2201bl7 5001bl1

Okay, we now have the (unchanged) situation where $printer contains 3 printer names. This is not desirable when we look at the contents of f_Create.

Suggest you change the "printer=" line above to use a different variable name (e.g. "printer_list="), then look at where you want a list against where you want a single printer.

The f_Create function contains "read printer". This overwrites the variable $printer. The "read printer" line is not required - it is probably what is hanging the script because the input channel is already tied up with the "for" loop.
Further correction: The shell "read" statements conflict with the "for" loop. I think this script needs a redesign.

Footnote. To answer your earlier question it would be possible to automate the whole process but only if your diffs.txt file contains all the information in (which it does not) in a suitably delimeted format.

Thanks methyl..
Your input has been of great help...I have made the changes as you suggested.
I have changed the create function by removing the read $printer
The function now does create my printers from the #printer_list EXAMPLES

+ f_Create
+ [ -z  0117bd1
 2201bl7
 5001bl1 ]
+ echo \n\t Adding printers !!!!
         Adding printers !!!!
+ /usr/lbin//lpadmin -p 0117bd1 -E -D  -L  -i /u1/cups/mac/_filter -v ://:
lpadmin: No such file or directory
+ echo \n\t Adding printers !!!!
         Adding printers !!!!
+ /usr/lbin//lpadmin -p 2201bl7 -E -D  -L  -i /u1/cups/mac/_filter -v ://:
lpadmin: No such file or directory
+ echo \n\t Adding printers !!!!
         Adding printers !!!!
+ /usr/lbin//lpadmin -p 5001bl1 -E -D  -L  -i /u1/cups/mac/_filter -v ://:
lpadmin: No such file or directory
+ echo \n\t Printer  created
         Printer  created

but without the required parameters.

lpstat -v 5001bl1
device for 5001bl1: ///dev/null

 
The driver=lpd or socket,Ip address,port=515 or 9100 are vital to create the printers...
I did some research and found something like this

while IFS=":" read printer driver IP port

Can it work from this????????:confused:

---------- Post updated at 14:08 ---------- Previous update was at 08:27 ----------

Must be a long weekend??????
asisstance will be highly appreciated.:slight_smile: