Skip to content

Instantly share code, notes, and snippets.

@hightemp
Created April 10, 2014 21:56
Show Gist options
  • Save hightemp/10427181 to your computer and use it in GitHub Desktop.
Save hightemp/10427181 to your computer and use it in GitHub Desktop.
#!/bin/bash
PASSLENGTH="10"
while [[ $# > 0 ]]; do
key="$1"
shift
case $key in
-h|--help)
echo "usage: passgen [-l length]"
echo "l : password length min 6 chars"
return
;;
-l|--length)
PASSLENGTH=$(($1+0))
shift
;;
esac
done
[ "$PASSLENGTH" -lt 6 ] && PASSLENGTH="6"
let LEN=0
PASSWORD=""
while [ $LEN -lt $PASSLENGTH ]; do
R=$((RANDOM % 3))
case "$R" in
"0") N=$(((RANDOM % 10)+48));; # 48-57
"1") N=$(((RANDOM % 26)+65));; # 65-90
"2") N=$(((RANDOM % 26)+97));; # 97-122
esac
C=`printf "%x" $N`
PASSWORD=$PASSWORD`printf "\x$C"`
LEN=${#PASSWORD}
done
echo $PASSWORD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment