Skip to content

Instantly share code, notes, and snippets.

@skreuzer
Created October 27, 2010 02:43
Show Gist options
  • Save skreuzer/648320 to your computer and use it in GitHub Desktop.
Save skreuzer/648320 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ksh
RANDOM=$$
if [ "$#" -ne "1" ]; then
echo "Usage: ${0##*/} PASSWORDLENTGH"
echo "\te.g. ${0##*/} 8"
exit 1
fi
if [ ! -z `echo "$1" | tr -d "[:digit:]"` ]; then
echo "Usage: ${0##*/} PASSWORDLENTGH"
echo "\te.g. ${0##*/} 8"
echo "\nError: PASSWORDLENGTH must be a number!"
exit 1
fi
if [ ! "$1" -gt "0" ]; then
echo "Usage: ${0##*/} PASSWORDLENTGH"
echo "\te.g. ${0##*/} 8"
echo "\nError: PASSWORDLENGTH must be greater then 0!"
exit 1
fi
STRING='q w e r t y u i o p a s d f g h j k l z x c v b n m Q W E R T Y U I O P A S D F G H J K L Z X C V B N M 1 2 3 4 5 6 7 8 9 0 _ - ! ? = '
LENGTH="$1"
typeset -i index
index=1
IFS_SAV="$IFS"
IFS=" "
# Put $STRING in an array
for i in $(echo $STRING); do
array[$index]=$i
((index=index+1))
done
string_len=${#array[*]}
IFS="$IFS_SAV"
typeset -i pwlen
pwlen=0
PASS=""
while [ "$pwlen" -lt "$LENGTH" ]; do
index=$(($RANDOM % $string_len))
PASS="$PASS${array[$index]}"
((pwlen=pwlen+1))
done
echo $PASS
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment