# error running shell script

**URL:** <https://community.unix.com/t/error-running-shell-script/265531>\
**Category:** Shell Programming and Scripting\
**Created:** [May 12, 2010, 3:15am UTC](https://community.unix.com/t/error-running-shell-script/265531 "2010-05-12T03:15:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 12, 2010, 3:15am UTC](https://community.unix.com/t/error-running-shell-script/265531/1 "2010-05-12T03:15:38Z")

</div>

how can i fix this error.

```nohighlight
+ 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

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 12, 2010, 3:19am UTC](https://community.unix.com/t/error-running-shell-script/265531/2 "2010-05-12T03:19:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 12, 2010, 3:24am UTC](https://community.unix.com/t/error-running-shell-script/265531/3 "2010-05-12T03:24:05Z")

</div>

ok here is code. calling a function

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [May 12, 2010, 3:57am UTC](https://community.unix.com/t/error-running-shell-script/265531/4 "2010-05-12T03:57:30Z")

</div>

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

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

```

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

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

```

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 12, 2010, 3:58am UTC](https://community.unix.com/t/error-running-shell-script/265531/5 "2010-05-12T03:58:47Z")

</div>

Hi.

As I said, you need spaces around [and]

```nohighlight
if ["$printer" -ne 1]

```

What is

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

```

supposed to be doing?

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 12, 2010, 4:46am UTC](https://community.unix.com/t/error-running-shell-script/265531/6 "2010-05-12T04:46:33Z")

</div>

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

```nohighlight
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:

```nohighlight
+ 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?????😕

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 12, 2010, 4:53am UTC](https://community.unix.com/t/error-running-shell-script/265531/7 "2010-05-12T04:53:02Z")

</div>

$printer isn't a number?

Perhaps should be

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

```

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

---

<div class="post-metadata">

**Author:** ![RahulJoshi](https://community.unix.com/user_avatar/community.unix.com/rahuljoshi/32/1074_2.png) [@RahulJoshi](https://community.unix.com/u/RahulJoshi)\
**Post date:** [May 12, 2010, 5:13am UTC](https://community.unix.com/t/error-running-shell-script/265531/8 "2010-05-12T05:13:35Z")

</div>

what is $printer here ?

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

what is $printer ?

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 12, 2010, 6:00am UTC](https://community.unix.com/t/error-running-shell-script/265531/9 "2010-05-12T06:00:52Z")

</div>

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????????😕

```nohighlight
 
+ 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????????

```nohighlight
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
}

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 12, 2010, 6:03am UTC](https://community.unix.com/t/error-running-shell-script/265531/10 "2010-05-12T06:03:34Z")

</div>

Or a for-loop:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 12, 2010, 6:19am UTC](https://community.unix.com/t/error-running-shell-script/265531/11 "2010-05-12T06:19:37Z")

</div>

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!!!!!!!

```nohighlight
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!!!

```nohighlight
+ 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????

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 12, 2010, 6:46am UTC](https://community.unix.com/t/error-running-shell-script/265531/12 "2010-05-12T06:46:35Z")

</div>

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

```nohighlight
$Printer

```

is not the same as

```nohighlight
$printer

```

In any case...

This:

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

```

Should be

```nohighlight
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:

```nohighlight
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
}

```

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 12, 2010, 9:46am UTC](https://community.unix.com/t/error-running-shell-script/265531/13 "2010-05-12T09:46:27Z")

</div>

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

```nohighlight
+ 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:

```nohighlight
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

```nohighlight
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

```nohighlight
#!/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?????😕

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [May 12, 2010, 10:15am UTC](https://community.unix.com/t/error-running-shell-script/265531/14 "2010-05-12T10:15:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/6_449c533f586939b6e5f246632a46421f.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [May 12, 2010, 12:41pm UTC](https://community.unix.com/t/error-running-shell-script/265531/15 "2010-05-12T12:41:45Z")

</div>

> [@](#):
>
> for i in `cat diffs.txt`  
> do  
> printer="$(awk -F: '{ print $1 }' | cut -c 11-18 diffs.txt)"  
> echo $printer  
> done

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

What is it intended to do?

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 13, 2010, 3:37am UTC](https://community.unix.com/t/error-running-shell-script/265531/16 "2010-05-13T03:37:08Z")

</div>

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

> [@](#):
>
> more 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

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.?🙂

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

> [@zaxxon](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [May 13, 2010, 3:46am UTC](https://community.unix.com/t/error-running-shell-script/265531/17 "2010-05-13T03:46:24Z")

</div>

> [@ggoliath](#):
>
> ---------- Post updated at 09:37 ---------- Previous update was at 09:21 ----------
> 
> [/COLOR]  
> 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.

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 13, 2010, 5:06am UTC](https://community.unix.com/t/error-running-shell-script/265531/18 "2010-05-13T05:06:46Z")

</div>

> [@aigles](#):
>
> 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

```plaintext
+ 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

```plaintext
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
}

```

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/6_449c533f586939b6e5f246632a46421f.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [May 13, 2010, 7:19pm UTC](https://community.unix.com/t/error-running-shell-script/265531/19 "2010-05-13T19:19:33Z")

</div>

```nohighlight
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.

```nohighlight
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:

```nohighlight
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.

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![ggoliath](https://community.unix.com/letter_avatar/ggoliath/32/6_449c533f586939b6e5f246632a46421f.png) [@ggoliath](https://community.unix.com/u/ggoliath)\
**Post date:** [May 14, 2010, 8:08am UTC](https://community.unix.com/t/error-running-shell-script/265531/20 "2010-05-14T08:08:12Z")

</div>

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

```nohighlight
+ 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.

```nohighlight
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????????😕

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

Must be a long weekend??????  
asisstance will be highly appreciated.🙂

[Next page](https://community.unix.com/t/error-running-shell-script/265531.md?page=2)
