X-Git-Url: https://iankelling.org/git/?p=cedit;a=blobdiff_plain;f=cedit;h=9d37b55e56bc1053527489f86c8c4673420361ca;hp=629fe01b1e01db5baabbed83a317206dca5ef2bb;hb=a612c049422536786fde20b55470e933f0b521ae;hpb=3e93da34a19ca898d8c1ab7902e600d2a9a2defa diff --git a/cedit b/cedit index 629fe01..9d37b55 100755 --- a/cedit +++ b/cedit @@ -17,18 +17,24 @@ cedit() { 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 +for other problems. + 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. -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 @@ -47,14 +53,14 @@ section. cedit is short for config edit. if [[ ! -e $file_dir ]]; then if ! mkdir -p $file_dir; then s=sudo - $s mkdir -p $file_dir || return 1 + $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 1 + $s touch $file || return 2 fi fi @@ -88,7 +94,7 @@ section. cedit is short for config edit. fi if ! $exists; then - ret=0 + ret=1 if $verbose; then echo "New file $file:" cat "$file" @@ -101,17 +107,21 @@ section. cedit is short for config edit. 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 fi - return 0 + if ! $backup; then + rm -r $"$temp" + fi + return $ret } cedit "$@"