Search code examples
luadir

Lua change current/working dir, Linux (without LFS or any non-std modules)


I need to change current working dir in lua script for execute specific actions, but i have a trouble with this simple task. I write test script test.lua :

os.execute("cd /usr")
os.execute("ls")

But lua test.lua output is:

test.lua

Current dir doesn't change. What's is wrong? And i can't use LFS or any non-std modules.


Thanks to all for explaining it situation. I choose another way : change work dir before run lua script, but i have a lot of troubles with paths which use in scripts and with scripts.


Solution

  • The Lua standard library is intended to be both small and portable. Therefore, it is based on the capabilities of the C-standard library for all but a select few functions. It has no function to change directories; that's why libraries like LFS exist.

    Have you considered that Lua may not be the appropriate language for your needs? If you're doing shell-style scripting work in an environment where you're not allowed to load non-standard modules, perhaps Python or Perl would be better for you. Both of them have extensive standard libraries with a host of features, all without the need to download non-standard modules.

    If you want to do any real shell-style scripting in Lua, you need modules. It's that simple. So you should either use external modules or use a different language.