LDP/LDP/guide/docbook/abs-guide/rand-string.sh

31 lines
687 B
Bash
Raw Normal View History

2007-11-08 15:53:07 +00:00
#!/bin/bash
# rand-string.sh
# Generating an 8-character "random" string.
2009-01-22 14:43:09 +00:00
if [ -n "$1" ] # If command-line argument present,
2007-11-08 15:53:07 +00:00
then #+ then set start-string to it.
str0="$1"
else # Else use PID of script as start-string.
str0="$$"
fi
POS=2 # Starting from position 2 in the string.
LEN=8 # Extract eight characters.
str1=$( echo "$str0" | md5sum | md5sum )
2012-04-04 22:51:18 +00:00
# Doubly scramble ^^^^^^ ^^^^^^
#+ by piping and repiping to md5sum.
2007-11-08 15:53:07 +00:00
randstring="${str1:$POS:$LEN}"
# Can parameterize ^^^^ ^^^^
echo "$randstring"
exit $?
# bozo$ ./rand-string.sh my-password
# 1bdd88c4
2016-10-24 12:35:31 +00:00
# No, this is not recommended
2007-11-08 15:53:07 +00:00
#+ as a method of generating hack-proof passwords.