Create a Command-Line Calculator Script

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will be creating a shell script that acts as a real calculator. The script will take two numbers and an operator as command-line arguments and perform the arithmetic operation accordingly. We will be using the expr or bc commands to perform the arithmetic operations. We will also be using piping to display the output.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/BasicSystemCommandsGroup -.-> linux/bc("`Arithmetic Calculations`") linux/FileandDirectoryManagementGroup -.-> linux/which("`Command Locating`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/case("`Case Statements`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills shell/if_else -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} linux/exit -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} linux/echo -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} linux/pipeline -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} linux/bc -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} linux/which -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/shebang -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/comments -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/quoting -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/variables_decl -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/variables_usage -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/case -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/cond_expr -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/exit_status -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/arith_ops -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/cmd_substitution -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/subshells -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} shell/globbing_expansion -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} linux/wildcard -.-> lab-18863{{"`Create a Command-Line Calculator Script`"}} end

Real Calculator

In this challenge, you are required to create an arithmetic calculator using command-line arguments in a Bash script. The calculator should be able to perform basic arithmetic operations based on user input.

Tasks

You need to complete the following task:

  • Create a Bash script that accepts two numbers and an operator as command-line arguments and performs the specified arithmetic operation.

Requirements

You must meet the following requirements:

  • The script should be named arithematic.sh.
  • The script should be located in the ~/project directory.
  • Use expr or bc commands to perform arithmetic operations.

Example

labex:project/ $ bash arithematic.sh 1 + 5
[OUTPUT] Arithematic operation performed is " +" [OUTPUT]
[OUTPUT] Therefore 1+5=6 [OUTPUT]
labex:project/ $ bash arithematic.sh 12 x 2
[OUTPUT] Arithematic operation performed is " x" [OUTPUT]
[OUTPUT] Therefore 12x2=24 [OUTPUT]

Summary

After completing this challenge, you will have a better understanding of how to use command-line arguments in shell scripts, how to perform arithmetic operations using expr or bc commands, and how to use piping to display the output. You will also have a working calculator script that you can use for your future projects.

Other Linux Tutorials you may like