如何使用bash脚本将新输入添加到配置文件

时间:2020-05-12 19:12:00

标签: linux bash shell redhat archlinux

我想使用bash脚本从终端向配置文件添加新输入。这是我尝试过的:

echo Hello, please add the new text here
read varname
sed -i "s/\<my-images=>/& $varname/" /home/myconfig
echo Image $varname has been added to the configuration. Thanks!!

/ home / myconfig具有

id=1
max-mb=1000
my-images=customimage

所需的输出是

id=1
max-mb=1000
my-images=mynewtext customimage

因此,应在my-images =之后添加mynewtext = 反正这样做吗?

1 个答案:

答案 0 :(得分:1)

问题在于您要传递给sed的正则表达式匹配。试试:

sed -i "s/my-images=/&$varname /" /home/myconfig

相反。