Last active
December 20, 2015 19:29
-
-
Save albertoalcolea/6183853 to your computer and use it in GitHub Desktop.
Another stupid password generator in bash
This file contains hidden or 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/bash | |
# | |
# Version: 1.0 | |
# Date: 31-07-2013 | |
# Author: Alberto Alcolea ([email protected]) | |
# | |
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