X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=cedit;h=e6d128d291ad58af7c630dd5f52e151586937859;hb=9ee05f569ce5da96047000722dbde36f689d826d;hp=68efcae69398a0cb73e60134eaa9972c2579db0d;hpb=6cc921147f723f23586dd0b0b036e028e90c8e3d;p=cedit diff --git a/cedit b/cedit index 68efcae..e6d128d 100755 --- a/cedit +++ b/cedit @@ -13,17 +13,29 @@ # See the License for the specific language governing permissions and # limitations under the License. -cedit() { # [-v] [section_name] FILE +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 +-h|--help Help" local s diff name init local file_dir="$(dirname "$file")" local exists=true local verbose=false - if [[ $1 == -v ]]; then - verbose=true - shift - fi + case $1 in + -v) verbose=true; shift ;; + -h|--help) echo "$help"; return ;; + esac + if (( $# == 2 )); then name=": $1" shift @@ -38,14 +50,14 @@ cedit() { # [-v] [section_name] FILE 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 @@ -79,7 +91,7 @@ cedit() { # [-v] [section_name] FILE fi if ! $exists; then - ret=0 + ret=1 if $verbose; then echo "New file $file:" cat "$file" @@ -92,12 +104,12 @@ cedit() { # [-v] [section_name] 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