我试图在读取 lua 脚本中的模块文件时读取模块文件中给出的 PATH 变量的值。我不确定这是否可以使用 lua 中的某些函数来完成,因为我对 lua 还很陌生。
模块文件 (netcdf) 下面仅给出了模块文件的一部分 -
set application netcdf
set version 4.1.1
set machine kgb
set app_base /sw/$machine/$application/$version
module-whatis "Sets up environment to use serial netcdf"
if [ module-info mode whatis ] {
break
}
#vvvvv If if not a library, remove this part vvvvv
if [ is-loaded intel ] {
set app_build "centos6.2_intel12"
} elseif [ is-loaded gcc ] {
set app_build "centos6.2_gnu4.4.6"
break
} elseif [ is-loaded pgi ] {
set app_build "centos6.2_pgi12.3"
break
} else {
puts stderr "You must have a programming environment loaded to use this module"
break
}
#^^^^^ If if not a library, remove this part ^^^^^
# This assumes something like --prefix=$SW_BLDDIR
set app_path $app_base/$app_build
setenv NETCDF_DIR "$app_path"
setenv NETCDF_INCLUDE "$app_path/include"
setenv NETCDF_LIB "$app_path/lib"
#setenv NETCDF_LINK "-I${FOO_INCLUDE} -L${FOO_LIB} -lfoo"
prepend-path PATH "$app_path/bin"
prepend-path LD_LIBRARY_PATH "$app_path/lib"
我正在阅读这个文件,有没有什么方法可以得到 PATH 的所有三个可能的组合值,无论用户/系统的环境如何,即
- PATH = /sw/kgb/netcdf/4.1.1/centos6.2_intel12/bin
- PATH = /sw/kgb/netcdf/4.1.1/centos6.2_gnu4.4.6/bin
- PATH = /sw/kgb/netcdf/4.1.1/centos6.2_pgi12.3/bin
我编写的代码仅读取该行,但很难排列变量中的值并获得所需的 PATH。
Lua 代码 -
-- reading module file
local mfile = v.file
local lines = lines_from(mfile)
-- print all line numbers and their contents
for k ,v in pairs(lines)do
print('line['..k..']',v)
end
-- see if file exists
function file_exists(file)
local f = io.open(file,"rb")
if f then f:close() end
return f~= nil
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exists
function lines_from(file)
if not file_exists(file) then return {} end
local lines = {}
for line in io.lines(file) do
if (string.match(line, 'set') or string.match(line,'prepend'))then
lines[#lines+1] = line
end
end
return lines
end
我得到的输出只是显示了我需要的感兴趣的行,但是获得 PATH 的所有可能值仍然遥不可及,任何帮助将不胜感激!
-K