From 24ec542e005faa8ba3e95f73b899c2ad865199e9 Mon Sep 17 00:00:00 2001 From: lshprung Date: Mon, 15 Mar 2021 10:49:24 -0700 Subject: First commit --- README.md | 12 ++++++++++++ randint.sh | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 README.md create mode 100755 randint.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..77089bb --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# randint.sh + +randint.sh is a bash script that can generate a random integer in a set range. Enter `./randint.sh -h` to see usage. + +## Downloading and Running + +1. Clone the Repository +2. `cd randint` +3. `chmod 755 randint.sh` +4. `./randint.sh` + +The script takes in at least one argument. More information can be found in the help dialogue. diff --git a/randint.sh b/randint.sh new file mode 100755 index 0000000..5b2e1d6 --- /dev/null +++ b/randint.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +print_help() { + echo "Usage: $0 [options] [Min] Max" + echo -e "\nOptions:" + echo " -h, --help print a brief description of this program and give this help list" +} + +get_randint() { + if [ -z $2 ]; then + local min=1 + local max=$1 + else + local min=$1 + local max=$2 + fi + echo $(( $RANDOM % $(( $max - $min + 1 )) + $min )) +} + +if [ -z $1 ] ; then + # No arguments passed + echo "Error: missing Max boundary" + print_help + exit +fi + +case $1 in + -h | --help) + echo "randint.sh is a bash script that can generate a random integer between 1 (or a given Min boundary) and a given Max boundary" + print_help + exit + ;; + + *) + get_randint $1 $2 + ;; +esac + -- cgit