0

I am new to Bash/Scripting in general and I want to create a basic calculator using Bash. I created this code to just test out if I can make the option to just add for now. Here is my code, feel free to let me know if there are more errors than what I mentioned above.

#! /bin/bash
#echo "Testing Calculator. Enter a number:"
#read NUM1
#echo "Your number is: $NUM1"

MENU_OPTIONS(){
echo "***** Menu Options *****"
echo "1 = ADD"
echo "2 = SUBTRACT"
echo "3 = MULTIPLY"
echo "4 = DIVIDE"
echo "Q = EXIT"
echo "************************"
}

ADDITION(){
echo "Please enter first number:"
read NUM_1
echo "Please enter second number:"
read NUM_2
NUM_3 = NUM_1 + NUM_2
echo "$NUM_1 + $NUM_2 equals $NUM_3"
}

USER_INPUT='a'
while [ $USER_INPUT != "Q" ]; do
    
    MENU_OPTIONS
    read USER_INPUT
    
    if [ $USER_INPUT = "1" ]
    then 
        ADDITION
    else
        echo "Bye."
        
    fi
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    You are missing "done" keyword at the end ti close while loop. – andellapie Mar 25 '21 at 20:58
  • 1
    [For starters, ShellCheck](https://www.shellcheck.net/) is always a good idea to find common errors with helpful links! – 0stone0 Mar 25 '21 at 20:58
  • 1) [No space around `=`](https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment) 2) [`done` to close a `while`](https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_02.html) 3) [Missing quotes around variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – 0stone0 Mar 25 '21 at 21:04

1 Answers1

0

You are missing "done" keyword at the end to close while loop

while [ $USER_INPUT != "Q" ]; do   
    MENU_OPTIONS
    read USER_INPUT
    
    if [ $USER_INPUT = "1" ]
    then 
        ADDITION
    else
        echo "Bye."
    
    fi
done

#You also need to calc NUM_3 in this way:

ADDITION(){
    echo "Please enter first number:"
    read NUM_1
    echo "Please enter second number:"
    read NUM_2
    NUM_3=$(( NUM_1 + NUM_2 ))
    echo "$NUM_1 + $NUM_2 equals $NUM_3"
}
andellapie
  • 156
  • 2