From a612c049422536786fde20b55470e933f0b521ae Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Thu, 27 Oct 2016 23:13:15 -0700 Subject: [PATCH 01/10] dont keep backups by default --- cedit | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cedit b/cedit index e6d128d..9d37b55 100755 --- a/cedit +++ b/cedit @@ -25,13 +25,16 @@ section. Without SECTION_NAME, it acts on a global unnamed section. cedit is short for config edit. -v Verbose +-b Keep backup file -h|--help Help" local s diff name init local file_dir="$(dirname "$file")" local exists=true local verbose=false + local backup=false case $1 in + -b) backup=true; shift ;; -v) verbose=true; shift ;; -h|--help) echo "$help"; return ;; esac @@ -116,6 +119,9 @@ section. cedit is short for config edit. echo "$diff" fi fi + if ! $backup; then + rm -r $"$temp" + fi return $ret } cedit "$@" -- 2.30.2 From a6992c8fdacb3db1bd68b782a8f301f50629e620 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Sat, 31 Oct 2020 18:04:53 -0400 Subject: [PATCH 02/10] indent and fix logic for printing output --- cedit | 182 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 90 insertions(+), 92 deletions(-) diff --git a/cedit b/cedit index 9d37b55..60bb471 100755 --- a/cedit +++ b/cedit @@ -14,7 +14,7 @@ # limitations under the License. cedit() { - local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE + local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE Create/modify a section in a config file Returns 1 if the file is modified by this command, 2 or higher @@ -27,101 +27,99 @@ section. cedit is short for config edit. -v Verbose -b Keep backup file -h|--help Help" - local s diff name init - local file_dir="$(dirname "$file")" - local exists=true - local verbose=false - local backup=false - - case $1 in - -b) backup=true; shift ;; - -v) verbose=true; shift ;; - -h|--help) echo "$help"; return ;; - esac - - if (( $# == 2 )); then - name=": $1" - shift + local s diff name init + local file_dir="$(dirname "$file")" + local exists=true + local verbose=false + local backup=false + + case $1 in + -b) backup=true; shift ;; + -v) verbose=true; shift ;; + -h|--help) echo "$help"; return ;; + esac + + if (( $# == 2 )); then + name=": $1" + shift + fi + + local file="$1" + local file_name="${file##*/}" + + local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#" + local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#" + + if [[ ! -e $file_dir ]]; then + if ! mkdir -p $file_dir; then + s=sudo + $s mkdir -p $file_dir || return 2 fi - - local file="$1" - local file_name="${file##*/}" - - local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#" - local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#" - - if [[ ! -e $file_dir ]]; then - if ! mkdir -p $file_dir; then - s=sudo - $s mkdir -p $file_dir || return 2 - fi - fi - if [[ ! -e $file ]]; then - exists=false - if ! $s touch $file; then - s=sudo - $s touch $file || return 2 - fi + fi + if [[ ! -e $file ]]; then + exists=false + if ! $s touch $file; then + s=sudo + $s touch $file || return 2 fi - - [[ -w $file ]] || s=sudo - - - if $exists; then - 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 - in_section=true; - fi - if ! $in_section; then - printf '%s\n' "$line" >> $file - fi - if [[ $line == $end ]]; then - in_section=false; - fi - done < "$temp" - fi - - IFS= read -d '' -n 1 -r init - if [[ $init ]]; then - $s tee -a "$file" >/dev/null <<<"$begin" - printf '%s' "$init" | $s tee -a "$file" >/dev/null - $s tee -a "$file" >/dev/null - $s tee -a "$file" >/dev/null <<<"$end" + fi + + [[ -w $file ]] || s=sudo + + + if $exists; then + 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 + in_section=true; + fi + if ! $in_section; then + printf '%s\n' "$line" >> $file + fi + if [[ $line == $end ]]; then + in_section=false; + fi + done < "$temp" + fi + + IFS= read -d '' -n 1 -r init + if [[ $init ]]; then + $s tee -a "$file" >/dev/null <<<"$begin" + printf '%s' "$init" | $s tee -a "$file" >/dev/null + $s tee -a "$file" >/dev/null + $s tee -a "$file" >/dev/null <<<"$end" + fi + + if ! $exists; then + ret=1 + if $verbose; then + echo "New file $file:" + cat "$file" fi - - if ! $exists; then - ret=1 - if $verbose; then - echo "New file $file:" - cat "$file" - fi - elif type -t diff &>/dev/null; then - diff=$(diff -u "$temp" "$file") - ret=$? - if $verbose; then - if (( $ret == 0 )); then - echo "No changes made to $file" - fi - else - echo "backup of original at $temp" - echo diff -u "$temp" "$file": - echo "$diff" - fi - else - # for systems like openwrt which don't have diff - diff=$(cmp "$temp" "$file") - ret=$? - if $verbose; then - echo "$diff" - fi + elif type -t diff &>/dev/null; then + diff=$(diff -u "$temp" "$file") + ret=$? + if (( $ret )); then + echo "backup of original at $temp" + echo diff -u "$temp" "$file": + echo "$diff" + elif $verbose; then + echo "No changes made to $file" fi - if ! $backup; then - rm -r $"$temp" + else + # for systems like openwrt which don't have diff + diff=$(cmp "$temp" "$file") + ret=$? + if $verbose; then + echo "$diff" fi - return $ret + fi + if ! $backup; then + rm -r $"$temp" + fi + return $ret } cedit "$@" -- 2.30.2 From 4e021bc14ba98fac5c037b2219dfca7386ea62c4 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 31 May 2021 21:39:00 -0400 Subject: [PATCH 03/10] 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 From ea111cb6cc7fed09fc520aeca3a43fe194387688 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 31 May 2021 23:03:39 -0400 Subject: [PATCH 04/10] fix rm of nothing --- cedit | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cedit b/cedit index dfee11d..9b21969 100755 --- a/cedit +++ b/cedit @@ -130,8 +130,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 } -- 2.30.2 From 380c58f9dea11ac15a68e1cad81294b242ed0ae7 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 31 May 2021 23:03:46 -0400 Subject: [PATCH 05/10] add basic manual test --- test/test | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 test/test diff --git a/test/test b/test/test new file mode 100755 index 0000000..76eaea4 --- /dev/null +++ b/test/test @@ -0,0 +1,39 @@ +#!/bin/bash + +cd $(mktemp -d) + +cat >f <<'EOF' +this +little +EOF + + +cedit -v f <<'EOF' +piggy +went +to +market +EOF + + +cedit -v sec2 f <<'EOF' +another little piggy +stayed home +EOF + +cat >>f <<'EOF' +wee wee wee +EOF + +cedit -v f <<'EOF' +piggy +went +to +market +EOF + + +cedit -v sec2 f <<'EOF' +another little piggy +stayed home +EOF -- 2.30.2 From 97f167fc565b8393b38196d2c29a870dd911cac5 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Wed, 15 Dec 2021 14:33:22 -0500 Subject: [PATCH 06/10] add quiet flag --- cedit | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cedit b/cedit index 9b21969..62331b4 100755 --- 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 @@ -115,7 +118,7 @@ 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" -- 2.30.2 From ea4ea4beebbe3d42e3016c8db246e8ad7d4aa20e Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Fri, 13 Jan 2023 17:52:07 -0500 Subject: [PATCH 07/10] add silent mode --- cedit | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/cedit b/cedit index 62331b4..e6d373c 100755 --- a/cedit +++ b/cedit @@ -25,20 +25,23 @@ section. Without SECTION_NAME, it acts on a global unnamed section. cedit is short for config edit. -q Quiet +-s Silent. Quiet and exit 0 on modified file. -v Verbose -b Keep backup file -h|--help Help" - local s diff name init - local file_dir="$(dirname "$file")" - local exists=true - local verbose=false - local backup=false - local quiet=false + local s diff name init file_dir exists verbose backup quiet silent + file_dir="$(dirname "$file")" + exists=true + verbose=false + backup=false + quiet=false + silent=false case $1 in -b) backup=true; shift ;; -v) verbose=true; shift ;; -q) quiet=true; shift ;; + -s) quiet=true; silent=true; shift ;; -h|--help) echo "$help"; return ;; esac @@ -136,6 +139,13 @@ section. cedit is short for config edit. if ! $backup && $exists; then rm -r "$temp" fi - return $ret + if $silent; then + case $ret in + 0|1) return 0 ;; + *) return $ret ;; + esac + else + return $ret + fi } cedit "$@" -- 2.30.2 From fbda9b47041869aca20a660c1b46f5c76c2c2fcb Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Sun, 10 Mar 2024 00:17:47 -0500 Subject: [PATCH 08/10] handle zone file comments --- cedit | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cedit b/cedit index e6d373c..09d7b82 100755 --- a/cedit +++ b/cedit @@ -53,8 +53,15 @@ section. cedit is short for config edit. local file="$1" local file_name="${file##*/}" - local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#" - local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#" + local comment + comment="#_#_#" + + # bind zone files use ; for comments yes, a little hacky detection. + if [[ $file_name == db.* ]]; then + comment=";;_;_;" + fi + local begin="$comment start delimiter of cedit section$name. do not modify. $comment" + local end="$comment end delimiter of cedit section$name. do not modify. $comment" if [[ ! -e $file_dir ]]; then if ! mkdir -p $file_dir; then -- 2.30.2 From bc0f839e6674afc8d0d5ffdc27fbe2733161d0b6 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Tue, 16 Apr 2024 01:29:07 -0400 Subject: [PATCH 09/10] add missing license info --- README | 10 ++++++++++ cedit | 9 ++++++++- test/test | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README b/README index 279bfe3..88bd451 100644 --- a/README +++ b/README @@ -1,3 +1,13 @@ +The following is the GNU All-permissive License as recommended in + + +Copyright (C) 2024 Ian Kelling + +Copying and distribution of this file, with or without modification, +are permitted in any medium without royalty provided the copyright +notice and this notice are preserved. This file is offered as-is, +without any warranty. + The main documentation is availiable via --help and near the top of the bash script file next to this file. diff --git a/cedit b/cedit index 09d7b82..1cfa580 100755 --- a/cedit +++ b/cedit @@ -1,5 +1,12 @@ #!/bin/bash -# Copyright (C) 2016 Ian Kelling +# I, Ian Kelling, follow the GNU license recommendations at +# https://www.gnu.org/licenses/license-recommendations.en.html. They +# recommend that small programs, < 300 lines, be licensed under the +# Apache License 2.0. This file contains or is part of one or more small +# programs. If a small program grows beyond 300 lines, I plan to switch +# its license to GPL. + +# Copyright 2024 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/test/test b/test/test index 76eaea4..63c2f65 100755 --- a/test/test +++ b/test/test @@ -1,4 +1,25 @@ #!/bin/bash +# I, Ian Kelling, follow the GNU license recommendations at +# https://www.gnu.org/licenses/license-recommendations.en.html. They +# recommend that small programs, < 300 lines, be licensed under the +# Apache License 2.0. This file contains or is part of one or more small +# programs. If a small program grows beyond 300 lines, I plan to switch +# its license to GPL. + +# Copyright 2024 Ian Kelling + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + cd $(mktemp -d) -- 2.30.2 From 97e5ceaf2d7d3fec58fcd6b0004f221b19783620 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Wed, 24 Apr 2024 15:34:05 -0400 Subject: [PATCH 10/10] minor docs update --- README | 16 +++++++++------- cedit | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README b/README index 88bd451..0287269 100644 --- a/README +++ b/README @@ -1,12 +1,14 @@ -The following is the GNU All-permissive License as recommended in - +# The following is the GNU All-permissive License as recommended in +# -Copyright (C) 2024 Ian Kelling +# Copyright (C) 2024 Ian Kelling -Copying and distribution of this file, with or without modification, -are permitted in any medium without royalty provided the copyright -notice and this notice are preserved. This file is offered as-is, -without any warranty. +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. This file is offered as-is, +# without any warranty. + +Create/modify a comment-delimited section in a config file The main documentation is availiable via --help and near the top of the bash script file next to this file. diff --git a/cedit b/cedit index 1cfa580..e47afa5 100755 --- a/cedit +++ b/cedit @@ -22,7 +22,7 @@ cedit() { local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE -Create/modify a section in a config file +Create/modify a comment-delimited section in a config file Returns 1 if the file is modified by this command, 2 or higher for other problems. -- 2.30.2