add quiet flag
[cedit] / cedit
diff --git a/cedit b/cedit
index 60bb4719aebf9729230b8b05a08ca8b30bb70667..62331b48c85808fefb44d1866cfec7218484784c 100755 (executable)
--- a/cedit
+++ b/cedit
@@ -24,6 +24,7 @@ The section is #comment delimited. Reads STDIN for the contents of the
 section. Without SECTION_NAME, it acts on a global unnamed
 section. cedit is short for config edit.
 
+-q         Quiet
 -v         Verbose
 -b         Keep backup file
 -h|--help  Help"
@@ -32,10 +33,12 @@ section. cedit is short for config edit.
   local exists=true
   local verbose=false
   local backup=false
+  local quiet=false
 
   case $1 in
     -b) backup=true; shift ;;
     -v) verbose=true; shift ;;
+    -q) quiet=true; shift ;;
     -h|--help) echo "$help"; return ;;
   esac
 
@@ -67,21 +70,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 +94,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
@@ -102,12 +118,12 @@ section. cedit is short for config edit.
   elif type -t diff &>/dev/null; then
     diff=$(diff -u "$temp" "$file")
     ret=$?
-    if (( $ret )); then
+    if (( $ret )) && ! $quiet; then
       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
@@ -117,8 +133,8 @@ section. cedit is short for config edit.
       echo "$diff"
     fi
   fi
-  if ! $backup; then
-    rm -r $"$temp"
+  if ! $backup && $exists; then
+    rm -r "$temp"
   fi
   return $ret
 }