rsyslog-doc icon indicating copy to clipboard operation
rsyslog-doc copied to clipboard

Clarify how to concatenate two strings together

Open deoren opened this issue 6 years ago • 3 comments

I found this page:

http://www.rsyslog.com/doc/master/rainerscript/expressions.html

which had this to say:

The language supports arbitrary complex expressions. All usual operators are supported. The precedence of operations is as follows (with operations being higher in the list being carried out before those lower in the list, e.g. multiplications are done before additions.

  • expressions in parenthesis
  • not, unary minus
  • *, /, % (modulus, as in C)
  • +, -, & (string concatenation)
  • ==, !=, <>, <, >, <=, >=, contains (strings!), startswith (strings!)
  • and
  • or

but wasn't entirely sure I was reading it right. It appeared (at a glance) to say that the +, - or & operators all were used with string concatenation (I don't know why I initially interpreted it that way), but that didn't make sense.

I turned to Google to see if this had been discussed elsewhere. Ironically Google dropped me one page back in the docs:

http://www.rsyslog.com/doc/master/rainerscript/data_types.html

where I found this:

RainerScript is a typeless language. That doesn’t imply you don’t need to care about types. Of course, expressions like “A” + “B” will not return a valid result, as you can’t really add two letters (to concatenate them, use the concatenation operator &).

I'll return to testing rsyslog changes, but once I wrap up there I'll circle back to this and submit a PR in an effort to add clarification to the relevant section(s). I'm sure they're probably clear to someone else, but I'll try to improve the wording so that I might have understood them at first glance.

deoren avatar Mar 06 '18 04:03 deoren

The braces "bind" to the token directly in front of them. Or said differently: commas matter and delimit tokens and their descriptions.

For the concrete question:

+, -, & (string concatenation)

is

+ [defined as usual, so no further explanation]
- [defined as usual, so no further explanation]
& (string concatenation) [further explanation given to make sure the right meaning is understood]

It's probably developer/math guy brevity ;-)

rgerhards avatar Mar 06 '18 07:03 rgerhards

@rgerhards Thanks for the feedback. Can you confirm whether this is the intended syntax for the & operator?

set $.ssh-login!result = "failed";
set $.email-notification!subject!topic = $.ssh-login!result & " SSH login attempt";

deoren avatar Mar 06 '18 08:03 deoren

In case anybody else is confused about @rgerhards answer, I think his comment about the braces binding to the token in front of them is regarding the documentation, not the RainerScript syntax itself (i.e. (string concatenation) "binds" to the & operator in the description of the expressions).

So + and - are just the arithmetic operators as used in other programming languages, while & is the sole string concatenation operator. @deoren's snippet is the intended syntax for string concatenation.

jbaiter avatar Aug 02 '19 13:08 jbaiter