Skip to content

Instantly share code, notes, and snippets.

@albertoalcolea
Last active December 20, 2015 19:29
Show Gist options
  • Save albertoalcolea/6183853 to your computer and use it in GitHub Desktop.
Save albertoalcolea/6183853 to your computer and use it in GitHub Desktop.
Another stupid password generator in bash
#! /bin/bash
#
# Version: 1.0
# Date: 31-07-2013
# Author: Alberto Alcolea (contact@albertoalcolea.com)
#
function usage() {
echo "USAGE: $0 [OPTIONS] [LENGTH]"
echo " Options:"
echo " -h, --help: this help."
echo " -l: lower case characters"
echo " -d: digits"
echo " -u: upper case characters"
echo " -o: other printable characters"
echo
echo "Default options: -ld 6"
}
# Default options
LENGTH=6
SET=''
# Get the args
while [ $# -gt 0 ]
do
if [ "$1" = '-h' ] || [ "$1" = '--help' ]
then
usage
exit 0
elif [[ "$1" =~ ^-[lduo]+$ ]]
then
if [[ "$1" =~ l ]]
then
SET=$SET'[:lower:]'
fi
if [[ "$1" =~ d ]]
then
SET=$SET'[:digit:]'
fi
if [[ "$1" =~ u ]]
then
SET=$SET'[:upper:]'
fi
if [[ "$1" =~ o ]]
then
SET=$SET'[:punct:]'
fi
elif [ $# -eq 1 ] && [[ "$1" =~ ^[0-9]+$ ]]
then
LENGTH="$1"
else
usage
exit 1
fi
shift
done
# Default characters set
if [ ! $SET ]
then
SET='[:lower:][:digit:]'
fi
# Print the password
echo $(cat /dev/urandom | tr -dc $SET | fold -w $LENGTH | head -n 1)
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment