|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles |
| 4 | + |
| 5 | +# GS |
| 6 | +# Mar. 2022 |
| 7 | + |
| 8 | +# DESCRIPTION: |
| 9 | +# |
| 10 | +# Random number generator for bash. You can specify a range, and it will output a random number |
| 11 | +# in that range--even a negative number if desired. |
| 12 | + |
| 13 | +# INSTALLATION INSTRUCTIONS: |
| 14 | +# 1. Create a symlink in ~/bin to this script so you can run it from anywhere. |
| 15 | +# cd /path/to/here |
| 16 | +# mkdir -p ~/bin |
| 17 | +# ln -si "${PWD}/random.sh" ~/bin/random # required |
| 18 | +# ln -si "${PWD}/random.sh" ~/bin/gs_random # optional; replace "gs" with your initials |
| 19 | +# 2. Now you can use this command directly anywhere you like like in any of these ways: |
| 20 | +# 1. `random` |
| 21 | +# 2. `gs_random` |
| 22 | + |
| 23 | +# REFERENCES: |
| 24 | +# 1. [my own answer with this code]: https://stackoverflow.com/a/71388666/4561887 |
| 25 | +# 1. `man bash`, then search for "RANDOM" |
| 26 | +# 1. How to generate random number in Bash?: https://stackoverflow.com/a/1195035/4561887 |
| 27 | + |
| 28 | + |
| 29 | +RETURN_CODE_SUCCESS=0 |
| 30 | +RETURN_CODE_ERROR=1 |
| 31 | + |
| 32 | +HELP_STR="\ |
| 33 | +Generate a random integer number according to the usage styles below. |
| 34 | +
|
| 35 | +USAGE STYLES: |
| 36 | + 'random' |
| 37 | + Generate a random number from 0 to 32767, inclusive (same as bash variable 'RANDOM'). |
| 38 | + 'random <max>' |
| 39 | + Generate a random number from 0 to 'max', inclusive. |
| 40 | + 'random <min> <max>' |
| 41 | + Generate a random number from 'min' to 'max', inclusive. Both 'min' and 'max' can be |
| 42 | + positive OR negative numbers, and the generated random number can be negative too, so |
| 43 | + long as the range (max - min + 1) is less than or equal to 32767. Max must be >= min. |
| 44 | +
|
| 45 | +This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles |
| 46 | +See: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/random.sh |
| 47 | +" |
| 48 | + |
| 49 | +print_help() { |
| 50 | + echo "$HELP_STR" | less -RFX |
| 51 | +} |
| 52 | + |
| 53 | +# Get a random number according to the usage styles above. |
| 54 | +# See also `utils_rand()` in utilities.c: |
| 55 | +# https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/utilities.c#L176 |
| 56 | +random() { |
| 57 | + # PARSE ARGUMENTS |
| 58 | + |
| 59 | + # help menu |
| 60 | + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then |
| 61 | + print_help |
| 62 | + exit $RETURN_CODE_SUCCESS |
| 63 | + fi |
| 64 | + |
| 65 | + # 'random' |
| 66 | + if [ $# -eq 0 ]; then |
| 67 | + min=0 |
| 68 | + max="none" |
| 69 | + # 'random max' |
| 70 | + elif [ $# -eq 1 ]; then |
| 71 | + min=0 |
| 72 | + max="$1" |
| 73 | + # 'random min max' |
| 74 | + elif [ $# -eq 2 ]; then |
| 75 | + min="$1" |
| 76 | + max="$2" |
| 77 | + else |
| 78 | + echo "ERROR: too many arguments." |
| 79 | + exit "$RETURN_CODE_ERROR" |
| 80 | + fi |
| 81 | + |
| 82 | + # CHECK FOR ERRORS |
| 83 | + |
| 84 | + if [ "$max" = "none" ]; then |
| 85 | + rand="$RANDOM" |
| 86 | + echo "$rand" |
| 87 | + exit "$RETURN_CODE_SUCCESS" |
| 88 | + fi |
| 89 | + |
| 90 | + if [ "$max" -lt "$min" ]; then |
| 91 | + echo "ERROR: max ($max) < min ($min). Max must be >= min." |
| 92 | + exit "$RETURN_CODE_ERROR" |
| 93 | + fi |
| 94 | + |
| 95 | + # CALCULATE THE RANDOM NUMBER |
| 96 | + |
| 97 | + # See `man bash` and search for `RANDOM`. This is a limitation of that value. |
| 98 | + RAND_MAX=32767 |
| 99 | + |
| 100 | + range=$((max - min + 1)) |
| 101 | + if [ "$range" -gt "$RAND_MAX" ]; then |
| 102 | + echo "ERROR: the range (max - min + 1) is too large. Max allowed = $RAND_MAX, but actual" \ |
| 103 | + "range = ($max - $min + 1) = $range." |
| 104 | + exit "$RETURN_CODE_ERROR" |
| 105 | + fi |
| 106 | + |
| 107 | + # NB: `RANDOM` is a bash built-in variable. See `man bash`, and also here: |
| 108 | + # https://stackoverflow.com/a/1195035/4561887 |
| 109 | + rand=$((min + (RANDOM % range))) |
| 110 | + echo "$rand" |
| 111 | +} |
| 112 | + |
| 113 | +# Set the global variable `run` to "true" if the script is being **executed** (not sourced) and |
| 114 | +# `main` should run, and set `run` to "false" otherwise. One might source this script but intend |
| 115 | +# NOT to run it if they wanted to import functions from the script. |
| 116 | +# See: |
| 117 | +# 1. *****https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/bash/argument_parsing__3_advanced__gen_prog_template.sh |
| 118 | +# 1. my answer: https://stackoverflow.com/a/70662049/4561887 |
| 119 | +# 1. https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/bash/check_if_sourced_or_executed.sh |
| 120 | +run_check() { |
| 121 | + # This is akin to `if __name__ == "__main__":` in Python. |
| 122 | + if [ "${FUNCNAME[-1]}" == "main" ]; then |
| 123 | + # This script is being EXECUTED, not sourced |
| 124 | + run="true" |
| 125 | + fi |
| 126 | +} |
| 127 | + |
| 128 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 129 | +# Main program entry point |
| 130 | +# ---------------------------------------------------------------------------------------------------------------------- |
| 131 | + |
| 132 | +# Only run main function if this file is being executed, NOT sourced. |
| 133 | +run="false" |
| 134 | +run_check |
| 135 | +if [ "$run" == "true" ]; then |
| 136 | + random "$@" |
| 137 | +fi |
0 commit comments