lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>For maximum portability, I suggest that one start Unix scripts with the line:
>
>   #! /usr/bin/env lua

Thanks. I think this has been suggest a long time ago in the list, but I
completely forgot about it.

>Please make a note of this trick in Section 8 of the Lua documentation
>titled "Lua Stand-alone".

Will do.

>The bytecode interpreter crashes on bytecode files that start with the
>string "#! /usr/bin/env lua".

It *crashes*? The parser should give a polite message. I tested it and it gave

 error: invalid control char;
   last token read: `0x1B' at line 2 in file `./luac.out'

This message comes from the parser not from the bytecode loader.
The reason is that Lua decides whether a file contains a source or precompiled
code by looking just at the first byte in the file. If it's ESC then the file
is treated as containing precompiled code; otherwise, it's treated as containing
source.

If we changed the code to what you suggested, then we'd have to skip the
first line before deciding whether it's source or binary. Not the end of the
world, but right now I don't see that we'd gain much except making the code
a little bit more complicated. Plus are you really suggesting that people
edit binary files to add the #! line?

Anyway, you call always do a 2 step solution:

 * file runlua
 #!/bin/sh
 sed 1d | /usr/bin/env lua

 * your script
 #!/usr/bin/env runlua
 WHATEVER (source or binary)

The only drawback is that you loose the name of the file in error messages.

>What is the file extension people normally use for Lua bytecode files?

How about .lua? :-) After all, it's transparent whether a file contains
source or precompiled code... (Of course, I understand the need for different
extensions.)
--lhf