sed替换需要协助

问题描述

我在/var/www/html/daloradius/library/daloradius.conf.PHP一个文件

具有以下提到的行:

$configValues['CONfig_DB_PORT'] = '3306'
$configValues['CONfig_DB_USER'] = 'root'

我要替换'3306'--->'5432'和'root'->'postgres' 使用shell脚本,所以请协助我如何使用sed命令。

解决方法

如果您无法在安装之前准备配置文件,而只是通过标准配置解压缩版本,则可以使bash脚本如下所示:

#!/bin/bash

set -e

dalconf="/var/www/html/daloradius/library/daloradius.conf.php"

cleanup() {
    rm -f "$tmpfile"
}

trap cleanup EXIT
tmpfile=$(mktemp)

# '3306' --> '5432' and 'root' --> 'postgres'

sed -E -e "s/'3306'/'5432'/" -e "s/'root'/'postgres'/" "$dalconf" > "$tmpfile"
mv "$tmpfile" "$dalconf"

注意:'3306''root'上的任何匹配项都将匹配,因此请增加匹配字符串的数量,以使您感到舒服。