-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsync_configs.sh
executable file
·72 lines (62 loc) · 1.88 KB
/
sync_configs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#########################################################################
# File Name: sync_configs.sh
# Author: Carbon (ecrasy@gmail.com)
# Description: feel free to use
# Created Time: 2023-02-26 01:57:56 UTC
# Modified Time: 2023-02-27 08:17:36 UTC
#########################################################################
#!/bin/bash
set -e
usage() { echo "Usage: $0 -c <configs dir> [-s <configs sub file>] [-a <configs add file>]" 1>&2; exit 1; }
configs_dir=""
diffs_sub=""
diffs_add=""
while getopts ":c:s:a:" o; do
case "${o}" in
c)
configs_dir=${OPTARG}
;;
s)
diffs_sub=${OPTARG}
;;
a)
diffs_add=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
[ -n "$configs_dir" ] || usage
configs_dir=$(echo $configs_dir | xargs realpath -s | sed 's:/*$::')
configs=$(find $configs_dir -maxdepth 2 -type f -name '*.config' | sort -f)
[ -n "$configs" ] || exit 0
# do subtraction
if [[ -n "$diffs_sub" && -e $diffs_sub ]]; then
for config in $configs
do
cdiffs=$(cat $diffs_sub)
echo -e "\n**************** Updating $(basename $config) ****************"
for cdiff in $cdiffs
do
sed -i -e "/${cdiff}/d" $config
echo "remove $cdiff from $(basename $config)"
done
echo -e "**************** $(basename $config) saved ****************\n"
done
fi
# do addition
if [[ -n "$diffs_add" && -e $diffs_add ]]; then
for config in $configs
do
cdiffs=$(cat $diffs_add)
echo -e "\n**************** Updating $(basename $config) ****************"
for cdiff in $cdiffs
do
echo $cdiff >> $config
echo "add $cdiff to $(basename $config)"
done
echo -e "**************** $(basename $config) saved ****************\n"
done
fi