有配置文件config.ini,內容如下:
[host]
host_conf=appts.xxxx.com
我想要修改host_conf=appts.xxxx.com(預生產環境)的內容為host_conf=appapi.xxxx.com(線上環境)
配置腳本如下:
host=appapi.xxxx.com 測試數據 host_env="hostconf=${host}" #這里注意使用雙引號 line=$(sed -n '/host_conf/=' config.ini) #獲取要修改文件的行號 newline=$(expr $line - 1)#計算插入文件的新的位置(后面使用追加方式修改文本,故這里上 line-1) sed -i "$line d" config.ini#刪除行 sed -i "${newline} a\\${host_env}" config.ini#將拼接好的字符串寫入行
執行效果:
文件修改生效