metadata - How to detect if a Bash environment variable has been read? -


i have script supplied configuration via environment variables. because configuration manually edited possible typos , variables never used added configuration.

is there way obtain meta-data variable allow post-execution step warn if variable had not read?

acceptable answers include,

  • a way count how many times var has been read
  • a flag true if var has been read
  • a way determine last time var accessed

there may other acceptable answers not listed above.

this problem has arisen in part because bash default untyped.

you have 2 questions in post. first question:

how detect if bash environment variable has been read?

even though none of acceptable answers include mine, answer first question benefit of others:

nohow, except modifying bash.

you're free reject of course.

second question:

is there way obtain meta-data variable allow post-execution step warn if variable had not read?

asking question doesn't make sense, since possible answers not included in acceptable answer list. i'll answer anyway:

no, except if bash modified.

now, though might uninterested in opinion not within list of acceptable answers, i'm going present anyway exercise me , maybe useful information others.

first of all, making warning in case useless, after execution. whatever mistakes made in providing parameters, have taken effect time, corrupted data or time lost. moreover, people not take notice of warnings if in opinion result of script's work ok. might find wrong later, might late.

if you're going validate parameters, before execution , abort if they're wrong.

one way ensure there no typos in environment variable names create namespace them requiring names prefixed fixed string , verifying variable names having prefix known program. however, doesn't protect typos in prefix string. still, no other program know of , means hardly expects such behavior program. violates "rule of least surprise" and, i'd say, unnecessarily so.

i suggest not validate environment variable names @ all. if program has complex configuration, requires validation, don't put environment. put configuration file, not shared other programs.

one easy way make configuration file sourced shell script sets bunch of variables. allow easy "parsing" (as bash it), , validation. validate it, source in subshell (so main shell not affected), output names of variables set, filtering out variables set before sourcing, compare them list of known configuration variable names.

something this:

function list_vars() {     declare -p | awk -f'[ =]' '/^declare/ {print $3}' }  function unset_vars() {     while read v;         unset "$v" 2>/dev/null;     done < <(list_vars); }  declare -a conf_var_names=(foo bar baz)  extra_vars=`     (         # unset variables can unset (and set)         unset_vars         # list variables set after sourcing configuration         (. conf_file.sh; list_vars) |             # remove variables set before sourcing             grep -v -f -f <(list_vars)     ) |         # remove known variables         grep -v -f -f <(ifs=$'\n'; echo "${conf_var_names[*]}") `  if [ -n "$extra_vars" ];     echo "unknown variables set configuration script: $extra_vars" >&2     exit 1 fi 

you may want special handling path , other important variables, though. may idea set variables read-only before sourcing, when validating.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -