summaryrefslogtreecommitdiffstats
path: root/pkgs/shell/genpasswd
blob: a6d96aa70b84365656067a5bb0b3d5b959dbeabb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /bin/sh
#!buildShellBin path=coreutils
#
# NAME
#       genpasswd - generate a password
#
# SYNOPSIS
#       genpasswd [OPTION]...
#
# DESCRIPTION
#       Produce a random string of a given length and alphabet to standard
#       output.
#
#       --length=number (default: 71)
#         Specify the number of bytes to produce.
#
#       --alphabet=string (default: -+.,=/A-Za-z0-9_)
#         Specify the list of characters that can be produced.
#         The string gets interpreted by tr and may contain single-character
#         collating elements. See tr(1) for details.
#
#       --newline=bool (default: true)
#         Specify whether a newline should be appended to the output.
#

set -efu

alphabet=-+.,=/A-Za-z0-9_
length=71
newline=true

while test $# -gt 0; do
  case $1 in
    --alphabet=*)
      alphabet=${1//--alphabet=}
      shift
      ;;
    --length=*)
      length=${1//--length=}
      shift
      ;;
    --newline=true|--newline=false)
      newline=${1//--newline=}
      shift
      ;;
    *)
      echo "$0: bad argument: $1" >&2
      exit 1
  esac
done

tr -dc -- "$alphabet" < /dev/urandom | dd status=none bs="$length" count=1

case $newline in true)
  echo
esac