Bash:更新文件中的变量 [英] Bash: Update a variable within a file

查看:14
本文介绍了Bash:更新文件中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个简单的答案,在我浏览答案之前,我可能会继续在 Google 上进行挖掘.但我的时间很紧,我希望得到一个简单的回应.

I know this is a simple answer and I could probably keep digging around on Google before I stroll across the answer. But I am on a tight schedule and I was hoping for an easy response.

我需要在安装时更新 ifcfg-eth0 中的变量.所以换句话说,这就是需要发生的事情:

I need to update a variable in ifcfg-eth0 upon an installation. So in other words, this is what needs to happen:

以下变量需要改自:

ONBOOT=no
BOOTPROTO=dhcp

ONBOOT=yes
BOOTPROTO=static

提前致谢!

干杯.

推荐答案

sed -i -e '/^ONBOOT=/s|.*|ONBOOT=yes|; /^BOOTPROTO=/s|.*|BOOTPROTO=static|' file

也试试:

sed -i -re 's|^(ONBOOT=).*|1yes|; s|^(BOOTPROTO=).*|1static|' file

或者

sed -i -e 's|^(ONBOOT=).*|1yes|; s|^(BOOTPROTO=).*|1static|' file

这篇关于Bash:更新文件中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