cedit() {
local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE
-Create/edit a #comment delimited section of a file
+Create/modify a section in a config file
-Reads STDIN for the contents of the section. Without SECTION_NAME, it
-acts on a global unnamed section. cedit is short for config edit.
+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
- if [[ $1 == -v ]]; then
- verbose=true
- shift
- fi
if (( $# == 2 )); then
name=": $1"
shift
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
fi
if ! $exists; then
- ret=0
+ ret=1
if $verbose; then
echo "New file $file:"
cat "$file"
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
+ if ! $backup; then
+ rm -r $"$temp"
+ fi
return $ret
}
cedit "$@"