bash 从 wp-config.php 读取变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7586995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:53:47  来源:igfitidea点击:

read variables from wp-config.php

bashshellvariablesscripting

提问by anthonysomerset

I'm busy writing a basic migration scripts for some WP sites, and i'm trying to automate creating mysql database and user credentials from reading the wp-config file

我正忙于为某些 WP 站点编写基本的迁移脚本,并且我正在尝试通过读取 wp-config 文件来自动创建 mysql 数据库和用户凭据

how can i read the following variables from the wp-config file so that i effectively end up with 3 variables within a bash script:

我如何从 wp-config 文件中读取以下变量,以便我有效地在 bash 脚本中得到 3 个变量:

/** The name of the database for WordPress */
define('DB_NAME', 'somedbname');

/** MySQL database username */
define('DB_USER', 'someusername');

/** MySQL database password */
define('DB_PASSWORD', 'somerandompassword');

eg my output should effectively give me:

例如我的输出应该有效地给我:

WPDBNAME=somedbname
WPDBUSER=somedbuser
WPDBPASS=somerandompassword

回答by Femi

Try this:

尝试这个:

WPDBNAME=`cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`

回答by Matt Clegg

If you want to use wp-config details to connect to mysql you can use;

如果你想使用 wp-config details 连接到 mysql 你可以使用;

mysql -u `cat wp-config.php | grep DB_USER | cut -d \' -f 4` -p`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4` -h `cat wp-config.php | grep DB_HOST | cut -d \' -f 4` `cat wp-config.php | grep DB_NAME | cut -d \' -f 4`

回答by Micha? ?rajer

you can use awk:

你可以使用 awk:

awk -F"[()']" '/^define/{printf "%s=\"%s\"\n", , ;}' < foo.php

This will give you:

这会给你:

DB_NAME="somedbname"
DB_USER="someusername"
DB_PASSWORD="somerandompassword"

Note, that this solution will work with variables containing spaces.

请注意,此解决方案适用于包含空格的变量。

回答by Jeff

find . -name "wp-config.php" -print0 | xargs -0 -r grep -e "DB_NAME" -e "DB_USER" -e "DB_PASSWORD"

回答by Eugene Ionichev

Here is an example of the universal way to pass php variables to bash without the need to parse:

这是将 php 变量传递给 bash 而无需解析的通用方法的示例:

#!/bin/bash

source <(php -r 'require("wp-config.php"); echo("DB_NAME=".DB_NAME."; DB_USER=".DB_USER."; DB_PASSWORD=".DB_PASSWORD); ')
mysqldump --user $DB_USER --password="$DB_PASSWORD" $DB_NAME | gzip > $DB_NAME-$(date +%Y%m%d%H%M%S).sql.gz

Algorithm explanation in a few words:

算法简述:

  1. run your php
  2. print variables as var=value
  3. source output
  4. use variables in bash
  1. 运行你的 php
  2. 将变量打印为 var=value
  3. 源输出
  4. 在 bash 中使用变量

回答by Miguel

If you are trying to dump the MySQL database, you can also use wp-cli db exportfunction:

如果您尝试转储 MySQL 数据库,您还可以使用wp-cli db export函数:

wp db export --path=PATHTOYOURWP