Last active
May 15, 2018 09:28
-
-
Save steverichey/0388a53e8c8c6159f2b4 to your computer and use it in GitHub Desktop.
Generates a random string of a specified length.
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
#!/bin/sh | |
set -e | |
if [ "$#" -lt 0 ]; then | |
echo "Usage: pw 16 [lim]" | |
exit 1 | |
fi | |
if [ "$#" -gt 2 ]; then | |
echo "Usage: pw 16 [lim]" | |
exit 1 | |
fi | |
if [ $1 -gt 65535 ]; then | |
echo "Requested string is too large" | |
exit 1 | |
fi | |
if [ "$#" -gt 1 ] && [ "$2" != "lim" ]; then | |
echo "Unrecognized second parameter" | |
exit 1 | |
fi | |
if [ "$#" -gt 1 ] && [ "$2" = "lim" ]; then | |
cat /dev/random | env LC_CTYPE=C tr -cd 'a-z0-9' | head -c $1 | |
else | |
cat /dev/random | env LC_CTYPE=C tr -cd 'a-zA-Z0-9`~!@#$%^&*()_+-={}[]|;<,>.?/' | head -c $1 | |
fi | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment