在bash中使用两个变量查找和替换



我正在尝试自动化一些修补步骤,我已经写了一个脚本来备份文件,然后在所有位置替换文件中的路径,在测试备份文件时可以,但即使声明成功,查找和替换也不起作用,我正在尝试使用said,但我还没有做到这一点,所以如果有更干净的方法,我不会反对,请参阅下面的代码示例:

#!/bin/bash
#set -x
nodemanager="/u01/app/oracle/admin/domain/mserver/ADF_INT/nodemanager/"
bindirectory="/u01/app/oracle/admin/domain/mserver/ADF_INT/bin/"
ouilocation="/u01/app/oracle/product/fmw/middleware12c/oui/bin/"
date=$(date +"%d-%m-%y")
echo $date
read -p "Please enter the current jdk path: " oldjdk
read -p "Please enter the new jdk path: " newjdk
echo "Backing up an uploading files to remote server...."
cd $nodemanager || exit
cp nodemanager.properties nodemanager_$date.bkp
if [ $? -ne 0 ]; # Again checking if the last operation was successful if not shall exit the 
script
then
echo -e "nodemanager.properties backup failed"
echo -e "Terminating script"
exit 0
fi
sed -i -e 's/${$oldjdk/$newjdk}/g' nodemanager.properties
if [ $? -ne 0 ]; # Again checking if the last operation was successful if not shall exit the 
script
then
echo -e "find and replace failed for nodemanager.properties"
echo -e "Terminating script"
exit 0
fi
echo -e "nodemanager.properties operations completed successfullyn"

谢谢JJ

为了给你省去一些麻烦,我发现/不是sed的一部分,它可以是任何与路径不冲突的分隔符,所以我使用了这个:

sed -i "s+$oldjdk+$newjdk+g" file

谢谢JJ

最新更新