#!/bin/bash
# makepass: Generates a password of custom length
# Requires: 'head' binary from coreutils
# Version: 0.1
# Build: 20-Nov-2010
# Author: Heiko Barth
# Licence: Beer-Ware (See: http://en.wikipedia.org/wiki/Beerware)
# Character classes
chars=$chars'abcdefghijkmnpqrstuvwxyz' # lower case; 'l' 'o' removed
chars=$chars'ABCDEFGHJKLMNPQRSTUVWXYZ' # upper case; 'I' 'O' removed
chars=$chars'123456789' # digits; 0 removed
chars=$chars'!$%#' # some special chars
# Some input validation
if [[ $1 =~ ^[0-9]+$ ]] && [ $1 -gt 0 ]; then length=$1
# Check if custom characters are given
if [ $# -eq 2 ] && [ ${#2} -gt 0 ]; then
shift
chars=$@
echo "Using custom characters: $chars"
fi
tput sc
echo -n "Progress: 0 of $length characters"
# Get random characters
while [ 1 ]; do
c=$(head -c 1 /dev/urandom)
if [[ $c =~ [$chars] ]]; then
res=$res$c
let i++
tput el1; tput rc
echo -n "Progress: $i of $length characters"
if [ ${#res} -eq $length ]; then
tput el1; tput rc
echo -e "Password: $res\n"
exit
fi
fi
done
else
echo "Syntax: makepass <length> [<characters>]" >&2
echo
exit 1
fi