Line immutable

line_immutable

Ensure a line is present in a file (idempotent). If the line already exists verbatim, nothing is done. Otherwise the line is appended, inserted before a given line number, or inserted after the first line matching a given pattern.

Example

line_immutable "nameserver 1.1.1.1" /etc/resolv.conf
line_immutable --after "^Host \*" "  ServerAliveInterval 60" ~/.ssh/config
line_immutable --line-number 3 "# inserted" /etc/myconfig

Arguments

  • $1 (string): The line to ensure is present
  • $2 (string): The file to operate on

Exit codes

  • 0: Line was already present or successfully inserted
  • 1: File not found, not writable, or pattern not matched