From 641a1b5a0972d848e854092bba8cf2ba86757c11 Mon Sep 17 00:00:00 2001 From: osm0sis Date: Wed, 12 Oct 2016 13:59:45 -0300 Subject: [PATCH] AK2: make section functions less potentially destructive - still unable to do an actual tweak check due to grep being limited to single lines, but.. - have them check to at least ensure there's a match for end string that comes after begin string so the sed should function without any chance of removing more than intended --- anykernel.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/anykernel.sh b/anykernel.sh index 37478c7..9500a2a 100755 --- a/anykernel.sh +++ b/anykernel.sh @@ -121,14 +121,25 @@ replace_string() { # replace_section replace_section() { - line=`grep -n "$2" $1 | head -n1 | cut -d: -f1`; - sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1; - sed -i "${line}s;^;${4}\n;" $1; + begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`; + for end in `grep -n "$3" $1 | cut -d: -f1`; do + if [ "$begin" -lt "$end" ]; then + sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1; + sed -i "${begin}s;^;${4}\n;" $1; + break; + fi; + done; } # remove_section remove_section() { - sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1; + begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`; + for end in `grep -n "$3" $1 | cut -d: -f1`; do + if [ "$begin" -lt "$end" ]; then + sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1; + break; + fi; + done; } # insert_line