Skip to content

Instantly share code, notes, and snippets.

@addlistener
Created October 5, 2019 12:50
Show Gist options
  • Save addlistener/6d74e8fd6d35117e952c5044f7f63ef2 to your computer and use it in GitHub Desktop.
Save addlistener/6d74e8fd6d35117e952c5044f7f63ef2 to your computer and use it in GitHub Desktop.
ubuntu add or change config file entry
function add_or_change {
TARGET_KEY=$1
REPLACEMENT_VALUE=$2
CONFIG_FILE=$3
if grep -e "^\s*$TARGET_KEY\s*=" "$CONFIG_FILE"; then
sed -i 's/^\('"$TARGET_KEY"'\s*=\s*\).*$/\1'"$REPLACEMENT_VALUE"'/' $CONFIG_FILE
grep -e "^\s*$TARGET_KEY\s*=" "$CONFIG_FILE"
else
echo "$TARGET_KEY=$REPLACEMENT_VALUE" >> "$CONFIG_FILE"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment