BCE-O535 Linux and Shell Programming

0 of 30 lessons complete (0%)

Module 6 Introduction to Shell

Arithmetic Expression

Instructions:

  1. Open a text editor (e.g., nano, vim, gedit) and create a new shell script file named arithmetic_lab.sh.
  2. Write a shell script that performs the following arithmetic operations:
    • Add two numbers.
    • Subtract one number from another.
    • Multiply two numbers.
    • Divide one number by another.
    • Raise a number to the power of another.
    • Find the remainder when one number is divided by another.
  3. Use both expr and double parentheses (( )) methods for arithmetic operations.
  4. Print out the results with descriptive messages.
  5. Save the script.
  6. Make the script executable using the command: chmod +x arithmetic_lab.sh.
  7. Execute the script using: ./arithmetic_lab.sh.

Bonus:

Modify the script to accept user input for the numbers and perform the arithmetic operations based on the input.

Sample Output:

Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2
Exponentiation: 2 raised to the power of 3 = 8
Modulus: 10 % 3 = 1

Questions:

  1. What is the purpose of the shebang line (#!/bin/bash) at the beginning of the script?
  2. Explain the difference between using expr and double parentheses (( )) for arithmetic operations.
  3. How would you modify the script to accept user input for the numbers?
  4. Write a command to make the script executable.
  5. How would you execute the script after making it executable?

Note: Encourage the students to experiment with different numbers and operations to thoroughly test their script.