0% found this document useful (0 votes)
8 views5 pages

Shell Programs

The document contains a series of shell scripts that perform various tasks, including calculating the sum of two numbers, finding the largest of three numbers, checking for palindromes, and performing arithmetic operations. Each script prompts the user for input and outputs the result based on the specified operation. The scripts cover a range of functionalities, from basic arithmetic to more complex tasks like generating Fibonacci numbers and displaying files in a directory.

Uploaded by

padmanabhpv2k04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Shell Programs

The document contains a series of shell scripts that perform various tasks, including calculating the sum of two numbers, finding the largest of three numbers, checking for palindromes, and performing arithmetic operations. Each script prompts the user for input and outputs the result based on the specified operation. The scripts cover a range of functionalities, from basic arithmetic to more complex tasks like generating Fibonacci numbers and displaying files in a directory.

Uploaded by

padmanabhpv2k04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

9.

Write a shell script to find sum of two numbers

#sum of two numbers


#echo "Enter first number: "
#read a
read -p 'Enter first number:' a
echo "Enter second number: "
read b
sum=$(($a + $b))
echo "sum ="$sum

10. Write a shell script to find biggest of two numbers

# Biggest of two numbers


read -p "Enter first number:" a
read -p "Enter second number:" b
if [ $a -gt $b ]
then
echo $a "is Bigger"
else
echo $b "is Bigger"
fi

11. Write a shell script to print sum of ‘n’ natural numbers

#sum of n natural numbers


read -p "Enter the limit:" n
i=1
sum=0
while [ $i -le $n ]
do
sum=$((sum+i))
i=$((i+1))
done
echo "Sum of natural numbers upto $n is : $sum"
12. Write a shell script to find factorial of a number

#sum of n natural numbers


read -p "Enter the limit:" n
i=1
fact=1
while [ $i -le $n ]
do
fact=$((fact*i))
i=$((i+1))
done
echo "Factorial of $n is : $fact"

13. Write a shell script to print first ‘n’ Fibonacci numbers


# first n fibonacci series
read -p "Enter the number of terms required:" n
echo "First $n Fibonacci terms are :"
a=0
b=1
s=2
echo $a
echo $b
while [ $s -lt $n ]
do
c=$((a+b))
a=$b
b=$c
echo $c
s=$((s+1))
done

14. Write a shell script to check whether a given number is palindrome or not
# check whether a given number is palindrome or not
read -p "Enter a number:" num
n=$num
rev=0
while [ $n -gt 0 ]
do
d=$(($n % 10))
rev=$((($rev *10) + d))
n=$(($n / 10))
done
if [ $rev -eq $num ]
then
echo "$num is Palindrome"
else
echo "$num is not palindrome"
fi

15. Write a shell script to find sum of digits of a given number


# find sum of digits of a given number
read -p "Enter a number:" num
n=$num
sum=0
while [ $n -gt 0 ]
do
d=$(($n % 10))
sum=$(($sum + d))
n=$(($n / 10))
done
echo "Sum of digits of the number $num is : $sum"

16. Write a shell script to find largest of three numbers

# Largest of three numbers


read -p "Enter first number:" a
read -p "Enter second number:" b
read -p "Enter third number:" c
if [ $a -gt $b ]
then
big=$a
else
big=$b
fi
if [ $c -gt $big ]
then
big=$c
fi
echo "$big is the Largest"
17. Write a shell script to perform all Arithmetic operations
# perform all arithmetic operations
echo "Program to perform all Arithmetic operations"
read -p "Enter first number:" a
read -p "Enter second number:" b
sum=$((a+b))
product=$((a*b))
difference=$((a-b))
division=$((a/b))
modulus=$((a%b))
echo "$a + $b = $sum"
echo "$a * $b = $product"
echo "$a - $b = $difference"
echo "$a / $b = $division"
echo "$a % $b = $modulus"

18. Write a shell script to print day of the week

# print day of the week


echo "This program prints the day of the week"
read -p "Enter the day(1-7):" 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

19. Write a shell script to compare two numbers


# compare two numbers
echo "This program compares two numbers"
read -p "Enter first number:" a
read -p "Enter second number:" b
if [ $a -gt $b ]
then
echo "$a is greater than $b"
elif [ $a -eq $b ]
then
echo "Both are equal"
else
echo "$a is lesser than $b"
fi

20. Write a shell script to display all files in a directory


# display all files in a directory
echo " This program will display all files in the current directory"
echo "Current directory : $PWD"
echo "Files are:"
for i in *
do
if [ -f $i ]
then
echo "$i"
fi
done

You might also like