|
- #!/bin/sh
-
-
- file=$2
- change=$3
- section=$(printf $change | cut -d ':' -f 1)
- property=$(printf $change | cut -d ':' -f 2)
- property_type=$(printf $property | cut -d '=' -f 1)
- ex_blank='^\s*$'
- ex_any_sec='^\[.*\]$'
-
-
- check_args() {
- if [ $1 -ne $2 ]; then
- printf "Invalid number of arguments. Expected $2\n$@\n"
- exit;
- fi
- return 0;
- }
-
- replace() {
- sed -i "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ {s/^$property_type=.*$/$property/ }" $1
- }
-
- add() {
-
- if [ -z "$(sed "/^\[$section\]$/p" $1)" ]; then printf "section $section does not exist. Aborting.\n" exit; fi
- sed -i "/^\[$section\]/a $property" $1
- }
-
- insert() {
-
- field=$(sed -n "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ {/^$property_type=.*$/p}" $1)
-
- if [ -z $field ]; then add $1; else replace $1; fi
- }
-
- get() {
-
- sed -n "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ p" $1
- }
-
- make() {
- sed -i "$ G; $ a [$section]" $1
- }
-
- sdelete() {
- sed -i "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ {/$ex_any_sec/! d; /\[$section\]/d}" $1
-
- }
-
- check_args $# 3
- case $1 in
- insert) insert $2 $3;;
- get) get $2 $3;;
- make) make $2 $3;;
- sdelete) sdelete $2 $3;;
- *) printf "invalid command $1";;
- esac
|