Instructions:
- Open a text editor (e.g.,
nano
,vim
,gedit
) and create a new shell script file namedarithmetic_lab.sh
. - 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.
- Use both
expr
and double parentheses(( ))
methods for arithmetic operations. - Print out the results with descriptive messages.
- Save the script.
- Make the script executable using the command:
chmod +x arithmetic_lab.sh
. - 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:
- What is the purpose of the shebang line (
#!/bin/bash
) at the beginning of the script? - Explain the difference between using
expr
and double parentheses(( ))
for arithmetic operations. - How would you modify the script to accept user input for the numbers?
- Write a command to make the script executable.
- 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.