__line: handle fixed strings correctly, posix gave us -F -x for this case

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
Nico Schottelius 2013-10-29 12:23:07 +01:00
parent 990581a459
commit 95513ab538
4 changed files with 31 additions and 16 deletions

View file

@ -1,6 +1,6 @@
#!/bin/sh
#
# 2012 Nico Schottelius (nico-cdist at schottelius.org)
# 2012-2013 Nico Schottelius (nico-cdist at schottelius.org)
#
# This file is part of cdist.
#
@ -24,16 +24,18 @@ file="/$__object_id"
if [ -f "$__object/parameter/regex" ]; then
regex=$(cat "$__object/parameter/regex")
greparg=""
else
if [ ! -f "$__object/parameter/line" ]; then
echo "Parameter line and regex missing - cannot explore" >&2
exit 1
fi
regex="^$(cat "$__object/parameter/line")\$"
regex="$(cat "$__object/parameter/line")"
greparg="-F -x"
fi
# Allow missing file - thus 2>/dev/null
if grep -q "$regex" "$file" 2>/dev/null; then
if grep -q $greparg "$regex" "$file" 2>/dev/null; then
echo present
else
echo absent

View file

@ -38,7 +38,19 @@ case "$state_should" in
exit 1
fi
echo "echo \"$line\" >> $file"
#echo "echo \"$line\" >> $file"
#line_sanitised=$(cat "$__object/parameter/line" | sed 's/"/\"/g')
# Idea: replace ' in the string:
# '"'"'
# |------> ': end the string
# |-|---> "'": create ' in the output string
# |--> ': continue the string
#
# Replace all \ so \t and other combinations are not interpreted
#
line_sanitised=$(cat "$__object/parameter/line" | sed -e "s/'/'\"'\"'/g" -e 's/\\/\\\\/g')
echo "echo '$line_sanitised' >> $file"
;;
absent)
@ -47,17 +59,16 @@ case "$state_should" in
exit 1
fi
[ "$line" ] && regex="^$line\$"
# Replace all / with \/
regex=$(echo "$regex" | sed 's;/;\\/;g')
echo $regex >&2
greparg=""
if [ "$line" ]; then
regex="$line"
greparg="-F -x"
fi
cat << eof
tmp=\$(mktemp)
sed '/$regex/d' "$file" > \$tmp && cat "\$tmp" > "$file" && rm -f "\$tmp"
grep -v $greparg '$regex' '$file' > \$tmp && cat "\$tmp" > '$file' && rm -f "\$tmp"
eof
#echo "echo q | ex -c \"/${line}/d|w|q\" \"${file}\""
;;
*)
echo "Unknown state: $state_should" >&2

View file

@ -32,11 +32,11 @@ regex::
given line, if the given regular expression does not match.
In case of absent, ensure all lines matching the
regular expression are absent (cannot be combined with
the line parameter, if state is absent).
regular expression are absent.
If the regular expression contains / (slashes), they need
to be escaped with \ (backslash): / becomes \/.
The regular expression is interpreted by grep.
Must not be combined with line, if state is absent.
file::
If supplied, use this as the destination file.
@ -64,9 +64,10 @@ __line legacy_timezone --file /etc/rc.conf --regex 'TIMEZONE=.*' --state absent
SEE ALSO
--------
- cdist-type(7)
- grep(1)
COPYING
-------
Copyright \(C) 2012 Nico Schottelius. Free use of this software is
Copyright \(C) 2012-2013 Nico Schottelius. Free use of this software is
granted under the terms of the GNU General Public License version 3 (GPLv3).

View file

@ -6,6 +6,7 @@ Changelog
2.3.6:
* New Type: __locale
* Type __line: Ensure special characters are not interpreted
2.3.5: 2013-10-10
* Core: Unit test fix for remote_copy (Steven Armstrong)