Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Linux Shell

Program

Chaitanya Sharma
BRANCH : ARTIFILLICAL INTELLIGENCE AND DATA SCIENCE
SEM : 3RD
ROLL NO : 07
SESSION : 2023-2024
1 Linux Shell Program

Index
Sr.no Programs
1. Greatest among three numbers
2. To find a year is leap year or not
3. To input angles of a triangle and find out whether it is valid triangle or not
4. To check whether a character is alphabet, digit or special character
5. To calculate profit or loss
6. Write a shell script to print all even and odd number from 1 to 10
7. Write a shell script to print table of a given number
8. Write a shell script to calculate factorial of a given number.
9. Write a shell script to print sum of all even numbers from 1 to 10.
10. Write a shell script to print sum of digit of any number
11. Write a shell script to make a basic calculator which performs addition, subtraction, Multiplication,
Division
12. Write a shell script to print days of a week
13. Write a shell script to print starting 4 months having 31 days
14. Write a shell script to find a number is Armstrong or not
15. Write a shell script to find a number is palindrome or not
16. Write a shell script to print Fibonacci series
17. Write a shell script to find prime number
18. Write a shell script to convert binary to decimal and decimal to binary
19. Write a Shell script to read and print elements of array
20. Write a Shell script to find sum of all array elements
21. Write a Shell script to find reverse of an array.
22. Write a Shell script to search an element in an array
23. Write a shell script to print different shapes- Diamond
24. Write a shell script to print different : Triangle
25. Write a shell script to print different shapes- Square
26. Write a shell script to print different shapes- Rectangle
27. Write a shell script to print different shapes- Hollow Square

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
2 Linux Shell Program

 then else-if, nested if-else

 Greatest among three numbers:

#!/bin/bash

echo "Enter three numbers: "


read num1
read num2
read num3

if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then


echo "$num1 is greater number."
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
echo "$num2 is greater number."
else
echo "$num3 is greater number."

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
3 Linux Shell Program

 To find if a year is a leap year or not:


Syntax:
#!/bin/bash

echo Enter a year to check whether the year is leap or not


read year

else

if [ $(( year%4)) -eq 0 ]; then


echo $year is leap year
echo $year is not leap year

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
4 Linux Shell Program

 To input angles of a triangle and find out whether it is valid triangle or


not.
Syntax:
#!/bin/bash

echo Enter three angles of triangle


read angle1
read angle2
read angle3

if [ $((angle1+angle2+angle3)) -eq 180 ] && [ $angle1 -gt 0 ] && [ $angle2 -gt 0 ] && [ $angle3 -
gt 0 ]; then
echo the entered angle from a valid triangle

else
echo the entered angle are not from valid triangle

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
5 Linux Shell Program

 To check whether a character is alphabet, digit or special character.


Syntax:
#!/bin/bash

echo Enter a character


read char
if [[ $char =~ [0-9] ]]; then
echo Enter a Charater is digit
elif [[ $char =~ [A-Za-z] ]]; then
echo Enter a Charater is Alphabet
elif [[ $char =~ [A-Za-z0-9] ]]; then
echo Enter a Charater is normal charater
else
echo Enter a Charater is special charater

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
6 Linux Shell Program

 To calculate profit or loss


Syntax:
#1/bin/bash

echo Enter the cost price of item


read Cost_price
echo Enter the selling price of item
read selling_price

if [ $Cost_price -gt $selling_price ]; then


loss=$((Cost_price - selling_price))
echo the loss of item is $loss
elif [ $Cost_price -lt $selling_price ]; then
pro =$((selling_price -Cost_price ))
echo the profit of item is $pro

else
echo there no losses and profit

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
7 Linux Shell Program

 Shell Programming - Looping- while, until, for loops

 Write a shell script to print all even and odd number from 1 to 10.
Syntax:
#!/bin/bash

n=1

echo even number are


while [ $n -le 10 ]
do
if [ $(( n % 2 )) -eq 0 ]; then
echo $n
fi
n=$((n+1))
done

j=1
echo Odd number are
while [ $j -le 10 ]
do
if [ $(( j % 2 )) -ne 0 ]; then
echo $j
fi
j=$((j+1))
done
Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
8 Linux Shell Program

 Write a shell script to print table of a given number


Syntax:
#!/bin/bash
read -p 'Enter a number to print its table ' num

n=1

un l [ $n -gt 10 ]
do
echo $num x $n = $((num*n))
n=$((n+1))
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
9 Linux Shell Program

 Write a shell script to calculate factorial of a given number.


Syntax:
#!/bin/bash

read -p 'Enter a number to calculate the factorial ' num

fac=1

for (( i=1 ; i <= num ; i++ ))


do
fac=$((fac*i))
done

