我正在尝试在Linux中创建一个线条,它将从wp-config.php收集数据库主机、数据库名称、用户名和密码,并将其组合成一行,在这里我可以导入带有.sh文件的数据库。
我需要收集的代码块是:
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
/** MySQL hostname */
define('DB_HOST', 'localhost');我要创建的代码行是:
mysql -h localhost -u username_here --password="password_here" database_name_here < db-backup.sql
我是个新手,所以我试着做的是:
grep DB_HOST DB_USER wp-config.php | cut -d "'" -f4 | awk '{print "mysql -h "$0;}'这个开始是有效的,但是当我想向字符串中添加更多信息时,我只是得到了最后一部分。
我该如何写这段代码,以获得包含所有信息的整行?
发布于 2017-03-06 05:56:00
MYSQLPWD=$(cat wp-config.php |grep DB_PASSWORD | cut -d \' -f 4)
MYSQLUSR=$(cat wp-config.php |grep DB_USER | cut -d \' -f 4)
MYSQLDBNAME=$(cat wp-config.php |grep DB_NAME | cut -d \' -f 4)
HT="localhost"
echo $MYSQLPWD
echo $MYSQLUSR
echo $MYSQLDBNAME
echo $HT
mysql -h$HT -u$MYSQLUSR -p$MYSQLPWD $MYSQLDBNAME < $MYSQLDBNAME发布于 2017-03-06 05:52:07
不是一行,但适用于您的简单示例
DB_PASSWORD=$(grep DB_PASSWORD wp-config.php | cut -d "'" -f4)
DB_USER=$(grep DB_USER wp-config.php | cut -d "'" -f4)
DB_NAME=$(grep DB_NAME wp-config.php | cut -d "'" -f4)
DB_HOST=$(grep DB_HOST wp-config.php | cut -d "'" -f4)
echo "mysql -h $DB_HOST -u $DB_USER --password=\"$DB_PASSWORD\" $DB_NAME"注意:你不需要awk,你只是用它来连接字符串,所以我删除了它。而且对输入文件中相关行的检测并不完善,它可能会受到许多因素的干扰,例如输入文件中是否有注释
/** we are using blabla as DB_NAME because <reasons> **/ 它会检测到该行与DB_NAME匹配。此外,只有一条匹配的行也很重要。
https://stackoverflow.com/questions/42614375
复制相似问题