用 bash 脚本注释掉 /fstab 中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29399790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comment out lines in /fstab with bash script
提问by Mike Q
I need a bash script command to look in /etc/fstab and find the line that contains a mount name "/mymount" and just puts a "#" at the beginning of the line to comment it out.
我需要一个 bash 脚本命令来查看 /etc/fstab 并找到包含挂载名称“/mymount”的行,并在该行的开头放置一个“#”以将其注释掉。
from this:
由此:
/dev/lv_mymount /mymount ext4 defaults 1 2
to this (with a #):
对此(带有#):
#/dev/lv_mymount /mymount ext4 defaults 1 2
回答by John1024
Using sed:
使用 sed:
sed -i '/[/]mymount/ s/^/#/' /etc/fstab
How it works:
这个怎么运作:
-i
Edit the file in-place
/[/]mymount/
Select only lines that contain
/mymount
s/^/#/
For those selected lines, place at the beginning of the line,
^
, the character#
.
-i
就地编辑文件
/[/]mymount/
只选择包含的行
/mymount
s/^/#/
对于那些选定的行,将
^
字符放在行首#
。
Using awk:
使用 awk:
awk '/[/]mymount/{##代码##="#"##代码##} 1' /etc/fstab >/etc/fstab.tmp && mv /etc/fstab.tmp /etc/fstab
How it works:
这个怎么运作:
/[/]mymount/ {$0="#"$0}
For those lines containing
/mymount
and place a#
at the beginning of the line.1
This is awk's cryptic shorthand for "print each line."
/[/]mymount/ {$0="#"$0}
对于那些包含
/mymount
并#
在行首放置 a的行。1
这是 awk 对“打印每一行”的神秘简写。