[__line] Ensure the line is only added once

This commit is contained in:
Dennis Camera 2020-01-15 17:23:13 +01:00
parent 3258fc98e1
commit 93ec4b46aa
2 changed files with 14 additions and 11 deletions

View file

@ -18,6 +18,17 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
# #
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
else
file="/$__object_id"
fi
if [ ! -f "$file" ]; then
echo "file_missing"
exit 0
fi
if [ -f "$__object/parameter/before" ]; then if [ -f "$__object/parameter/before" ]; then
position="before" position="before"
elif [ -f "$__object/parameter/after" ]; then elif [ -f "$__object/parameter/after" ]; then
@ -33,17 +44,6 @@ else
needle="line" needle="line"
fi fi
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
else
file="/$__object_id"
fi
if [ ! -f "$file" ]; then
echo "file_missing"
exit 0
fi
awk -v position="$position" -v needle="$needle" ' awk -v position="$position" -v needle="$needle" '
function _find(_text, _pattern) { function _find(_text, _pattern) {
if (needle == "regex") { if (needle == "regex") {

View file

@ -1,6 +1,7 @@
#!/bin/sh -e #!/bin/sh -e
# #
# 2018 Steven Armstrong (steven-cdist at armstrong.cc) # 2018 Steven Armstrong (steven-cdist at armstrong.cc)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -104,10 +105,12 @@ BEGIN {
if (anchor && match(\$0, anchor)) { if (anchor && match(\$0, anchor)) {
if (position == "before") { if (position == "before") {
print line print line
add = 0
print print
} else if (position == "after") { } else if (position == "after") {
print print
print line print line
add = 0
} }
next next
} }