Commit | Line | Data |
---|---|---|
bbe7e165 SBS |
1 | #!/bin/bash |
2 | ||
3 | # Desc: Writes keys from dconf dump | |
4 | # Purpose: Applying custom dconf dump without clobbering other keys | |
5 | # Usage: bkdconfwf [file] | |
6 | # Note: Useful if 'dconf load [file]' wipes existing settings. | |
7 | ||
8 | # Yell, Die, Try Three-Fingered Claw technique | |
9 | # Ref/Attrib: https://stackoverflow.com/a/25515370 | |
10 | yell() { echo "$0: $*" >&2; } | |
11 | die() { yell "$*"; exit 111; } | |
12 | try() { "$@" || die "cannot $*"; } | |
13 | ||
14 | #declare -A LG_AA # Associative Array for dconf <dir/key> and <value> | |
15 | ||
16 | # Process input file - save <dir/key> & <value> pairs to LG_AA | |
17 | FILEIN="$1" | |
18 | cat "$FILEIN" | while read line; do | |
19 | yell "Begin processing of:$line"; | |
20 | if [[ "$line" == "" ]]; then # Check for non-blank line | |
21 | yell "Blank line detected."; | |
22 | elif [[ "$line" =~ ^"["(.*)"]"$ ]]; then # Check for bracket-enclosed string (new line group) | |
23 | yell "New line group detected at line:$line"; | |
24 | # Note start of new line group | |
25 | lgNum=0 | |
26 | # Extract path | |
27 | lgDir="$line"; | |
28 | lgDir="${lgDir#[*}"; # See [2] | |
29 | lgDir="${lgDir%*]}"; # See [2] | |
30 | yell "Path is:$lgDir"; | |
31 | elif [[ "$line" =~ (.*)"="(.*) ]]; then # Check for key=value pair | |
32 | yell "DEBUG:Key value pair detected:$line"; | |
33 | # Extract key | |
34 | key="${line%%=*}"; # See [1,3] | |
35 | yell "Key extracted:$key"; | |
36 | # Extract value | |
37 | value="${line#*=}"; # See [1,3] | |
38 | yell "Value extracted:$value"; | |
39 | # Add <path/key>=<value> to associative array LG_AA | |
40 | #yell "DEBUG:LG_AA old:$LG_AA"; | |
41 | #yell "DEBUG:lgDir:$lgDir"; | |
42 | #yell "DEBUG:value:$value"; | |
43 | #LG_AA["$lgDir/$key"]+="'$value' "; | |
44 | #yell "DEBUG:LG_AA new:${LG_AA[@]}"; | |
45 | # Apply keyvalue pair | |
46 | try dconf write "/$lgDir/$key" "$value"; | |
47 | else | |
48 | yell "ERROR:Unrecognized string detected." | |
49 | fi; | |
50 | # Increment line group counter | |
51 | ((lgNum++)) | |
52 | yell "DEBUG:line group number:$lgNum" | |
53 | yell "Finished processing of:$line"; | |
54 | yell "============================="; | |
55 | done | |
56 | ||
57 | # Ref/Attrib | |
58 | # [1] How to extract substrings around specific character using parameter expansion. chepner. https://stackoverflow.com/a/15149278 https://stackoverflow.com/questions/15148796/get-string-after-character#comment43744397_15149278 | |
59 | # [2] How to extract string from brackets. Alex Howansky. https://stackoverflow.com/a/7209856 | |
60 | # [3] Advanced Bash-Scripting Guide, section 10.1 Manipulating Strings http://tldp.org/LDP/abs/html/string-manipulation.html |