簡體   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