Skip to content

Instantly share code, notes, and snippets.

@bentglasstube
Created March 30, 2014 15:04
Show Gist options
  • Save bentglasstube/9874066 to your computer and use it in GitHub Desktop.
Save bentglasstube/9874066 to your computer and use it in GitHub Desktop.
Password manager written in bash.
#!/bin/sh
FILE="$HOME/Dropbox/passwords.gpg"
quiet=""
output=""
while getopts hqo opt
do
case $opt in
q) quiet="y" ;;
o) output="y" ;;
h)
echo "Usage: $0 [options] [search] [number]"
echo " -q - be quiet"
echo " -o - output password on STDOUT"
echo " -h - print this message and exit"
exit 0
;;
esac
done
search=${!OPTIND}
((OPTIND++))
number=${!OPTIND}
if [ -z "$search" ]; then
vim $FILE
else
out=$(gpg -q -d $FILE |grep "$search")
if [ $? -ne 0 ]; then
echo "Couldn't find account matching '$search'" >&2
exit 1
fi
lines=$(echo "$out" |wc -l)
if [ $lines -gt 1 ]; then
if [[ -z $number ]]; then
echo "Choose one:"
echo "$out" |awk '{ print NR ") " $1 ":" $2 }'
read -p "Account? " number
fi
acct=$(echo "$out" |awk "NR==$number { print \$1 }")
user=$(echo "$out" |awk "NR==$number { print \$2 }")
pass=$(echo "$out" |awk "NR==$number { print \$3 }")
else
acct=$(echo "$out" |awk '{ print $1 }')
user=$(echo "$out" |awk '{ print $2 }')
pass=$(echo "$out" |awk '{ print $3 }')
fi
hash xclip 2>/dev/null
if [ $? -ne 0 ]; then
output="yes"
fi
if [[ -z $quiet ]]; then
echo "Acct: $acct";
echo "User: $user";
if [[ -z $output ]]; then
echo -n "$pass" |xclip -selection c
echo "Pass: Copied to clipboard"
else
echo "Pass: $pass"
fi
else
if [[ -z $output ]]; then
echo -n "$pass" |xclip -selection c
echo "Copied to clipboard"
else
echo $pass
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment