Created
February 18, 2013 20:11
-
-
Save MrDOS/4980271 to your computer and use it in GitHub Desktop.
Simple alphanumeric password generation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# Simple alphanumeric password generation. | |
DEFAULT_LENGTH=10 | |
if [ "$1" = "-h" -o "$1" = "--help" ] | |
then | |
echo "Usage: $0 [length]" 1>&2 | |
echo "Generate a random alphanumeric password." 1>&2 | |
echo -n "If a length is given, output that many characters. Otherwise, " | |
echo "output $DEFAULT_LENGTH characters." 1>&2 | |
exit 1 | |
fi | |
length=$DEFAULT_LENGTH | |
if [ $# -gt 0 -a ! `echo "$1" | tr -d [:digit:] | wc -c` -gt 1 ] | |
then | |
length=$1 | |
fi | |
# We make a horrible assumption that a random string 16 * $length will contain | |
# at least $length alphanumeric characters. | |
read_length=`expr $length \* 16` | |
head -c $read_length < /dev/urandom | tr -dc [:alnum:] | head -c $length && echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment