7

I would like to print a list of all environment variables and their values. I searched the Stackoverflow and the following questions come close but don't answer me:

Unlike C, Lua doesn't have envp** parameter that's passed to main() so I couldn't find a way to get a list of all environment variables. Does anybody know how I can get the list of the name and value of all environment variables?

Community
  • 1
  • 1
AlexStack
  • 16,766
  • 21
  • 72
  • 104

5 Answers5

5

Standard Lua functions are based on C-standard functions, and there is no C-standard function to get all the environment variables. Therefore, there is no Lua standard function to do it either.

You will have to use a module like luaex, which provides this functionality.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 4
    @AlexStack: Not quite. `argv` refers to the command-line arguments passed to the program. `envp` refers to environment variables, but it's non-standard. – Keith Thompson Mar 04 '13 at 18:39
2

You can install the lua-posix module. Alternatively, RedHat installations have POSIX routines built-in, but to enable them, you have to do a trick:

  cd /usr/lib64/lua/5.1/
  # (replace 5.1 with your version)
  ln -s ../../librpmio.so.1 posix.so
  # (replace the "1" as needed)
  lua -lposix
  > for i, s in pairs(posix.getenv()) do print(i,s,"\n") end

The trick is in creating a soft-link to the RPM's "io" directory and to naming the soft-link the same name of the library LUA will attempt to open. If you don't do this, you get:

./librpmio.so: undefined symbol: luaopen_librpmio

or similar.

Otheus
  • 566
  • 5
  • 7
2
local osEnv = {}

for line in io.popen("set"):lines() do 
  envName = line:match("^[^=]+")
  osEnv[envName] = os.getenv(envName)
end

this would not work in some cases, like "no valid shell for the user running your app"

2

This code was extracted from an old POSIX binding.

static int Pgetenv(lua_State *L)        /** getenv([name]) */
{
    if (lua_isnone(L, 1))
    {
        extern char **environ;
        char **e;
        if (*environ==NULL) lua_pushnil(L); else lua_newtable(L);
        for (e=environ; *e!=NULL; e++)
        {
            char *s=*e;
            char *eq=strchr(s, '=');
            if (eq==NULL)       /* will this ever happen? */
            {
                lua_pushstring(L,s);
                lua_pushboolean(L,0);
            }
            else
            {
                lua_pushlstring(L,s,eq-s);
                lua_pushstring(L,eq+1);
            }
            lua_settable(L,-3);
        }
    }
    else
        lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
    return 1;
}
lhf
  • 70,581
  • 9
  • 108
  • 149
  • well, this is one way to solve the problem using C. Apparently there's no pure-Lua function that does this. Thanks anyway. – AlexStack Oct 07 '11 at 14:24
  • 2
    @AlexStack, as mentioned by Nicol, there is no pure-Lua function because `environ` is not ANSI C, just POSIX. – lhf Oct 07 '11 at 14:48
1

An easy 2 liner:

    buf = io.popen("env", '*r')
    output = buf:read('*a')
    print(output) -- or do whatever
Mysterion
  • 11
  • 1