Thank you - but this does'n woked - the result was:
25409var
---------- Post updated at 08:22 AM ---------- Previous update was at 08:15 AM ----------
thank you - but this only works with hardcoded Text. In my original Programm I've to use a variable and this is why i cant use this.
to be more percise:
#!/bin/bash
vara="Erste Zeile \n zweite Zeile" #This Values are read form a File
varb="\n Dritter Teil"
var="$vara" "$varb"
zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k "$zabitem" -o "$var"
This seems to have several problems. First, you don't put newlines into your varb string. Second, you do the catenation wrong. Actually, you should have received an error from the line which assigns to var.
You say that vara is read from a file, so I assume that it already has the newlines in place (otherwise you do something wrong when reading it). As for varb and var:
vara=$(you_said_that_this_is_read_from_a_file_somehow)
varb=$'\n Dritter Teil' # This is literal
var="${vara}$varb" # This catenates it
Of course this works, but it is not what the OP wanted. Actually, there was nearly the same answer before, and he commented on this already. He said that if he specifies the variables literally (as you did), it works for him too, so this is obviously discussed already.
So I modified his last example to be more close to what he wanted. In what way do you think that it is not representative?
thank you very much! I'm happy to get your support. Maybe it was my fault, because I tryed to use a simplified script to explain.
I'll try to test your examples im my script...
Best regards
mpmichael
I don't think it is "a line", but several lines; at least it follows from his comment. So in any case, he must have some program which extracts a set of lines from the input file and writes these to stdout. Hence, if we catch this with
var1=$\(this_program\)
we already have in var1 a string containing the newlines and are settled. Since we don't know more how he gets his lines for var1, we can not be more concrete, but I think we can safely assume that any extraction of lines from a file must be done with some program.
Then he has another variable (var2) which, from his comment, is a literal string containing newlines.
Finally, he wants to create a third variable which catenates the previous two. While this now is not related to the newline problem anymore, the code he shows for catenation was incorrect, so I fixed that too.
the Program is reading Logfiles (line by line, looking for (multiline) Stacktraces. If a Stacktrace is discoverered I collect the lines
outline="$outline $line" #I used vara and varb in my example
until the trace is finisched. If this happens - I send the trace (in one line but with the escape codes) to zabbix via zabbix_send.
The traces are getting in Zabbix but the escape-codes (\n) are printed and it's one line. From Zabbix site - there is a resolution (ZBXNEXT-2158) - and I try to follow this with my script.
I tested with this script (based on your suggestions):
#!/bin/bash
zabitem="test"
# Schreibe nach Zabbix: zabwrite
zabwrite(){
zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k "$zabitem" -o "$1"
#echo "Zabbix output: $1"
}
#$line is read from File
vara=$'test'
varb=$'\nDritter Teil' # This is literal
var="${vara}$varb" # This catenates it
zabwrite "$var"
the result was:
2018-02-21 15:50:07 test
Dritter Teil
fine!
But my challange is how to bring the value of $line (which is the line from the file) in vara? Like:
It becomes a bit clearer now. You seem to read lines from some text file, which by default won't contain any <new line> chars unless certain merasures taken. You don't use e.g. "command substitution" as rovf suspected. You then want to join those lines into a variable, separated by a <new line> char.
Good Idea - I checked it with my programm - but it was unhappy with the $'n'
The codeline was:
outline="$outline" $'\n' "$line"
the result:
Zeile 88: $'\n': Befehl nicht gefunden
(Command not fund)
I also tried many tricky things but they all didn't work - but I guess the right answer is simple.
Best regards
mpmichael
---------- Post updated at 11:02 AM ---------- Previous update was at 10:56 AM ----------
My latest try was (in a loop):
outline="${outline} $line \n"
result was:
de.mi.SkipCountSkipPolicy$$FastClassBySpringCGLIB$$2be9540e.invoke(<generated>) \n at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) \n at org.s
You have a space between the closing quote after $outline and the dollar sign which introduces the newline escape, so of course you get an error message!