Answer the question
In order to leave comments, you need to log in
[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
Severity: FAILURE
Time: 1200928930
Host: myhost.mydomain.org
\n
This is a test notification to demonstrate the format
#!/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
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.
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 questionAsk a Question
731 491 924 answers to any question