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

How to add new input to the configuration file using bash script

我想使用 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

所以mynewtext应该加在my-images=之后 无论如何都要这样做?

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

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

相反。