Complex: bash, using ANSI-C quotes in skript

Hello,

I hope someone can hep with this. I use a skript to send multiline Data to a Monitoring system. Bu I'm not able to use linebreaks or escape sequences.

The skript is simple like that:

#!/bin/bash
var="Erste Zeile \n zweite Zeile \n Dritter Teil" 
zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k "$zabitem" -o "$var"

This works - but without the linebreaks. If I use the command on commandline with:

zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k "$zabitem" -o $'Erste Zeile \n zweite Zeile \n Dritter Teil'

the result is fine!

The question ist - how can I use the $'string' with string as a variable (like $var).

Thank you and best regards
mpm

try:

var='Erste Zeile \n zweite Zeile \n Dritter Teil'

Use of single quotes setting the variable so the backslash is not interpreted until the variable is used.

Welcome to the forum.

Why not try

var=$'Erste Zeile \n zweite Zeile \n Dritter Teil'

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

Please give REPRESENTATIVE examples. The following works for me:

vara=$'Erste Zeile \n zweite Zeile'
varb=$'\n Dritter Teil'
var="$vara$varb"
echo "$var"
Erste Zeile 
 zweite Zeile
 Dritter Teil

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?

e.g. a line in an input file?

Hello,

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.

1 Like

To give you more details:

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:

vara="$line"

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.

Did you consider the $'\n' bash ism? Try

outline="$outline" $'\n' "$line"
1 Like

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 

What's your bash version?

EDIT: Drop the spaces when assigning the variable:

outline="$outline"$'\n'"$line"
1 Like

[LEFT][/LEFT]echo "$BASH_VERSION" = 4.3.48(1)-release

---------- Post updated at 02:25 PM ---------- Previous update was at 02:20 PM ----------

Yea! Great - that did it!!
You solved it!

Glad you got what you need. You can see also how important a spot-on, precise, detailed specification is... once the problem is clear, help is near.

1 Like

Sometimes the easiest solution is to just include literal <newline> characters in your assignments as below:

outline="$outline
$line"

This will work with any shell designed to accept Bourne shell syntax.

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!

Be careful with spaces! Try

foo=bar   baz

You will get an error too!