output asked headers in the order they were asked; avoid header name spoofing by...
[hband-tools.git] / user-tools / randstr
blob87a7660a468f7a6a41ef0455911a9840effa4040
1 #!/bin/bash
3 true <<'EOF'
5 =pod
7 =head1 NAME
9 randstr - Generate random string from a given set of characters and with a given length.
11 =head1 SYNOPSIS
13 randstr <LENGTH> [<CHARS>]
15 =head1 DESCRIPTION
17 B<CHARS> is a character set expression, see tr(1).
18 Default B<CHARS> is C<[a-zA-Z0-9_]>
20 =cut
22 EOF
24 set -e
25 set +o pipefail
26 set -u
29 if [ "$1" = --help ]
30 then
31 pod2text "$0"
32 exit 0
36 length=$1
37 charset='[a-zA-Z0-9_]'
38 if [ $# -gt 1 ]
39 then
40 charset=$2
43 cat /dev/urandom | tr --delete --complement "$charset" | { read -r -n $length str ; printf %s "$str"; }
45 exit ${PIPESTATUS[2]}