J
J
juffinhalli2012-10-12 22:52:30
bash
juffinhalli, 2012-10-12 22:52:30

[SOLVED] Explain what this bash script does

Good evening, hackers!

Some formatted text is passed by the daemon via STDIN to the sh script:

#!/bin/sh
     while read x y
     do  case "$x$y" in
             '') exec logger -t collectd${severity+" $severity" -p user."$severity"};;
             Severity:WARNING) severity=warning;;
             Severity:OKAY)    severity=notice;;
             Severity:FAILURE) severity=err;;
         esac
     done

Sample text:
Severity: FAILURE
  Time: 1200928930
  Host: myhost.mydomain.org
  \n
  This is a test notification to demonstrate the format

But I can't figure out what's going on.
I see a cycle ... and then it's not clear.

Please help with the following questions:
What happens (step by step) when executing the script?
How can I pass arbitrary text to this script via STDIN?
How to modify the script so that this arbitrary text is written to a file and not to syslog?

Thanks in advance.

UPD. Thanks for the help, but one point remained unclear - how to redirect all incoming text to the

UPD2 file. Gotta do it like this

#!/bin/sh
read severity
read time
echo $severity >> file
echo $time >> file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Huseynov, 2012-10-12
@juffinhalli

read xy - reads a line, divides it by spaces, as the shell divides command line arguments into separate arguments. The first and second blocks are assigned to the x and y variables, respectively.
Next, the read line is parsed. If the combination Severity:WARNING (OK, FAILURE) is read, then the corresponding value is written to the severity variable. If the string is empty, then logger is started with the required parameters. The rest of the lines are ignored.

S
Skpd, 2012-10-12
@Skpd

Line by line reading. The variable $x gets the first word, $y the second.
Next, the $severity variable is set to a value depending on $x and $y.
When hitting an empty line, a log entry is made.
To write to a file, change line 4 to something like
'') echo "severity: $severity" > test.log;;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question