HenrikBengtsson / CBI-software

A Scientific Software Stack for HPC (CentOS oriented)
https://wynton.ucsf.edu/hpc/software/software-repositories.html
5 stars 2 forks source link

How to identify Linux distribution CentOS 7 or Rocky 8 #96

Open HenrikBengtsson opened 10 months ago

HenrikBengtsson commented 10 months ago

Bash

unset CBI_LINUX
if [[ -f /etc/os-release ]]; then
   CBI_LINUX=$(grep -E "^PRETTY_NAME=" /etc/os-release | sed -E 's/^PRETTY_NAME="//' | sed 's/ Linux //' | sed -E 's/[ .].*//')
   CBI_LINUX=${CBI_LINUX,,}
fi
unset CBI_LINUX; if [[ -f /etc/os-release ]]; then CBI_LINUX=$(grep -E "^PRETTY_NAME=" /etc/os-release | sed -E 's/^PRETTY_NAME="//' | sed 's/ Linux /_/' | sed -E 's/[ .].*//'); CBI_LINUX=${CBI_LINUX,,}; fi

On CentOS 7, it gives:

$ echo $CBI_LINUX
centos7

and on Rocky 8, it gives:

$ echo $CBI_LINUX
rocky8

Lua

local f = io.open("/etc/os-release", "r")
if f ~= nil then
  f:close()
  for line in io.lines("/etc/os-release") do 
    if (string.sub(line, 1, 13) == 'PRETTY_NAME="') then
      line = string.sub(line, 14, string.len(line))
      line = string.lower(line)
      line = line:gsub(" linux ", "")
      line = line:gsub("[ .].*", "")
      CBI_LINUX = line
      break
    end
  end
end
print(CBI_LINUX)

Lmod

-- Set environment variable CBI_LINUX to 'centos7' or 'rocky8'
if mode() == "load" then
  if os.getenv("CBI_LINUX") == nil then
    if isFile("/etc/os-release") then
      execute{cmd = "CBI_LINUX=$(grep -E '^PRETTY_NAME=' /etc/os-release | sed -E 's/^PRETTY_NAME=\"//' | sed 's/ Linux //' | sed -E 's\
/[ .].*//'); CBI_LINUX=${CBI_LINUX,,}", modeA = {"load"}}
    end
  end
elseif mode() == "unload" then
   execute{cmd = "unset CBI_LINUX", modeA = {"unload"}}
end