On this put up, I shall be discussing the scripts from a mathematical and numerical perspective. Though I posted a extra advanced script (Easy Calculator) within the earlier put up, it was tough for customers to grasp. Therefore, I believed I might educate you one other helpful side of studying in smaller components.
Earlier than this text, three articles within the Shell Scripting Sequence have been printed and they’re:
Let’s proceed the training course of with thrilling new shell scripts, beginning with mathematical ones.
1. Primary Addition Shell Script
On this script ‘addition.sh‘, you’ll create a shell script to carry out fundamental addition operations.
vi addition.sh
Add the next code to addition.sh file.
#!/bin/bash
# This script provides two numbers offered by the consumer
echo “Enter the primary quantity: “
learn a
echo “Enter the second quantity: “
learn b
sum=$((a + b))
echo “The sum of $a and $b is: $sum”
Subsequent, make the script executable and run it.
chmod 755 addition.sh
./addition.sh
2. Primary Subtraction Shell Script
On this script ‘subtraction.sh‘, you’ll create a shell script to carry out a fundamental subtraction operation.
vi subtraction.sh
Add the next code to subtraction.sh file:
#!/bin/bash
# This script subtracts two numbers offered by the consumer
echo “Enter the First Quantity: “
learn a
echo “Enter the Second Quantity: “
learn b
x=$(($a – $b))
echo “$a – $b = $x”
Subsequent, make the script executable and run it.
chmod 755 subtraction.sh
./subtraction.sh
![Basic Subtraction Shell Script](https://www.tecmint.com/wp-content/uploads/2013/08/Basic-Subtraction-Shell-Script.png)
3. Primary Multiplication Shell Script
To this point you’ll be having fun with lots, studying scripts in such a straightforward method, so the subsequent in chronological order is Multiplication.
vi multiplication.sh
Add the next code to multiplication.sh file.
#!/bin/bash
# This script multiplies two numbers offered by the consumer
echo “Enter the primary quantity: “
learn a
echo “Enter the second quantity: “
learn b
product=$((a * b))
echo “The product of $a and $b is: $product”
Subsequent, make the script executable and run it.
chmod 755 multiplication.sh
./multiplication.sh
![Basic Multiplication Shell Script](https://www.tecmint.com/wp-content/uploads/2013/08/Basic-multiplication-Shell-Script.png)
4. Primary Division Shell Script
On this division script, it prompts the consumer to enter two numbers, then divides the primary quantity by the second and shows the consequence.
vi division.sh
Add the next code to the division.sh file.
#!/bin/bash
echo “Enter the First Quantity: “
learn a
echo “Enter the Second Quantity: “
learn b
echo “$a / $b = $(expr $a / $b)”
Subsequent, make the script executable and run it.
chmod 755 division.sh
./division.sh
![Basic Division Shell Script](https://www.tecmint.com/wp-content/uploads/2013/08/Basic-division-Shell-Script.png)
5. Print Multiplication Desk in Bash
On this script, we’ll print the multiplication desk of a user-specified quantity. It prompts the consumer to enter a quantity after which shows the multiplication outcomes from 1 to 10 for that quantity.
vi desk.sh
Add the next code to the desk.sh file.
#!/bin/bash
echo “Enter the quantity for which you wish to print the desk: “
learn n
i=1
whereas [ $i -le 10 ]
do
desk=$(expr $i * $n)
echo “$i x $n = $desk”
i=$(expr $i + 1)
finished
Subsequent, make the script executable and run it.
chmod 755 desk.sh
./desk.sh
![Print Multiplication Table in Bash](https://www.tecmint.com/wp-content/uploads/2013/08/Print-Multiplication-Table-in-Bash.png)
6. Examine If a Quantity is Even or Odd in Bash
On this Bash script, we’ll decide whether or not a user-provided quantity is even or odd. It prompts the consumer to enter a quantity, checks its divisibility by 2 utilizing the modulo operator, after which prints whether or not the quantity is even or odd primarily based on the consequence.
vi evenodd.sh
Add the next code to evenodd.sh file.
#!/bin/bash
echo “Enter the quantity:”
learn n
num=$(expr $n % 2)
if [ $num -eq 0 ]
then
echo “$n is an Even Quantity”
else
echo “$n is an Odd Quantity”
fi
Subsequent, make the script executable and run it.
chmod 755 evenodd.sh
./evenodd.sh
![Check If a Number is Even or Odd in Bash](https://www.tecmint.com/wp-content/uploads/2013/08/Check-Number-Even-or-Odd-in-Bash.png)
7. Calculate Factorial Utilizing Bash Script
This script calculates the factorial of a user-input quantity utilizing a `whereas` loop, after which prints the consequence.
vi factorial.sh
Add the next code to the factorial.sh file.
#!/bin/bash
echo “Enter the quantity:”
learn a
reality=1
whereas [ $a -gt 0 ]
do
reality=$(expr $reality * $a)
a=$(expr $a – 1)
finished
echo “The factorial is: $reality”
Subsequent, make the script executable and run it.
chmod 755 factorial.sh
./factorial.sh
![Calculate Factorial Using Bash Script](https://www.tecmint.com/wp-content/uploads/2013/08/Calculate-Factorial-Number.png)
Now you can loosen up understanding that calculating `12*11*10*9*8*7*6*5*4*3*2*1` is far less complicated with the script above than doing it manually. Think about needing to seek out `99!` – this script shall be extremely helpful in such conditions.
8. Examine if a Quantity is an Armstrong Quantity in Bash
This Bash script determines whether or not a given three-digit quantity is an Armstrong quantity. An Armstrong quantity (or Narcissistic quantity) is a quantity the place the sum of the cubes of its digits equals the quantity itself.
For instance, 371 is an Armstrong quantity as a result of 3×3×3+7×7×7+1×1×1 =371.
vi armstrong.sh
Add the next code to armstrong.sh file.
#!/bin/bash
echo “Enter a quantity:”
learn n
arm=0
temp=$n
whereas [ $n -ne 0 ]
do
r=$(expr $n % 10)
arm=$(expr $arm + $r * $r * $r)
n=$(expr $n / 10)
finished
echo “Calculated sum of cubes: $arm”
if [ $arm -eq $temp ]
then
echo “Armstrong”
else
echo “Not Armstrong”
fi
Subsequent, make the script executable and run it.
chmod 755 armstrong.sh
./armstrong.sh
![Check if a Number is an Armstrong Number](https://www.tecmint.com/wp-content/uploads/2013/08/Check-Number-is-Armstrong-Number.png)
9. Examine if a Quantity is Prime in Bash
This Bash script checks if a quantity is prime by counting its divisors. It prints “Prime” if the quantity has precisely two divisors, and “Not Prime” in any other case.
vi prime.sh
Add the next code to prime.sh file.
#!/bin/bash
echo “Enter any quantity:”
learn n
# Initialize variables
i=1
c=0
# Examine for divisors
whereas [ $i -le $n ]
do
if [ $(expr $n % $i) -eq 0 ]
then
c=$(expr $c + 1)
fi
i=$(expr $i + 1)
finished
# Decide if the quantity is prime
if [ $c -eq 2 ]
then
echo “Prime”
else
echo “Not Prime”
fi
Subsequent, make the script executable and run it.
chmod 755 prime.sh
./prime.sh
![Check if a Number is Prime in Bash](https://www.tecmint.com/wp-content/uploads/2013/08/Check-if-a-Number-is-Prime.png)
That’s all for now. In our subsequent article, we’ll cowl different mathematical applications in shell scripting. Don’t overlook to share your ideas within the feedback part.