Skip to content

Instantly share code, notes, and snippets.

@steverichey
Last active May 15, 2018 09:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steverichey/0388a53e8c8c6159f2b4 to your computer and use it in GitHub Desktop.
Save steverichey/0388a53e8c8c6159f2b4 to your computer and use it in GitHub Desktop.
Generates a random string of a specified length.
#!/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