rsyslog/tests/rscript_backticks_braces_envvar.sh
Rainer Gerhards 13ca403ea0
rainerscript: backticks support ${VAR} and adjacent text
This implements brace-style env vars inside backticks with `echo` and
fixes termination of unbraced `$VAR`. Most importantly, variables can
now be adjacent to static text (e.g., `foo${ENV}bar`), which previously
did not work and caused confusion. This aligns behavior with common
bash expectations.

Why
- Users expect `${VAR}` and `$VAR!` to expand while keeping punctuation.
- Concatenations like `foo${ENV}bar` are common and should be valid.

What
- Add support for braced variables `${VAR}` with proper `}` handling.
- For unbraced `$VAR`, stop the name at the first non `[A-Za-z0-9_]`
  char and emit that char (e.g., `!`, `.`) as literal output.
- Improve error handling for overlong env var names.
- Keep other shell features (e.g., `$(pwd)`) unsupported by design.

Docs
- Update `constant_strings.rst` to document `${VAR}`, the new
  termination rules, and examples including `foo${ENV}bar` and `$VAR!`.

Tests
- Add `rscript_backticks_braces_envvar.sh` for `${VAR}` support.
- Add `rscript_backticks_static_text.sh` for `$VAR!` and adjacency.
- Convert `rscript_backticks_empty_envvar-vg.sh` to non-VG variant and
  adjust `tests/Makefile.am`.

Compatibility
- Backward compatible. Changes affect only previously broken edge cases.

Misc
- Refresh lexer copyright years.
2025-08-15 14:43:41 +02:00

21 lines
496 B
Bash
Executable File

#!/bin/bash
## Validate ${VAR} expansion in backticks with static text.
. ${srcdir:=.}/diag.sh init
generate_conf
add_conf '
template(name="outfmt" type="list") {
property(name="msg" field.delimiter="58" field.number="2")
constant(value="\n")
}
if `echo foo${MYVAR}bar` == "foo42bar" and $msg contains "msgnum" then
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
'
export MYVAR=42
startup
injectmsg 0 1
shutdown_when_empty
wait_shutdown
seq_check 0 0
exit_test