銜接上文,以下是test文件夾下的腳本。
5、mysql_vars.sh腳本
#! /bin/sh
###########################################################
# Copyright (c) 2012, Heng.Wang. All rights reserved.
#
# This program is used to get the variables of mysql server.
###########################################################
# set -x
# Get the key value of input arguments format like '--args=value'.
get_key_value()
{
echo "$1" | sed 's/^--[a-zA-Z_-]*=//'
}
# Usage will be helpful when you need to input the valid arguments.
usage()
{
cat <<EOF
Usage: $0 [configure-options]
-?, --help Show this help message.
--mysqldir=<> Set the mysql directory
--host=<> Set the host name.
--port=<> Set the port number.
--user=<> Set the user name.
--password=<> Set the password.
--socket=<> Set the socket file
--outputdir=<> Set the output directory.
Note: this script is intended for internal use by developers.
EOF
}
# Print the default value of the arguments of the script.
print_default()
{
cat <<EOF
The default value of the variables:
mysqldir $MYSQLDIR
host $HOST
port $PORT
user $USER
password $PASSWORD
socket $SOCKET
outputdir $OUTPUTDIR
EOF
}
# Parse the input arguments and get the value of the input argument.
parse_options()
{
while test $# -gt 0
do
case "$1" in
--mysqldir=*)
MYSQLDIR=`get_key_value "$1"`;;
--host=*)
HOST=`get_key_value "$1"`;;
--port=*)
PORT=`get_key_value "$1"`;;
--user=*)
USER=`get_key_value "$1"`;;
--password=*)
PASSWORD=`get_key_value "$1"`;;
--socket=*)
SOCKET=`get_key_value "$1"`;;
--outputdir=*)
OUTPUTDIR=`get_key_value "$1"`;;
-? | --help)
usage
exit 0;;
*)
echo "Unknown option '$1'"
exit 1;;
esac
shift
done
}
#############################################################
# Define the variables the script used for executing.
MYSQLDIR=/opt/Percona-Server
HOST=localhost
PORT=3306
USER=root
PASSWORD=
SOCKET=/tmp/mysql.sock
OUTPUTDIR=/opt/output
# Call the parse_options function to parse the input arguments.
parse_options "$@"
# Define the mysql and sysbench executable program.
MYSQLADMIN=$MYSQLDIR/bin/mysqladmin
# If the mysqladmin is not exist, exit the script.
if [ -f $MYSQLADMIN ]
then
# If the output directory is not exist, then make directory.
[[ -d $OUTPUTDIR ]] || mkdir -p $OUTPUTDIR
cd $MYSQLDIR
./bin/mysqladmin --host=$HOST --port=$PORT --user=$USER --password=$PASSWORD --socket=$SOCKET variables >> ${OUTPUTDIR}/mysql_vars_${HOST}_${PORT}.var
else
echo "$MYSQLADMIN is not exist!"
echo "Please check the mysql home directory."
exit -1
fi