-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_database_variables.sh
88 lines (68 loc) · 1.77 KB
/
mysql_database_variables.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -----------------------------------------------------------------------------
# Name : mysql_database_variables.sh
# Description : get all variables from mysql
# Author : qiaolei
# Date : 2014/10/13
# -----------------------------------------------------------------------------
#!/bin/sh
usage()
{
echo "usage: $0 <username> <password>"
}
if [ $# -ne 2 ];then
usage
exit 1
fi
LOG_FILE=/tmp/mysql_scripts.log
if [ ! -f $LOG_FILE ];then
touch $LOG_FILE
fi
TIMESTAMP=`date +%Y%m%d%H%M%S`
echo "@$TIMESTAMP: {$0 $*}" | tee -a $LOG_FILE
USERNAME=$1
PASSWORD=$2
TMP_VARIABLES=/tmp/variables.tmp
if [ ! -f $TMP_VARIABLES ];then
touch $TMP_VARIABLES
fi
if [ $PASSWORD != "NULL" ]; then
mysqladmin -u $USERNAME -p$PASSWORD variables > $TMP_VARIABLES
else
mysqladmin -u $USERNAME variables > $TMP_VARIABLES
fi
TMP_KEYS=/tmp/keys.tmp
cat $TMP_VARIABLES | sed '1,3d'| sed '$d' | tr -d "[|]" | awk '{print $1}' > $TMP_KEYS
TMP_VALUES=/tmp/values.tmp
cat $TMP_VARIABLES | sed '1,3d'| sed '$d' | tr -d "[|]" | awk '{print $2}' > $TMP_VALUES
# keys
declare -a KEYS
i=0
while read LINE
do
KEYS[$i]=${LINE%%:}
i=`expr $i + 1`
done < $TMP_KEYS
#values
declare -a VALUES
i=0
while read LINE
do
VALUES[$i]=$LINE
i=`expr $i + 1`
done < $TMP_VALUES
rm -f $TMP_VARIABLES $TMP_KEYS $TMP_VALUES
# store key-value pair in a map
declare -A VARIABLES
NUM_OF_KEYS=${#KEYS[@]}
for ((i=0;i<$NUM_OF_KEYS;i++));do
VARIABLES[${KEYS[$i]}]=${VALUES[$i]}
done
# construct a jason string
RESULT=`printf "%s%s" "$RESULT" "{"`
for ((i=0;i<$NUM_OF_KEYS;i++));do
RESULT=`printf "%s%s%s%s" "$RESULT" "\"${KEYS[$i]}\": \"" "${VARIABLES[${KEYS[$i]}]}" "\","`
# echo "${KEYS[$i]}: ${VARIABLES[${KEYS[$i]}]}"
done
RESULT=${RESULT%%,}
RESULT=`printf "%s%s" "$RESULT" "}"`
echo $RESULT