echo factorial of given number is $fac

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
10 Linux Shell Program

 Write a shell script to print sum of all even numbers from 1 to 10


Syntax:
#i/bin/bash

n=1
sum=0
until [ $n -gt 10 ]
do
if [ $((n%2)) -eq 0 ]; then
sum=$((sum+n))
fi
n=$((n+1))
done

echo The sum of all even number from 1 to 10 is $sum

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
11 Linux Shell Program

 Write a shell script to print sum of digit of any number.


Syntax:
#!/bin/bash

echo Enter a number to calculate the sum of digit


read num
sum=0
while [ $num -ne 0 ]
do
sum=$((sum+num%10))
num=$((num/10))
done

echo the sum of digit is $sum

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
12 Linux Shell Program

 Shell Programming - case structure, use of break

 Write a shell script to make a basic calculator which performs addition,


subtraction, Multiplication, division

Syntax:
#!/bin/bash

read -p 'Enter a first number ' num1


read -p 'Enter a second number ' num2

echo Select a opera on


echo 1. Addi on
echo 2.Subtrac on
echo 3.Mul plica on
echo 4.Division

read choose

case $choose in
1)
echo The addi on of two number is $((num1+num2))
;;
2)
echo The subtrac on of two number is $((num1-num2))
;;
3)
echo The mul plica on of two number is $((num1*num2))
;;
4)
if [ $num2 -ne 0 ]; then
echo The division is $((num1/num2))
else
echo Error divison by zero

;;

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
13 Linux Shell Program

*)
echo Invalid choosen
;;
esac

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
14 Linux Shell Program

 Write a shell script to print days of a week.


Syntax:

#!/bin/bash
read -p 'Enter a day (1-7) to get the corresponding day of week' day

case $day in
1) echo Sunday;;
2) echo Monday;;
3) echo Tuesday;;
4) echo Wednesday;;
5) echo Thursday;;
6) echo Friday;;
7) echo Saturday;;
*) echo Invalid input;;
esac

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
15 Linux Shell Program

 Write a shell script to print starting 4 months having 31 days


Syntax:
#!/bin/bash

echo Months with 31 days

months=( 'January' 'February' 'March' 'April' 'May' 'June' 'July' 'August' 'September' 'October'
'November' 'December' )
counter=1
for (( i=0 ; i<12 ; i++ ));
do
case ${months[i]} in
'January' | 'March' | 'May' | 'July' | 'August' | 'October' | 'December' )
echo ${months[i]}

counter=$((counter+1))

if [ $counter -gt 4 ]; then


break

;;
*)
#statment
;;
esac
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
16 Linux Shell Program

 Shell Programming – Functions


14. Write a shell script to find a number is Armstrong or not

Syntax:
#!/bin/bash

Armstrong(){
num=$1
sum=0
temp=$num
temp1=$num
count=0

while [ $temp1 -gt 0 ];do


temp1=$((temp1/10))
count=$((count+1))
done

if [ $count -gt 3 ]; then


echo the digit is larger than 3 digit number

while [ $temp -gt 0 ]; do


digit=$((temp%10))
sum=$((sum + digit**3))
temp=$((temp/10))
done

if [ $num -eq $sum ]; then


echo The number is Armstrong
else
echo The number is not Armstrong

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
17 Linux Shell Program

echo Enter a number to check its armstrong number or not


read armstrong
Armstrong $armstrong

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
18 Linux Shell Program

15.Write a shell script to find a number is palindrome or not


Syntax:

#!/bin/bash

func on palindrone() {
num=$1
sum=0
temp=$num

while [ $temp -gt 0 ];


do
digit=$((temp % 10))
sum=$((sum*10 + digit))
temp=$((temp/10))
done

if [ $num -eq $sum ]; then


echo $num is Palindrone
else
echo $num is not Palindrone

}
echo Enter a number to check is palindrone or not
read input
palindrone $input

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
19 Linux Shell Program

16. Write a shell script to print Fibonacci series.


Syntax:
#!/bin/bash

Fibonacci(){
limit=$1
a=0
b=1

echo fibonacci series are


echo $a
echo $b
for (( i=0; i<limit; i++ )) do
c=$((a+b))
echo $c
a=$b
b=$c
done
}
echo Enter the number of terms for fibonacci series
read term
Fibonacci $term

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
20 Linux Shell Program

17. Write a shell script to find prime number


Syntax:
#!/bin/bash

isPrime(){
num=$1
count=0

for (( i=1; i<=num; i++)); do


if [ $((num%i)) -eq 0 ]; then
count=$((count+1))
fi
done

if [ $count -eq 2 ]; then


echo The number is prine number
else
echo The number is not prime number
fi
}

echo Enter a number to check prime number or not


read number
isPrime $number

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
21 Linux Shell Program

18. Write a shell script to convert binary to decimal and decimal to


binary
Syntax:
#!/bin/bash

binarytodecimal(){
binary=$1
base=1
decimal=0

while [ $binary -gt 0 ]; do


remainder=$((binary % 10 ))
decimal=$((decimal+remainder*base))
binary=$((binary/10))
base=$((base*2))
done

echo Decimal is $decimal


}

decimaltobinary(){
decimal=$1
binary=0

while [ $decimal -gt 0 ]; do


remainder=$((decimal % 2 ))
binary=$((binary*10 + remainder))
decimal=$((decimal/2))
done

temp=$binary
binary=0

while [ $temp -gt 0 ]; do


remainder=$((temp % 10))
binary=$((binary*10 + remainder))

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
22 Linux Shell Program

temp=$((temp / 10 ))
done

echo Binary is $binary


}
echo Select option
echo 1. Binary to Decimal
echo 2. Decimal to Binary

read choose
case $choose in
1)
echo Enter a binary digit
read input
binarytodecimal $input;;
2)
echo Enter a decimal digit
read input
decimaltobinary $input;;
*)
echo Invalid Choose;;
esac

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
23 Linux Shell Program

 Shell Programming – Arrays


