make section placement stable. slightly less verbose output
authorIan Kelling <iank@fsf.org>
Tue, 1 Jun 2021 01:39:00 +0000 (21:39 -0400)
committerIan Kelling <iank@fsf.org>
Tue, 1 Jun 2021 01:39:00 +0000 (21:39 -0400)
cedit

diff --git a/cedit b/cedit
index 60bb4719aebf9729230b8b05a08ca8b30bb70667..dfee11d592c78d1e64b7442fa8a003161c621922 100755 (executable)
--- 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