ambevar-dotfiles/.scripts/blind-append

42 lines
855 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'. It will not work for the Bourne Shell.
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
sed -i "s/${STRING}/${STRING}${DUMMY}/g" "${FILE}"
fi
echo "Secret appended to ${FILE}."