mirror of
https://github.com/ovh/debian-cis.git
synced 2025-06-21 18:23:42 +02:00
IMP(8.2.5): find multiline pattern in files (syslog)
Add func to find pattern in file that spreads over multiple lines
The func will remove commented lines (that begin with '#')
and consider the file as one long line.
Thus, this is not possible to look for pattern at beginning of line
with this func ('^' and '$')
Improved pattern in 8.2.5
Add syslog-ng to installed dependencies in Dockerfiles
Fixed multifile arguments when looking for pattern that got broken
in d2bbf754
due to "nocase" and _does_pattern_exist_in_file wrapper
Please note that you can only look for pattern in ONE FILE at once
Fixed 8.2.5 and 8.3.2 with for loop on files and 'FOUND' flag
You now need to specify each and every file to look for or embed a
'find' command as follow :
`FILES="$SYSLOG_BASEDIR/syslog-ng.conf $(find $SYSLOG_BASEDIR/conf.d/)"`
Improved test files
Applied shellcheck recommendations
This commit is contained in:
27
lib/utils.sh
27
lib/utils.sh
@ -114,15 +114,42 @@ _does_pattern_exist_in_file() {
|
||||
if $SUDO_CMD [ -r "$FILE" ] ; then
|
||||
debug "$SUDO_CMD grep -q $OPTIONS -- '$PATTERN' $FILE"
|
||||
if $($SUDO_CMD grep -q $OPTIONS -- "$PATTERN" $FILE); then
|
||||
debug "Pattern found in $FILE"
|
||||
FNRET=0
|
||||
else
|
||||
debug "Pattern NOT found in $FILE"
|
||||
FNRET=1
|
||||
fi
|
||||
else
|
||||
debug "File $FILE is not readable!"
|
||||
FNRET=2
|
||||
fi
|
||||
}
|
||||
|
||||
# Look for pattern in file that can spread over multiple lines
|
||||
# The func will remove commented lines (that begin with '#')
|
||||
# and consider the file as one long line.
|
||||
# Thus, this is not possible to look for pattern at beginning of line
|
||||
# with this func ('^' and '$')
|
||||
does_pattern_exist_in_file_multiline() {
|
||||
local FILE="$1"
|
||||
shift
|
||||
local PATTERN="$*"
|
||||
|
||||
debug "Checking if multiline pattern: $PATTERN is present in $FILE"
|
||||
if $SUDO_CMD [ -r "$FILE" ] ; then
|
||||
debug "$SUDO_CMD grep -v '^[[:space:]]*#' $FILE | tr '\n' ' ' | grep -Pq -- "$PATTERN""
|
||||
if $($SUDO_CMD grep -v '^[[:space:]]*#' $FILE | tr '\n' ' ' | grep -Pq -- "$PATTERN" ); then
|
||||
debug "Pattern found in $FILE"
|
||||
FNRET=0
|
||||
else
|
||||
debug "Pattern NOT found in $FILE"
|
||||
FNRET=1
|
||||
fi
|
||||
else
|
||||
debug "File $FILE is not readable!"
|
||||
FNRET=2
|
||||
fi
|
||||
}
|
||||
|
||||
add_end_of_file() {
|
||||
|
Reference in New Issue
Block a user