#! /usr/bin/env bash # # Add name, id and other relevant info up here # # Declare a variable A as an integer # Check to see if anything is passed to script and store passed value in variable A # If nothing is passed to script then store random number (range 1-15) to variable A # $# shows how many command line arguments are there if it is zero there are no command line arguments. if [ $# -eq 0 ] then A=$((RANDOM%15+1)) else A=$1 fi # If variable A contains the value 0 then replace it with a random number (range 1-20) if [ "$A" -eq 0 ] then A=$((RANDOM%20+1)) fi # Function ADD: If value is within range 1-5 add two to that number four times ADD() { if [ "$A" -gt 1 -a "$A" -lt 5 ] then ans=$((A+A+A)) echo $ans fi } # Function CUBE: If value is within range 6-10 find the cube of that number CUBE() { if [ "$A" -gt 6 -a "$A" -lt 10 ] then ans=$((A*A*A)) echo $ans fi } # Function FACT: If 11 or more find the factorial and divide result by a random number FACT() { if [ $A -lt 11 ] then if [ $A -lt 2 ] then $ans=1 else N=$(( $( FACT $((A - 1)) ) * $A )) ans= $((N/RANDOM)) echo $ans fi fi } PS3='Please enter an option: ' options=( "Option 1" "Option 2" "Option 3" "Quit" ) # an array select opt in "${options[@]}" # the @ refers to a sequence of values or an array. So basically it is saying for all the items in the array options... do case $opt in "Option 1") ADD $A ;; "Option 2") CUBE $A ;; "Option 3") FACT $A ;; "Quit") break ;; *) echo "Invalid option";; esac done # use shellcheck.net to check for errors in the script. #NOTES: # $((EXPR)) - Arithmetic expansion used to make calculations in bash.