From: Ian Kelling Date: Tue, 1 Jun 2021 01:39:00 +0000 (-0400) Subject: make section placement stable. slightly less verbose output X-Git-Url: https://iankelling.org/git/?a=commitdiff_plain;h=4e021bc14ba98fac5c037b2219dfca7386ea62c4;p=cedit make section placement stable. slightly less verbose output --- 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