Algorithm
1. Get a number
2. Use for loop or while loop to compute the factorial by using the below formula
3. fact(n) = n * n-1 * n-2 * .. 1
4. Display the result.
Factorial of a number using while loop - Shell Script
#shell script for factorial of a number
#factorial using while loop
echo "Enter a number"
read num
fact=1
while [ $num -gt 1 ]
do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done
echo $fact
gt stands for greater than (>).
Print numbers 1 to 100 using while loop - Shell Script
#shell script to print numbers 1 to 100
i=1
while [ $i -le 100 ]
do
echo $i
i=$(($i+1))
done
-le stands for less than or equal to.
Same program using expr
#shell script to print numbers 1 to 100
#using while loop and expr
i=1
while [ $i -le 100 ]
do
echo $i
i=`expr $i + 1`
done
Print numbers 1 to 100 using for loop - Shell Script
#shell script to print numbers 1 to 100
#using for loop
for((i=1;i<=100;i++))
do
echo $i
done
Shell script to print sum of all digit
Let's write a shell script to print the sum of all digits in a given number.
Algorithm
1. Get a number
2. Split each digit from the number using modulo operator.
3. Calculate the sum
4. Print the result.
Sum of all digits - Shell Script
echo "Enter a number"
read num
sum=0
while [ $num -gt 0 ]
do
mod=$((num % 10)) #It will split each digits
sum=$((sum + mod)) #Add each digit to sum
num=$((num / 10)) #divide num by 10.
done
echo $sum
Shell script to print sum of all digit using expr
#sum of all digits - shell script
echo "Enter a number"
read num
sum=0
while [ $num -gt 0 ]
do
mod=`expr $num % 10` #It will split each digits
sum=`expr $sum + $mod` #Add each digit to sum
num=`expr $num / 10` #divide num by 10.
done
echo $sum
Shell script to find largest of n numbers
Let's write a shell script to find largest of n numbers.
Algorithm
1. Get N
2. Read N numbers using loop
3. Set first number as max.
4. From number 2 onwards update the max value if the number > max.
5. Display the result
Largest of n numbers - Shell Script
#shell script for largest of n numbers
echo "Enter Size(N)"
read N
i=1
max=0
echo "Enter Numbers"
while [ $i -le $N ]
do
read num
if [ $i -eq 1 ] #set first number as max
then
max=$num
else #from number 2 update max if the num > max.
if [ $num -gt $max ]
then
max=$num
fi
fi
i=$((i + 1)) #increment i by 1
done
echo $max
Output
Enter Size(N)
5
Enter Numbers
123
4
56
78
34
123
Enter Size(N)
5
Enter Numbers
1
2
3
4
5
5
Shell program to find average of n numbers
Let's write a shell program to find average of n numbers
Algorithm
1. Get N (Total Numbers)
2. Get N numbers using loop
3. Calculate sum
4. Average = sum / N
5. print the result.
Average of n numbers - Shell Script
#shell script to find average of n numbers
echo "Enter Size(N)"
read N
i=1
sum=0
echo "Enter Numbers"
while [ $i -le $N ]
do
read num #get number
sum=$((sum + num)) #sum+=num
i=$((i + 1))
done
avg=$(echo $sum / $N | bc -l)
echo $avg
We can't directly perform floating point arithmetic in bash.
So that we have used bc "An arbitrary precision calculator language" to calculate
the avg.
Output
Enter Size(N)
5
Enter Numbers
10
20
30
40
50
30.00000000000000000000
Enter Size(N)
3
Enter Numbers
1
2
5
2.66666666666666666666
Use printf to print required floating points
#shell script to find average of n numbers
echo "Enter Size(N)"
read N
i=1
sum=0
echo "Enter Numbers"
while [ $i -le $N ]
do
read num #get number
sum=$((sum + num)) #sum+=num
i=$((i + 1))
done
avg=$(echo $sum / $N | bc -l)
printf '%0.2f' "$avg"
%0.2f will print only 2 floating numbers.
Output
Enter Size(N)
5
Enter Numbers
10
20
30
40
50
30.00
Enter Size(N)
3
Enter Numbers
1
2
5
2.67
Shell script to find factorial of a number
Let's write a shell script to find the factorial of a number.
Algorithm
1. Get a number
2. Use for loop or while loop to compute the factorial by using the below formula
3. fact(n) = n * n-1 * n-2 * .. 1
4. Display the result.
Factorial of a number using while loop - Shell Script
#shell script for factorial of a number
#factorial using while loop
echo "Enter a number"
read num
fact=1
while [ $num -gt 1 ]
do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done
echo $fact
gt stands for greater than (>).
Output
Enter a number
3
6
Enter a number
4
24
Enter a number
5
120
Factorial of a number using for loop - Shell Script
#shell script for factorial of a number
#factorial using for loop
echo "Enter a number"
read num
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact * i)) #fact = fact * i
}
echo $fact
Output
Enter a number
3
6
Enter a number
4
24
Enter a number
5
120
Shell program to find sum of n numbers
Let's write a shell program to find sum of n numbers.
Algorithm
1. Get N (Total Numbers).
2. Get N numbers using loop.
3. Calculate the sum.
4. Print the result.
Sum of n numbers using while loop - Shell Script
#shell script to find sum of n numbers
echo "Enter Size(N)"
read N
i=1
sum=0
echo "Enter Numbers"
while [ $i -le $N ]
do
read num #get number
sum=$((sum + num)) #sum+=num
i=$((i + 1))
done
echo $sum
Output
Enter Size(N)
5
Enter Numbers
1
2
3
4
5
15
Enter Size(N)
3
Enter Numbers
10
20
30
60
Sum of n numbers using for loop- Shell Script
#shell script to find sum of n numbers using for loop
echo "Enter Size(N)"
read N
sum=0
echo "Enter Numbers"
for((i=1;i<=N;i++))
do
read num #get number
sum=$((sum + num)) #sum+=num
done
echo $sum
Output
Enter Size(N)
5
Enter Numbers
1
2
3
4
5
15
Enter Size(N)
3
Enter Numbers
10
20
30
60
How to add array elements in shell script
Let's write a shell script to add array elements in shell script.
Algorithm
1. Declare and initialize the array elements
2. Iterate the array using for loop
3. Add each array element
4. Print the result
Add array elements - Shell Script
#sum of array shell script
arr=(10 20 30 40 50)
sum=0
for i in ${arr[@]}
do
sum=`expr $sum + $i`
done
echo $sum
Using ${arr[@]} or ${arr[*]}, we can access the all array elements.
Output
150
Shell program to add two numbers using functions
Using function keyword, we can declare function in shell script.
In this tutorial, we will learn how to add two numbers using function in shell
script.
Function Syntax in shell script
function fun_name ()
{
#Implement the function here
}
To call the function, just add the function name in the script.
> fun_name
Call function with arguments in shell script
To call the function with arguments, add the argument values after the function
name.
Like,
> fun_name arg1 arg2
In function $1 will refer the value of agr1, and $2 will refer the value of arg2 and
so on.
1. Initialize two variables.
2. Declare and implement the addition function.
3. Call the add function with two arguments.
Add two variables using function in shell script
#$1 will have the value 10 which is the value of a
#$2 will have the value 20 which is the value of b
function add()
{
sum=$(($1 + $2))
echo "Sum = $sum"
}
a=10
b=20
#call the add function and pass the values
add $a $b
Output
Sum = 30
How to compare two strings in unix shell script
In this tutorial, let us discuss how to compare two string in the shell script.
Input
shell shell
Output
Equal
Input
shell bash
Output
Un Equal
Shell script to compare two strings
Example
#shell script to compare two strings
read -p "Enter two strings: " str1 str2
if [ $str1 == $str2 ]
then
echo "Equal"
else
echo "Un Equal"
fi
Output
Enter two strings: shell bash
Un Equal
Enter two strings: shell shell
Equal
Compare variable value with string in shell script
Example
read -p "Enter a string: " str1
if [ $str1 == "linux" ]
then
echo "linux"
elif [ $str1 == "unix" ]
then
echo "unix"
else
echo "Neither linux nor unix"
fi
Output
Enter a string: linux
linux
Enter a string: unix
unix
Enter a string: one
Neither linux nor unix