在bash shell脚本

时间:2016-01-01 21:03:28

标签: mysql linux bash shell centos

我在CentOS 7(x64)上安装MySQL 5.7,如下所示:

yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm;
yum install -y mysql-community-server;

新版本的mysql会自动生成随机root密码。可以使用以下命令找到它:

grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log | tail -1

e.g。输出:

  

2016-01-01T20:51:50.861279Z 1 [注意]为root @ localhost生成临时密码:s8,arY7l_xQ:

有没有办法将随机生成的根传递s8,arY7l_xQ:解析为上述命令输出中的变量?

1 个答案:

答案 0 :(得分:1)

这似乎对我有用:

dan@ebony ~ $ cat file
2016-01-01T20:51:50.861279Z 1 [Note] A temporary password is generated for root@localhost: s8,arY7l_xQ:
dan@ebony ~ $ grep 'temporary.*root@localhost' file | sed 's/.*root@localhost: //'
s8,arY7l_xQ:
dan@ebony ~ $ passwd="`grep 'temporary.*root@localhost' file | sed 's/.*root@localhost: //'`"
dan@ebony ~ $ echo $passwd
s8,arY7l_xQ: