繁体   English   中英

如何用 bash 中的 url 中的文本替换文件中的特定文本部分?

[英]How to replace specific section of text in a file, with text from url in bash?

我正在尝试用 url 链接中的文本替换文件中的一些文本。 我已经接近了,但结果有意想不到的并发症。

这是我需要使用 (wp-config.php) 执行此操作的文件的文本:

<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the installation.
 * You don't have to use the web site, you can copy this file to "wp-config.php"
 * and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * Database settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://wordpress.org/support/article/editing-wp-config-php/
 *
 * @package WordPress
 */

// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );

/** Database username */
define( 'DB_USER', 'username_here' );

/** Database password */
define( 'DB_PASSWORD', 'password_here' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

/**#@+
 * Authentication unique keys and salts.
 *
 * Change these to different unique phrases! You can generate these using
 * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
 *
 * You can change these at any point in time to invalidate all existing cookies.
 * This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );

/**#@-*/

/**
 * WordPress database table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/support/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

/* Add any custom values between this line and the "stop editing" line. */



/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

我需要用这个链接中的替换盐部分:

https://api.wordpress.org/secret-key/1.1/salt/

我相信盐中的转义字符会使我的事情变得复杂,因为我是 bash 的新手。 这是我得到的最接近的,但这需要一些帮助:

cp wp-config.php wp-config.bak
curl https://api.wordpress.org/secret-key/1.1/salt/ | awk -F "'" 'NR==FNR {a[$2]=$4;next} {$4=a[$2]}1' OFS="'" - wp-config.php > wp-config.tmp
mv wp-config.tmp wp-config.php
cat wp-config.php

再一次,这会产生新的问题,虽然它确实替换了 salts 部分,但它也使文件不可读。 希望有人可以帮助我调整这个,或者告诉我一个更好的方法来完成这个。

由于您信任 URL 的内容,您可以将wp-config.php中的defines行替换为(一次):

require '/path/to/salts';

然后当您需要新盐时,只需执行以下操作:

url='https://api.wordpress.org/secret-key/1.1/salt/'

if salts=$(curl -fs "$url"); then
    printf '<?php\n%s\n' "$salts" > /path/to/salts
fi

您需要确保您的网络服务器进程有权读取 salts 文件。

这可能是导致wp-config.php在您执行 awk/mv 时变得不可读的原因:如果创建临时文件的用户将其创建为rw-------例如(或rw-r----但webservice进程不在同一个组中运行),那么webserver进程将无法读取文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM