On 11 December 2010 Patrick Gundlach wrote:
Hi,
when I have a script that starts with the line
#!/usr/bin/env texlua
I have on my Mac OS X system the full path to my script in arg[0]. Is this portable? That is, if I run this under windows/solaris/... will I always find out where this script is located?
Hi Patrick, these are two different questions. First, the #! line works everywhere except on Windows. It finds the interpreter (not your script) in PATH. There is no reasonable alternative on Windows. You have to associate filename extensions with interpreters in the registry. If you are using TeX Live, you can start your script from a wrapper instead.
Or other question: is there a more-or-less portable way to find out the location of a script from inside the script? I'd like to set up package.path to a relative directory such as:
package.path = ....
Try this: ------------------------------------------------------- #!/usr/bin/env texlua dirname, filename = string.match(arg[0], '(.*[/\\])(.*)') print('dirname='..dirname) print('filename='..filename) ------------------------------------------------------- it works as expected on Unix: $ testarg0.lua dirname=/usr/local/bin/ filename=testarg0.lua You will certainly get similar results in TeX Live because the location of your script is determined by the wrapper, and hence by kpathsea, which returns the full path. Please note that dirname found by string.match() in the example above contains the leading directory separator already and that backslashes are supported too. In order to install your script under TeX Live, copy it to TEXMFLOCAL/scripts. It must have one of the extensions .tlu, .texlua, or .lua. Then run texhash. For Unix you have to make the script executable and to create symlinks to the script in all bin/<platform> directories. The symlinks should not have extensions. (ln -s relative/path/to/myscript.lua myscript) For Windows, go to bin/win32 and "cp -p runscript.exe myscript.exe" (or, on Windows itself, "copy /b runscript.exe myscript.exe". I'm not sure about Windows in general. I can imagine that it works too if the extension of your script is associated with the texlua binary in the registry. I suppose that this is necessary if you want to run your script under MikTeX. BTW, whether you are using the extension .tlu, .texlua, or .lua doesn't matter if your script is invoked by a wrapper. However, if you associate extensions with interpreters in the registry, I recommend .tlu and .texlua for scripts which depend on texlua. This avoids nasty problems when people install a generic Lua engine too. Regards, Reinhard -- ---------------------------------------------------------------------------- Reinhard Kotucha Phone: +49-511-3373112 Marschnerstr. 25 D-30167 Hannover mailto:reinhard.kotucha@web.de ---------------------------------------------------------------------------- Microsoft isn't the answer. Microsoft is the question, and the answer is NO. ----------------------------------------------------------------------------