Skip to content

Instantly share code, notes, and snippets.

@mitch-kyle
Created December 19, 2018 01:14
Show Gist options
  • Save mitch-kyle/ba4cb4782b7606ae19323b7118888add to your computer and use it in GitHub Desktop.
Save mitch-kyle/ba4cb4782b7606ae19323b7118888add to your computer and use it in GitHub Desktop.
Use this bash script to destroy your config files!
#!/bin/bash
COMMENT_PREFIX=${COMMENT_PREFIX:="#"}
SEPARATOR=${SEPARATOR:="="}
in_place=no
function usage {
echo "update-config-val: comment all lines beginning with <config-property> and insert a line at the end of the <file> with the format <config-property>$SEPARATOR<value>
Usage: update-config-val [OPTIONS] config-property value file
Options:
-c PREFIX, --comment-prefix PREFIX - Set the prefix to use when commenting out old values. defaults to '#'
-s SEPARATOR, --separator SEPARATOR - Set the separator between property and value. defaults to '='
-i, --in-place - Modify the file in place rather than printing the new file to stdout"
}
if [ $# -lt 3 ]; then
usage
exit 1
fi
while [ $# -gt 3 ]; do
case $1 in
-h|--help)
usage
exit 0
;;
-s|--separator)
SEPARATOR="$2"
shift
;;
-c|--comment-prefix)
COMMENT_PREFIX="$2"
shift
;;
-i|--in-place)
in_place=yes
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
shift
done
property=$1
value=$2
file=$3
function new_file {
sed "s/^$property.*/$COMMENT_PREFIX&/g" $file
echo "$COMMENT_PREFIX Set on $(date)"
echo "$property$SEPARATOR$value"
}
if [ $in_place = yes ]; then
temp_file=$(mktemp "update-config-val.XXXXXX")
new_file > $temp_file
mv $temp_file $file
else
new_file
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment