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.

24 lines
304 B

  1. #!/bin/bash
  2. usage() {
  3. echo "Usage: $(basename "$0") <REGEX> <STRING> <FILE>"
  4. }
  5. if [[ $# -ne 3 ]]; then
  6. usage
  7. exit 2
  8. fi
  9. REGEX=$1
  10. STRING=$2
  11. FILE=$3
  12. grep -q "$REGEX" $FILE
  13. if [ $? -eq 1 ]; then
  14. echo "$STRING" >> $FILE
  15. echo appended
  16. else
  17. sed -i "s/$REGEX/$STRING/g" $FILE
  18. echo replaced
  19. fi