19.Write a Shell script to read and print elements of array.

Syntax:
#!/bin/bash
echo Enter the number of element in array
read num

echo Enter the element in array

for ((i=0; i<num; i++));do


read array[i]
done
echo Element of array are
for (( i=0; i<num; i++));
do
echo ${array[i]}
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
24 Linux Shell Program

20.Write a Shell script to find sum of all array elements


Syntax:
#!/bin/bash

echo Enter a element in array


read -a array
sum=0
for (( i=0; i<${#array[@]}; i++)); do
sum=$((sum+${array[i]}))
done

echo the sum of array is $sum

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
25 Linux Shell Program

21.Write a Shell script to find reverse of an array


Syntax:
#!/bin/bash

echo Enter the element in array


read -a array

echo Reverse of array is

for (( i=${#array[@]}; i>0; i-- )); do


echo ${array[i-1]}
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
26 Linux Shell Program

22.Write a Shell script to search an element in an array


Syntax:
#!/bin/bash

echo Enter the element in array


read -a array

echo Enter the element to search


read search
found=0
for (( i=0; i<${#array[@]}; i++)); do

if [ $search -eq ${array[i]} ]; then


found=$((found+1))
fi
done

if [ $found -ne 0 ]; then


echo $search element is found in array
else
echo $search element is not found
fi

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
27 Linux Shell Program

23. Write a shell script to print different shapes- Diamond


Syntax:
#!/bin/bash

echo "Enter the height of the diamond: "


read height

for ((i=1; i<=height; i++)); do

for ((j=1; j<=height-i; j++)); do


echo -n " "
done

for ((k=1; k<=2*i-1; k++)); do


echo -n "*"
done

echo
done

for ((i=height-1; i>=1; i--)); do


# Print spaces before each row
for ((j=1; j<=height-i; j++)); do
echo -n " "
done

for ((k=1; k<=2*i-1; k++)); do


echo -n "*"
done

echo
done

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
28 Linux Shell Program

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
29 Linux Shell Program

24. Write a shell script to print different : Triangle


Syntax:
#!/bin/bash

echo "Enter the height of the triangle: "


read height

for ((i=1; i<=height; i++)); do


for ((j=1; j<=height-i; j++)); do
echo -n " "
done

for ((k=1; k<=2*i-1; k++)); do


echo -n "*"
done

echo
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
30 Linux Shell Program

25.Write a shell script to print different shapes- Square


Syntax:
#!/bin/bash

echo "Enter the side length of the square: "


read side_length

for ((i=1; i<=side_length; i++)); do

for ((j=1; j<=side_length; j++)); do


echo -n "*"
done

echo
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
31 Linux Shell Program

26.Write a shell script to print different shapes- Rectangle


Syntax:
#!/bin/bash

echo "Enter the number of rows for the rectangle: "


read rows

echo "Enter the number of columns for the rectangle: "


read cols

for ((i=1; i<=rows; i++)); do


for ((j=1; j<=cols; j++)); do
echo -n "*"
done

echo
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh
32 Linux Shell Program

27.Write a shell script to print different shapes- Hollow Square


Synatx:
#!/bin/bash

echo "Enter the number of rows for the rectangle: "


read rows

echo "Enter the number of columns for the rectangle: "


read cols

for ((i=1; i<=rows; i++)); do

for ((j=1; j<=cols; j++)); do


echo -n "*"
done

echo
done

Output:

Submi ed By:- Chaitanya Sharma(3rd Sem) Submi ed to:- Mr. Ganesh Singh

You might also like