【问题标题】:How to correctly comment and uncomment fstab with sed?如何使用 sed 正确注释和取消注释 fstab?
【发布时间】:2019-07-24 00:48:46
【问题描述】:

我想使用命令来注释和取消注释 /etc/fstab 中的 swap 分区,而不会对已经是 cmets 的行产生副作用。

用例 1) 示例 fstab:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda6 during installation
UUID=97166a83-2932-4a8b-862a-68aa71d4e166 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=b95babcf-2d8d-412d-a15d-c8f9de651ee8 none            swap    sw              0       0

我设法使用以下命令对其进行评论/取消评论:

sudo sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab
sudo sed -i '/^#.* swap /s/^#//' /etc/fstab

问题是swap was on /dev/sda5 during installation 行也被注释/取消注释,如果两次取消注释可能会导致文档失去完整性,从而导致:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda6 during installation
UUID=97166a83-2932-4a8b-862a-68aa71d4e166 /               ext4    errors=remount-ro 0       1
 swap was on /dev/sda5 during installation
UUID=b95babcf-2d8d-412d-a15d-c8f9de651ee8 none            swap    sw              0       0

更新:

用例 2) 一些交换分区没有 UUID:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=112aa94b-3c82-4a90-9600-e3e45dc820de /               ext4    errors=remount-ro 0       1
# /home was on /dev/sda3 during installation
UUID=24ec038a-a164-4d32-809a-5a5552c009e9 /home           ext4    defaults        0       2
/swapfile                                 none            swap    sw              0       0

我如何创建一个 sed 表达式来制作这些 cmets?

【问题讨论】:

    标签: regex linux shell sed


    【解决方案1】:

    你可以试试这个:

    sudo sed -i 's/.* none.* swap.* sw.*/#&/' /etc/fstab
    sudo sed -i '/.* none.* swap.* sw.*/s/^#//' /etc/fstab
    

    【讨论】:

    • @UlLox 对不起.. 您的解决方案非常适合我发布的示例,但有些机器在交换分区中没有 UUID。我已经更新了这个问题。不知何故,我认为它必须考虑到它是第三个参数 = swap.
    • 几乎完美.. 我在一些文档中看到一些交换总是带有“无”安装点。我认为这已经足够了,不需要 sw。这里有很多例子:wiki.archlinux.org/index.php/fstab
    • 奖励不要让评论命令推荐一行:sudo sed -e '/.* none.* swap.*/ s/^#*/#/' -i /etc/fstab
    • 感谢这对我来说非常有用。拥有正则表达式是强大的。我正在尝试为 Ubuntu Vagrant 机器做事
    【解决方案2】:

    将其放入脚本(或函数)并使用 sudo 运行:

    file="/etc/fstab"; line=$(cat ${file} | grep -nE '^# swap' | cut -d':' -f1); ((line++)); read=$(awk "NR >= ${line} && NR <= ${line}" <<< `cat ${file}`); [ "${read:0:1}" != '#' ] && sed -iE "${line}s:^:# :" "${file}" || sed -iE "${line}s:^#\+ \+::" "${file}"
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多