dotfiles/shell/mysqlpw.sh

40 lines
1,003 B
Bash
Raw Normal View History

2017-01-26 13:48:55 +01:00
#!/bin/sh
#The function will find the given user and search for a mysql-password
#in the home-directory, which will get printed out!
#arg1: user where to find a config-file
# if not given, it uses the current user in the $HOME and $USER-Variable
2021-12-06 12:41:02 +01:00
mysqlpw() {
_mysqlpw_H=$HOME
_mysqlpw_U=$USER
2017-01-26 13:48:55 +01:00
# use different user, if arg1 given
2021-12-06 12:41:02 +01:00
if [ -n "$1" ]; then
2017-01-26 13:48:55 +01:00
# check if given user is even existing
2021-12-06 12:41:02 +01:00
if ! id "$1" >/dev/null 2>/dev/null; then
echo user "$1" not existing >&2
2017-01-26 13:48:55 +01:00
return 1
fi
2021-12-06 12:41:02 +01:00
_mysqlpw_U=$1
_mysqlpw_H=$(grep ^"$1": /etc/passwd | cut -d ":" -f 6)
2017-01-26 13:48:55 +01:00
fi;
2021-12-06 12:41:02 +01:00
_mysqlpw_cnf="$_mysqlpw_H/.my.cnf"
2017-01-26 13:48:55 +01:00
2021-12-06 12:41:02 +01:00
if [ ! -e "$_mysqlpw_cnf" ]; then
echo "$_mysqlpw_cnf" not existing >&2 && return 1
2017-01-26 13:48:55 +01:00
fi
2021-12-06 12:41:02 +01:00
if [ ! -r "$_mysqlpw_cnf" ]; then
echo "$_mysqlpw_cnf" not readable >&2 && return 1
2017-01-26 13:48:55 +01:00
fi
2021-12-06 12:41:02 +01:00
_mysqlpw_PW=$( (grep "password=" "$_mysqlpw_cnf" || echo undefined) | cut -d "=" -f 2)
2017-01-26 13:48:55 +01:00
2021-12-06 12:41:02 +01:00
echo MySQL-Password for user "$_mysqlpw_U" is "$_mysqlpw_PW"
2017-01-26 13:48:55 +01:00
}