Using Lua os.getenv in luCI pages?

I'm trying to read an export'd system environmental variable in a .lua page. It doesn't seem to be actually doing anything though.

   if os.getenv("SHIELD_MODE") == "Router" then 

doesn't do what it's supposed to do (namely, replace a single tab with 2 tabs)

On the device, an export -p gives:

oot@OpenWrt:/usr/lib/lua/luci# export -p
export FIRSTBOOT='1'
export HOME='/root'
export OLDPWD='/usr/lib/lua/luci/sys'
export PATH='/usr/sbin:/usr/bin:/sbin:/bin'
export PWD='/usr/lib/lua/luci'
export SHIELD_MODE='Router'
export SHLVL='3'
export TERM='linux'
export numcores='2'
export serial

I'm new and unwise in the Lua ways, so any instruction would be most appreciated!

The Lua code you posted is correct, but the uhttpd server only exports a limited amount of environment variables to the CGI program and clears the rest of the environment.

So I am afraid your only choice is to either manually read the environment variable from elsewhere, e.g. from /etc/profile or to use the embedded Lua interpreter (uhttpd-mod-lua) which should have access to the full process environment.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.