From 4e021bc14ba98fac5c037b2219dfca7386ea62c4 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 31 May 2021 21:39:00 -0400 Subject: [PATCH] make section placement stable. slightly less verbose output --- cedit | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/cedit b/cedit index 60bb471..dfee11d 100755 --- a/cedit +++ b/cedit @@ -67,21 +67,19 @@ section. cedit is short for config edit. [[ -w $file ]] || s=sudo + local in_section=false if $exists; then + local tailn=1 local temp="$(mktemp -d)/$file_name" cp "$file" "$temp" cp /dev/null "$file" - local in_section=false while IFS= read -r line; do - if [[ $line == $begin ]]; then + tailn=$(( tailn + 1 )) + if [[ $line == "$begin" ]]; then in_section=true; + break fi - if ! $in_section; then - printf '%s\n' "$line" >> $file - fi - if [[ $line == $end ]]; then - in_section=false; - fi + printf '%s\n' "$line" >> $file done < "$temp" fi @@ -93,6 +91,21 @@ section. cedit is short for config edit. $s tee -a "$file" >/dev/null <<<"$end" fi + if $exists && $in_section; then + while IFS= read -r line; do + if [[ $line == "$begin" ]]; then + in_section=true; + fi + if ! $in_section; then + printf '%s\n' "$line" >> $file + fi + if [[ $line == $end ]]; then + in_section=false; + fi + done < <(tail -n +$tailn "$temp") + fi + + if ! $exists; then ret=1 if $verbose; then @@ -106,8 +119,8 @@ section. cedit is short for config edit. echo "backup of original at $temp" echo diff -u "$temp" "$file": echo "$diff" - elif $verbose; then - echo "No changes made to $file" + #elif $debug; then + # echo "No changes made to $file" fi else # for systems like openwrt which don't have diff -- 2.30.2