Skip to content
Jaskirat Rajasansir edited this page May 15, 2022 · 2 revisions

Environment Variable Manager

This library provides an abstraction for the raw environment variables provided via getenv with caching and parsing of pre-defined variables.

The variables that are loaded and parsed by default are:

  • $QHOME
  • $QLIC
  • $PATH
  • The shared object environment variable for the current Operating System (see os.q for more details)

These variables are loaded and parsed during library initialisation.

.env.loadAllEnvVars[]

If any of the variables cached by this library are updated (via setenv), they can be reloaded into the process by calling this function.

The cache configuration is found at .env.cfg.vars.

q) .env.loadAllEnvVars[]
2021.01.10 16:37:45.306 INFO pid-1171 jas 0 Loading all configured environment variables [ Total: 4 ]
2021.01.10 16:37:45.306 DEBUG pid-1171 jas 0 Loading environment variable [ Variable: QHOME ] [ Parse Function: .convert.stringToHsym ]
2021.01.10 16:37:45.306 DEBUG pid-1171 jas 0 Loading environment variable [ Variable: QLIC ] [ Parse Function: .convert.stringToHsym ]
2021.01.10 16:37:45.306 DEBUG pid-1171 jas 0 Loading environment variable [ Variable: PATH ] [ Parse Function: .env.i.parsePathTypeVar ]
2021.01.10 16:37:45.329 DEBUG pid-1171 jas 0 Loading environment variable [ Variable: LD_LIBRARY_PATH ] [ Parse Function: .env.i.parsePathTypeVar ]

.env.get[envVar]

Queries the specified environment varaible either from the cache or directly via getenv if not pre-configured.

This function will throw an EnvironmentVariableNotDefinedException if the specified environment variable is not cached and getenv returns empty string.

/ Cached variable (folder path)
q) .env.get`QHOME
`:/opt/q/current

/ Cached variable (PATH-type environment variable)
q) .env.get`PATH
`:/home/jas/.local/bin`:/usr/local/sbin`:/usr/local/bin`:/usr/sbin`:/usr/bin`..

/ Un-cached, set variable
q) .env.get`EDITOR
"vim"

/ Un-cached, empty variable
q) .env.get`LD_PRELOAD
'EnvironmentVariableNotDefinedException
  [0]  .env.get`LD_PRELOAD

.env.which[cmd]

Searches for the specified command in the $PATH environment variable and returns the first match. This provides equivalent functionality to the Linux which command.

q).env.which`ls
`:/bin/ls
q)system "which ls"
"/bin/ls"

/ Exception if command not found
q).env.which`something
'CommandNotFoundException
  [0]  .env.which `something
       ^
q)system "which something"
'os
  [1]  \which something
Clone this wiki locally