Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # +-----------------------------------------------------------------------------------------+
- # | Bash random string generator |
- # | Handy for passwords or keys |
- # | |
- # | |
- # | November 2011 flip hess flip@nerdmetbril.nl |
- # +-----------------------------------------------------------------------------------------+
- # Global variables:
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- ARGS="${#}"
- CHARSET="A-Za-z0-9!@\#\$%^&*\(\)_+=-[]><~}{"
- # CHARSET="[A-Za-z0-9]"
- # CHARSET="[A-Za-z]"
- # CHARSET="[0-9]
- # Functions:
- # exit function
- function die()
- {
- echo "${1}"
- exit 1
- }
- # Shows usage function.
- function fShowUsage()
- {
- echo -e "\n\
- Usage: ${SCRIPT_PATH} <total chars>\n\
- Create a set string of random characters
- Handy for passwords, keys etc.
- November 2011 flip@nerdmetbril.nl\n\
- "
- return 0
- }
- # Create random string
- function fRandom()
- {
- local LENGTH="${1}"
- # check for arguments:
- [ ${#} = 1 ] || { fShowUsage ; exit 1; }
- # check if arg only contains numbers:
- CHECK=$( echo ${LENGTH} | tr -d "[0-9]")
- [ -z ${CHECK} ] || { fShowUsage ; exit 1; }
- # GENERATE STRING
- PASS="$( < /dev/urandom tr -dc "${CHARSET}" | head -c "${LENGTH}" )"
- # Output
- echo -e "Your ${LENGTH} characters string is:\n ${PASS}"
- }
- # Start the program:
- fRandom "${@}"
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement