Last active
January 2, 2016 08:59
-
-
Save cu39/8280015 to your computer and use it in GitHub Desktop.
Random string generator on shell
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
#!/usr/bin/env bash | |
# ref http://d.hatena.ne.jp/pasela/20120710/random | |
usage() { | |
cat << EOF | |
Usage: ${0##*/} [-l letter_set] [-w word_num] [-n number_of_passwords] | |
Generates random letters for password. | |
OPTIONS: | |
-l string Set of letters to be picked up. | |
-g regexp Regexp for 'grep -i'. You MUST define a possible pattern, | |
one of the letters in '-l' param for example, | |
otherwise never terminate. | |
-w integer Number of letters a password. | |
-n integer Number of passwords to generate. | |
-h Show this message. | |
EOF | |
exit 0; | |
} | |
# default | |
letter_set='0-9A-Za-z_-' | |
reg='[_-]' | |
letter_num=16 | |
line=20 | |
while getopts "l:g:w:n:h" opt; do | |
case $opt in | |
l) letter_set=${OPTARG} ;; | |
g) reg=${OPTARG} ;; | |
w) letter_num=${OPTARG} ;; | |
n) line=${OPTARG} ;; | |
h) usage ;; | |
esac | |
done | |
cat /dev/urandom \ | |
| LC_CTYPE=C tr -dc $letter_set \ | |
| fold -w $letter_num \ | |
| grep -i $reg \ | |
| head -n $line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment