ambevar-dotfiles/.scripts/blind-append

46 lines
910 B
Bash
Executable File

#!/usr/bin/env zsh
if [ $# -gt 2 ] || [ $# -lt 1 ] || [ "$1" = "-h" ]; then
cat <<EOF
Usage: ${0##*/} FILE [STRING]
Append to all STRING found in FILE a secret phrase being prompted. If STRING is
omitted, secret phrase will be appended to the end of the file. If FILE does
not exist, it will be created and secret phrase will be inserted. STRING will be
ignored.
This requires 'read -s'. As of Oct 6 2013 it will not work for strictly
POSIX-compliant shells.
This is a stupid program, don't use it.
EOF
exit
fi
FILE="$1"
STRING=""
DUMMY=""
if [ $# -eq 2 ]; then
STRING="$2"
fi
echo -n "Secret: "
read -s DUMMY
echo ""
if [ ! -e "$FILE" ] || [ -z "$STRING" ]; then
echo "$DUMMY" >> "$FILE"
echo "Secret appended to ${FILE} at the end."
exit
fi
if [ $# -eq 1 ]; then
echo "$DUMMY" >> "$FILE"
else
ex -sc "%s/${STRING}/${STRING}${DUMMY}/g|xit" "${FILE}"
fi
echo "Secret appended to ${FILE}."