Adding Two Numbers

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will write a shell script to add two numbers entered by the user. The script will accept real numbers as input and use the bc command and piping to perform the addition operation. We will also learn about piping and arithmetic operations in shell scripting.


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/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/logical("`Logic Operations`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") 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/while_loops("`While Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/AdvancedScriptingConceptsGroup -.-> shell/here_strings("`Here Strings`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills shell/if_else -.-> lab-18319{{"`Adding Two Numbers`"}} linux/echo -.-> lab-18319{{"`Adding Two Numbers`"}} linux/pipeline -.-> lab-18319{{"`Adding Two Numbers`"}} linux/redirect -.-> lab-18319{{"`Adding Two Numbers`"}} linux/logical -.-> lab-18319{{"`Adding Two Numbers`"}} linux/read -.-> lab-18319{{"`Adding Two Numbers`"}} linux/printf -.-> lab-18319{{"`Adding Two Numbers`"}} linux/bc -.-> lab-18319{{"`Adding Two Numbers`"}} linux/which -.-> lab-18319{{"`Adding Two Numbers`"}} shell/shebang -.-> lab-18319{{"`Adding Two Numbers`"}} shell/comments -.-> lab-18319{{"`Adding Two Numbers`"}} shell/quoting -.-> lab-18319{{"`Adding Two Numbers`"}} shell/variables_decl -.-> lab-18319{{"`Adding Two Numbers`"}} shell/variables_usage -.-> lab-18319{{"`Adding Two Numbers`"}} shell/case -.-> lab-18319{{"`Adding Two Numbers`"}} shell/while_loops -.-> lab-18319{{"`Adding Two Numbers`"}} shell/cond_expr -.-> lab-18319{{"`Adding Two Numbers`"}} shell/arith_ops -.-> lab-18319{{"`Adding Two Numbers`"}} shell/arith_expansion -.-> lab-18319{{"`Adding Two Numbers`"}} shell/read_input -.-> lab-18319{{"`Adding Two Numbers`"}} shell/cmd_substitution -.-> lab-18319{{"`Adding Two Numbers`"}} shell/subshells -.-> lab-18319{{"`Adding Two Numbers`"}} shell/adv_redirection -.-> lab-18319{{"`Adding Two Numbers`"}} shell/here_strings -.-> lab-18319{{"`Adding Two Numbers`"}} shell/globbing_expansion -.-> lab-18319{{"`Adding Two Numbers`"}} linux/wildcard -.-> lab-18319{{"`Adding Two Numbers`"}} end

Adding Two Numbers

In this challenge, you are tasked with creating a script to add two numbers, including real numbers. The script will use the bc command and piping to perform the addition operation.

Tasks

You need to complete the following task:

  1. Write a script that asks the user to enter two numbers, allowing for real numbers as input.
  2. Use the bc command and piping to perform the addition operation.

Requirements

You must meet the following requirements:

  • Script name: add2No.sh
  • Work directory: ~/project

Example

Sample Execution / Output:

labex:project/ $ bash add2No.sh
[INPUT] Enter the value of Number1 [INPUT]
12
[INPUT] Enter the value of Number2 [INPUT]
13
[INPUT] Enter the method to be used in Addition operation [INPUT]
1) method 1 uses expr command to add two numbers
2) method 2 uses bc (floating point is accepted) command to add two numbers
3) method 3 uses (( )) command to add two numbers
1

[OUTPUT] OPERATION PERFORMED
12+13 = 25

Summary

After completing this challenge, you will have learned how to:

  • Accept user input in shell script.
  • Perform arithmetic operations in shell script.
  • Use the bc command and piping to perform floating-point arithmetic operations.
  • Understand the concept of piping in shell scripting.

Other Linux Tutorials you may like