使用 sed 更改/etc/fstab [英] Using sed to change /etc/fstab
本文介绍了使用 sed 更改/etc/fstab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在脚本中更改 /etc/fstab
.我想将 acl
属性添加到根分区.
I would like to change /etc/fstab
inside a script. I want to add the acl
attribute to the root partition.
一个 fstab
行条目如下所示:
One fstab
line entry looks like this:
UUID=730aee20-52b7-4920-75cd-d0d995ef2445 / ext3 errors=remount-ro 0 1
我想把它改成:
UUID=730aee20-52b7-4920-75cd-d0d995ef2445 / ext3 acl,errors=remount-ro 0 1
我想:1.搜索根分区/
的行2.在/
I thought:
1. Search line with root partition /
2. insert acl
after /
我怎样才能用 sed 做到这一点?
How can I do that with sed?
推荐答案
当我们都拥有 awk
时,谁还需要一些 3rd 方工具?
Who needs some 3rd party tool when we all have awk
?
awk '$2~"^/$"{$4="acl,"$4}1' OFS="\t" /etc/fstab
示例输出
$ awk '$2~"^/$"{$4="acl,"$4}1' OFS="\t" /etc/fstab
/dev/sda2 swap swap defaults 0 0
/dev/sda5 / ext4 acl,defaults 1 1
/dev/sda1 /boot ext4 defaults 1 2
/dev/sda6 /home ext4 defaults 1 2
/dev/sdb1 /backup ext4 defaults 1 2
#/dev/cdrom /mnt/cdrom auto noauto,owner,ro 0 0
/dev/fd0 /mnt/floppy auto noauto,owner 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
proc /proc proc defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
说明
$2~"^/$"
搜索第二个字段$2
看它是否与正斜杠本身匹配^/$
{$4="acl,"$4}
如果我们看到匹配,将 acl 添加到第 4 个字段的开头$4
}1
这是一个awk
快捷方式,相当于print $0
,即打印整行(包括我们可能有的任何更改)制作)OFS="\t"
将输出字段分隔符OFS
设置为选项卡\t
.默认为空格/etc/fstab
我们要用作输入的文件
$2~"^/$"
Search the 2nd field$2
to see if it matches a forward slash by itself^/$
{$4="acl,"$4}
If we see a match, prepend acl to the beginning of the 4th field$4
}1
This is anawk
shortcut which is equivalent toprint $0
, i.e. print the whole line (including any alterations we may have made)OFS="\t"
Set the Output Field SeparatorOFS
to a tab\t
. The default is space/etc/fstab
The file we want to use as input
这篇关于使用 sed 更改/etc/fstab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文