~dricottone/my-utils

ref: 581955c8fa8a187f983dc223c947f12e9f9ad474 my-utils/core/rand -rwxr-xr-x 2.2 KiB
581955c8Dominic Ricottone Fix mktar 2 years ago
                                                                                
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash

name="rand"
version="1.0"
read -r -d '' help_message <<-EOF
	Get a random number within an inclusive range
	Usage: rand START END [OPTIONS]
	Options:
	 -h, --help        print this message
	 -n N, --number N  print N numbers (Default: 1)
	 -q, --quiet       suppress error messages and prompts
	 -v, --verbose     show additional messages
	 -V, --version     print version number and exit
	 -w N, --width N   print numbers zero-padded to N-wide (Default: 0)
EOF

source /usr/local/lib/mylib.bash

positional=()
quiet=0
verbose=0
width=1
number=1
while [[ $# -gt 0 ]]; do
  case "$1" in

  -h|--help)
    help_msg
    shift
    ;;

  -n|--number)
    if ! is_positive_integer "$2"; then
      error_msg "Cannot set number of outputs to '${2}' (not an integer >= 1)"
    fi
    debug_msg "Setting number of outputs to ${2} (was ${number})"
    number="$2"
    shift; shift
    ;;

  -q|--quiet)
    debug_msg "Setting quiet option to 1 (was ${quiet})"
    quiet=1
    shift
    ;;

  -v|--verbose)
    debug_msg "Setting verbose option to 1 (was ${verbose})"
    verbose=1;
    shift
    ;;

  -V|--version)
    version_msg
    ;;

  -w|--width)
    if ! is_natural "$2"; then
      error_msg "Cannot set zero-padding width to '${2}' (not an integer >= 0)"
    fi
    debug_msg "Setting zero-padding width to ${2} (was ${width})"
    width="$2";
    shift; shift
    ;;

  *)
    debug_msg "Argument '${1}' added to positional array"
    positional+=("$1")
    shift
    ;;
  esac
done

# check arguments
if [[ ${#positional[@]} -lt 2 ]]; then
  debug_msg "Expected 2 arguments (given ${#positional[@]})"
  usage_msg
else
  range_start="${positional[0]}"
  range_end="${positional[1]}"
  if ! is_integer "$range_start"; then
    error_msg "Expected integer for range start (given '${range_start}')"
  elif ! is_integer "$range_end"; then
    error_msg "Expected integer for range end (given '${range_end}')"
  elif [[ "$range_start" -ge "$range_end" ]]; then
    error_msg "Expected ascending range (given '${range_start}' and '${range_end}')"
  fi
fi

# main routine
code=0
if ! seq -f "%0${width}g" "$range_start" "$range_end" | shuf -n "$number"; then
  code=1
fi

# return stored code
exit "$code"