|
|
@@ -0,0 +1,59 @@ |
|
|
|
#!/bin/sh |
|
|
|
#The script inserts or replaces an ini field where $1 is the path of the target file and $2 is the value to add or replace. $2 has the form section:property=value. It only cares about the first instance of $2 |
|
|
|
|
|
|
|
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='^\[.*\]$' |
|
|
|
#ex_sec='/^\[$section\]/,/^\[.*\]$\|^\s*$/' |
|
|
|
|
|
|
|
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() { |
|
|
|
#Check if the section exists. If not, abort. |
|
|
|
if [ -z "$(sed "/^\[$section\]$/p" $1)" ]; then printf "section $section does not exist. Aborting.\n" exit; fi |
|
|
|
sed -i "/^\[$section\]/a $property" $1 |
|
|
|
} |
|
|
|
|
|
|
|
insert() { |
|
|
|
#Check if the field is already there |
|
|
|
field=$(sed -n "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ {/^$property_type=.*$/p}" $1) |
|
|
|
|
|
|
|
if [ -z $field ]; then add $1; else replace $1; fi |
|
|
|
} |
|
|
|
|
|
|
|
get() { |
|
|
|
#Prints a section of the ini file |
|
|
|
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 |