简体   繁体   English

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

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

I am trying to replace some text in a file with text from a url link.我正在尝试用 url 链接中的文本替换文件中的一些文本。 I have gotten close, but the result has unintended complications.我已经接近了,但结果有意想不到的并发症。

Here is the text of the file I need to do this with (wp-config.php):这是我需要使用 (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';

I need to replace the section of salts with the ones in this link:我需要用这个链接中的替换盐部分:

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

I believe there are escape characters in the salts complicating things for me as I am new to bash.我相信盐中的转义字符会使我的事情变得复杂,因为我是 bash 的新手。 Here is the closest I have gotten but this took some help:这是我得到的最接近的,但这需要一些帮助:

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

Once again, this creates new issues, while it does replace the salts section it also makes the file unreadable.再一次,这会产生新的问题,虽然它确实替换了 salts 部分,但它也使文件不可读。 Hoping someone can help me tweak this, or show me a better way to accomplish this.希望有人可以帮助我调整这个,或者告诉我一个更好的方法来完成这个。

Since you trust the contents of the URL, you could replace (once) the defines lines in wp-config.php with:由于您信任 URL 的内容,您可以将wp-config.php中的defines行替换为(一次):

require '/path/to/salts';

and then when you need new salts, just do something like:然后当您需要新盐时,只需执行以下操作:

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

You'll need to ensure that your webserver process has permission to read the salts file.您需要确保您的网络服务器进程有权读取 salts 文件。

This is probably what is causing wp-config.php to become unreadable when you do your awk/mv: if the user creating the temporary file creates it as rw------- for example (or as rw-r---- but the webservice process does not run in the same group), then the webserver process will be unable to read the file.这可能是导致wp-config.php在您执行 awk/mv 时变得不可读的原因:如果创建临时文件的用户将其创建为rw-------例如(或rw-r----但webservice进程不在同一个组中运行),那么webserver进程将无法读取文件。

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

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