#!/bin/bash function usage { builtin echo "Usage: $(basename "$0") " } [[ $# -ne 3 ]] && usage && exit 2 REGEX=$1 STRING=$2 FILE=$3 if ! grep -Eq "$REGEX" "$FILE"; then printf "%s\n" "$STRING" >>"$FILE" echo 'appended' else sed -Ei "s|$REGEX|$STRING|g" "$FILE" echo 'replaced' fi