MIAOU-BASH is a collection of settings and helpers for leveraging BASH. Developer-friendly, it may be used as solo package with or without the miaou project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
312 B

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. #!/bin/bash
  2. function usage {
  3. builtin echo "Usage: $(basename "$0") <REGEX> <STRING> <FILE>"
  4. }
  5. [[ $# -ne 3 ]] && usage && exit 2
  6. REGEX=$1
  7. STRING=$2
  8. FILE=$3
  9. if ! grep -Eq "$REGEX" "$FILE"; then
  10. printf "%s\n" "$STRING" >>"$FILE"
  11. echo 'appended'
  12. else
  13. sed -Ei "s|$REGEX|$STRING|g" "$FILE"
  14. echo 'replaced'
  15. fi