简体   繁体   中英

comment out lines in /fstab with bash script

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.

from this:

    /dev/lv_mymount /mymount  ext4    defaults        1 2

to this (with a #):

    #/dev/lv_mymount /mymount  ext4    defaults        1 2

Using 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 # .

Using awk:

awk '/[/]mymount/{$0="#"$0} 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM