Strange syntax error, occurs only when script is executed directly

A

Antoon Pardon

I am workin on a solaris 11 machine. The python version is 2.7.6
path to python is /opt/local/bin/python.

These are the 15 first lines of the script:

#! /opt/local/bin/python

class vslice(object):

def __init__(self, fun):
self.fun = fun

def __getitem__(self, inx):
if not isinstance(inx, tuple):
inx = inx,
#if
return self.fun(*inx)
#end __getitem__
#end __vslice__

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Now if I execute the script by explicitly calling the interpreter
everything works fine.

# /opt/local/bin/python /usr/local/bin/ldapwatch /opt/local/log/openldap.log | head
Apr 21 15:12:38 conn=110982 fd=125 ACCEPT from IP=10.0.59.10:46238 (IP=10.0.128.65:389)
Apr 21 15:12:38 conn=110982 op=0 BIND dn="uid=anonymous,ou=local,ou=people,o=global" method=128
Apr 21 15:12:38 conn=110982 op=0 BIND dn="uid=anonymous,ou=local,ou=people,o=global" mech=SIMPLE ssf=0
Apr 21 15:12:38 conn=110982 op=0 RESULT tag=97 err=0 text=
Apr 21 15:12:38 conn=110982 op=1 SRCH base="ou=people,o=global" scope=2 deref=0 filter="(uid=anonymous)"
Apr 21 15:12:38 conn=110982 op=1 SEARCH RESULT tag=101 err=0 nentries=1 text=
Apr 21 15:12:38 conn=110982 op=2 UNBIND
Apr 21 15:12:38 conn=110982 fd=125 closed
====
Apr 21 15:12:57 conn=110983 fd=125 ACCEPT from IP=10.1.28.235:39616 (IP=10.0.128.65:389)

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

However if I call the script directly and want the #! line do its work I get the following error.

# /usr/local/bin/ldapwatch /opt/local/log/openldap.log | head
/usr/local/bin/ldapwatch: line 3: syntax error near unexpected token `('
/usr/local/bin/ldapwatch: line 3: `class vslice(object):'

I have no idea what is going on here. The persmision for /usr/local/bin/ldapwatch look fine:
# ls -l /usr/local/bin/ldapwatch -rwxr-xr-x 1 root root 2092 Apr 22 10:05 /usr/local/bin/ldapwatch


Does anyone have an idea where I should look to fix this?
 
S

Steven D'Aprano

I am workin on a solaris 11 machine. The python version is 2.7.6 path to
python is /opt/local/bin/python.

Are you sure about that? You ought to double check that /opt/local/bin/
python is what you think it is, and not (say) a symlink to a different
binary.

These are the 15 first lines of the script:

#! /opt/local/bin/python

This being Solaris, what happens if you remove the space between the hash-
bang and the path? On Linux it makes no difference, but Solaris tends to
be a bit more idiosyncratic about things like this.



[...]
However if I call the script directly and want the #! line do its work I
get the following error.

# /usr/local/bin/ldapwatch /opt/local/log/openldap.log | head
/usr/local/bin/ldapwatch: line 3: syntax error near unexpected token `('
/usr/local/bin/ldapwatch: line 3: `class vslice(object):'

That's not a Python syntax error, so its something else failing before
Python gets to see it. The only way I can reproduce this is to execute
Python code using sh:

[steve@ando ~]$ cat ./test2
class X(object):
pass

print X
[steve@ando ~]$ ./test2
../test2: line 1: syntax error near unexpected token `('
../test2: line 1: `class X(object):'

Does anyone have an idea where I should look to fix this?

Ask your local Solaris expert :)

This appears to be the same symptoms:

http://www.linuxmisc.com/12-unix-shell/581d028236386dae.htm
 
C

Chris Angelico

This being Solaris, what happens if you remove the space between the hash-
bang and the path? On Linux it makes no difference, but Solaris tends to
be a bit more idiosyncratic about things like this.

I'm pretty sure the POSIX standard stipulates that a space there is
optional. Should be no difference between "#!/" and "#! /" on any
compliant OS. (But I can't right now find a citation for that, so I
may be wrong.)

ChrisA
 
T

Tim Chase

I'm pretty sure the POSIX standard stipulates that a space there is
optional. Should be no difference between "#!/" and "#! /" on any
compliant OS. (But I can't right now find a citation for that, so I
may be wrong.)

I wondered this too, so went researching and found this:

http://en.wikipedia.org/wiki/Shebang_(Unix)#Magic_number

which said that there was actually some contention that the space was
required, but that the specs state that both with-and-without are
permissible.

-tkc
 
C

Chris Angelico

I wondered this too, so went researching and found this:

http://en.wikipedia.org/wiki/Shebang_(Unix)#Magic_number

which said that there was actually some contention that the space was
required, but that the specs state that both with-and-without are
permissible.

Yeah, I read the Wikipedia page, but was looking for a citable
standards document, and didn't find one. (Which may just mean I didn't
look hard enough. Wasn't going to spend an hour on it.)

ChrisA
 
A

Andrew Cooper

I'm pretty sure the POSIX standard stipulates that a space there is
optional. Should be no difference between "#!/" and "#! /" on any
compliant OS. (But I can't right now find a citation for that, so I
may be wrong.)

ChrisA

man execve

4.3BSD implicitly mandates a space given its description of the shebang
line, while POSIX simply implies the presence of a space.

I have recently had to check this point. All current kernel
implementations I cared to check would strip whitespace around the
interpreter, although Solaris was not one such implementation.

~Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top