Complex: bash, using ANSI-C quotes in skript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex: bash, using ANSI-C quotes in skript
# 8  
Old 02-21-2018
e.g. a line in an input file?
# 9  
Old 02-21-2018
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
# 10  
Old 02-21-2018
Quote:
Originally Posted by RudiC
e.g. a line in an input file?
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.
This User Gave Thanks to rovf For This Post:
# 11  
Old 02-21-2018
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

Code:
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):

Code:
#!/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:

Code:
vara="$line"

# 12  
Old 02-21-2018
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' bashism? Try
Code:
outline="$outline" $'\n' "$line"

This User Gave Thanks to RudiC For This Post:
# 13  
Old 02-21-2018
Good Idea - I checked it with my programm - but it was unhappy with the $'n'
The codeline was:
Code:
outline="$outline" $'\n' "$line"

the result:
Code:
 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):

Code:
outline="${outline} $line \n"

result was:

Code:
de.mi.SkipCountSkipPolicy$$FastClassBySpringCGLIB$$2be9540e.invoke(<generated>) \n at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) \n at org.s


Last edited by mpmichael; 02-21-2018 at 12:04 PM.. Reason: look
# 14  
Old 02-21-2018
What's your bash version?

EDIT: Drop the spaces when assigning the variable:

Code:
outline="$outline"$'\n'"$line"


Last edited by RudiC; 02-21-2018 at 01:36 PM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Complex Bash Script

I have an FTP server with thousands of Invoices. All Invoices are in a folder called /volume1/MBSInvoices/ Monthly invoices are added to that folder every month. Here is a sample filename of the Invoices: invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf the Account ID is the... (6 Replies)
Discussion started by: badr777
6 Replies

2. UNIX for Beginners Questions & Answers

Bash not recognizing single quotes in Mac?

Hi, I just bought a new mac and have been running a program out of terminal, but even early on I noticed that my single quotes looked a lot different from the ones used in all of the namelists and other files of the program. Specifically, mine are kind of slanted whereas the others are very... (7 Replies)
Discussion started by: jtcastro99
7 Replies

3. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

4. Shell Programming and Scripting

How to count number of double quotes in bash

Need a little help. I have just a simple string with a lot double quotes in it. I need to be able to parse through this string, and know how many double quotes I have, and where I am, so I can key off every 9th double quote. For example (coding is not complete): #!/bin/bash count=0... (3 Replies)
Discussion started by: Akilleez
3 Replies

5. UNIX for Dummies Questions & Answers

Is there a way to set ' and " as a non quotes type in bash?

Howdy, I got a script that adds a esc char before all chars interpeted by bash shell but I wan't other solution. Is there a way to set ' and " as a non quotes type in bash (some local variable)? Have found that scsh is a non-quoting type shell but after reading Why I don't use scsh as a scripting... (3 Replies)
Discussion started by: johny_be_good
3 Replies

6. Programming

why the implementatoin of Bakery algorithm in ANSI C does not work in ANSI C

I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry? Only print out: Thread ID: 0 START! Thread ID: 0 END! Thread ID: 0 START!... (2 Replies)
Discussion started by: sehang
2 Replies

7. Shell Programming and Scripting

Howto make ksh skript bash-compatible with backticks

I have the following ksh-script: #!/bin/ksh # Ueberprüfe, ob genau ein Parameter angegeben wurde test "$#" -eq "1" || { echo "USAGE: path_cleanup <PATH_NAME>"; return 1; } # Ueberpruefe, ob awk und nawk installiert sind test -x /bin/nawk || { echo "ERROR: nawk is not installed"; return 1;... (2 Replies)
Discussion started by: doc_symbiosis
2 Replies

8. Shell Programming and Scripting

how to use in bash variables and quotes

I have some troubles with variables and quotes... I want: if $URL is empty (no user input) go to http://www.localhost/index.php/ else add this string (search) "?s=+$URL" EXAMPLE: No user input string= http://www.localhost/index.php/ User input = "unix" string=... (3 Replies)
Discussion started by: aspire
3 Replies

9. Shell Programming and Scripting

Retain quotes from bash script arguments

Hi there, I've been scouring these forums and have found similar threads, but none apparently helped me solved my problem :rolleyes: I'd like to run a command within a bash script, but that command is provided by the user to the script and may contain quotes, which is where the problem lies.... (9 Replies)
Discussion started by: cypression
9 Replies

10. Shell Programming and Scripting

Convert file from Unix - ANSI to PC - ANSI

Hi, I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this? Thanks, Sunil (0 Replies)
Discussion started by: ssmallya
0 Replies
Login or Register to Ask a Question