Four Function Calculator

ShellShellBeginner
Practice Now

Introduction

In this challenge, you'll create a basic four-function calculator in a shell script. This will help you understand the fundamentals of defining and using multiple functions in shell scripting.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) shell(("Shell")) -.-> shell/VariableHandlingGroup(["Variable Handling"]) shell(("Shell")) -.-> shell/ControlFlowGroup(["Control Flow"]) shell(("Shell")) -.-> shell/FunctionsandScopeGroup(["Functions and Scope"]) shell(("Shell")) -.-> shell/AdvancedScriptingConceptsGroup(["Advanced Scripting Concepts"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") shell/VariableHandlingGroup -.-> shell/variables_usage("Variable Usage") shell/ControlFlowGroup -.-> shell/if_else("If-Else Statements") shell/FunctionsandScopeGroup -.-> shell/func_def("Function Definition") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("Arithmetic Expansion") subgraph Lab Skills linux/echo -.-> lab-388893{{"Four Function Calculator"}} shell/variables_usage -.-> lab-388893{{"Four Function Calculator"}} shell/if_else -.-> lab-388893{{"Four Function Calculator"}} shell/func_def -.-> lab-388893{{"Four Function Calculator"}} shell/arith_expansion -.-> lab-388893{{"Four Function Calculator"}} end

Create Calculator Functions

Tasks

  1. Navigate to the ~/project directory where you'll find a partially completed script named calculator.sh.
  2. Open the calculator.sh file and complete the four functions: add, subtract, multiply, and divide.

Requirements

  • The script calculator.sh is already created in the ~/project directory with a basic structure.
  • Your task is to complete the following functions:
    • add: Takes two parameters and returns their sum.
    • subtract: Takes two parameters and returns the result of subtracting the second from the first.
    • multiply: Takes two parameters and returns their product.
    • divide: Takes two parameters and returns the result of dividing the first by the second. Remember to handle division by zero.
  • Each function should take two parameters and echo the result.
  • The main part of the script (which calls the functions) is already provided.

Example

Here's an example of how the completed script should work:

$ ./calculator.sh
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): +
Result: 15

$ ./calculator.sh
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): -
Result: 5

$ ./calculator.sh
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): *
Result: 50

$ ./calculator.sh
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): /
Result: 2

$ ./calculator.sh
Enter first number: 10
Enter second number: 0
Enter operation (+, -, *, /): /
Error: Division by zero is not allowed.

The script's strings must reference the examples and remain unchanged to prevent test failures.

โœจ Check Solution and Practice

Summary

In this challenge, you created a four-function calculator using shell scripting. You practiced defining multiple functions that take parameters, perform calculations, and return results. This exercise reinforced your understanding of basic function declaration and usage in shell scripts, demonstrating practical applications for simple computations and error handling.