Skip to content

Commit

Permalink
Improve logic of lineinfile function
Browse files Browse the repository at this point in the history
When state = absent and file doesn't exist, it is a no-operation.
  • Loading branch information
jan-cerny committed Jul 19, 2019
1 parent 09978d7 commit f85eaf0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions shared/bash_remediation_functions/include_lineinfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,18 @@ function lineinfile() {
local insert_before="$7"
local insensitive="${8:-true}"

[ ! -e "$path" ] && [ "$create" != "true" ] && die "Path '$path' wasn't found on this system and not creating. Refusing to continue."
[ ! -e "$path" ] && [ "$create" == "true" ] && touch "$path"

if [ "$state" == "absent" ]; then
lineinfile_absent "$path" "$regex" "$insensitive"
if [ "$state" == "absent" ] ; then
if [ -e "$path" ] ; then
lineinfile_absent "$path" "$regex" "$insensitive"
fi
elif [ "$state" == "present" ]; then
if [ ! -e "$path" ] ; then
if [ "$create" == "true" ] ; then
touch "$path"
else
die "Path '$path' wasn't found on this system and option 'create' is set to '$create'. Refusing to continue."
fi
fi
lineinfile_present "$path" "$regex" "$line" "$insert_after" "$insert_before" "$insensitive"
fi
}
Expand Down

0 comments on commit f85eaf0

Please sign in to comment.