summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlshprung <lshprung@yahoo.com>2021-03-15 10:49:24 -0700
committerlshprung <lshprung@yahoo.com>2021-03-15 10:49:24 -0700
commit24ec542e005faa8ba3e95f73b899c2ad865199e9 (patch)
treef33a90cf0637ae52de8c700fdb1510a8ac64a9d4
First commit
-rw-r--r--README.md12
-rwxr-xr-xrandint.sh38
2 files changed, 50 insertions, 0 deletions
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
+