0% found this document useful (0 votes)
3 views8 pages

Shell Scripting Program

The document contains a series of shell programming tasks and code snippets, including calculating the area of a triangle, converting Celsius to Fahrenheit, checking leap years, and performing arithmetic operations using switch cases. It also includes programs for checking divisibility, finding the largest of three numbers, generating Fibonacci numbers, and checking for palindromes, among others. Each task is accompanied by example inputs and outputs to illustrate the expected functionality.

Uploaded by

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

Shell Scripting Program

The document contains a series of shell programming tasks and code snippets, including calculating the area of a triangle, converting Celsius to Fahrenheit, checking leap years, and performing arithmetic operations using switch cases. It also includes programs for checking divisibility, finding the largest of three numbers, generating Fibonacci numbers, and checking for palindromes, among others. Each task is accompanied by example inputs and outputs to illustrate the expected functionality.

Uploaded by

Aditya Mohanty
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question: printf "Area of triangle = %d.

%02d\n" "$int"
"$dec"
Write a shell program to compute area of a
triangle whose sides a, b, c are given. Check Sample Input/Output:
a+b>c, b+c>a & c+a>b before area.
Input: 3 4 5
Code:
Question:
\
Write a shell program to convert Celsius to
echo "Enter three sides (a b c):" Fahrenheit.
read a b c Code:
if [ $((a + b)) -le $c ] || [ $((b + c)) -le $a ] || [ \
$((c + a)) -le $b ]; then
echo "Enter temperature in Celsius:"
echo "Not a valid triangle"
read c
exit 0
# Integer conversion: F = (C*9)/5 + 32
fi (integer division)
per=$((a + b + c)) f=$(( (c*9)/5 + 32 ))
s_scaled=$(( (per * 100) / 2 )) echo "Fahrenheit = $f"
sa=$((s_scaled - a*100)) Sample Input/Output:
sb=$((s_scaled - b*100)) Input: 37
sc=$((s_scaled - c*100)) Output: Fahrenheit = 98
t4=$(( s_scaled * sa )) Question:
t4=$(( t4 * sb )) Write a shell program to check whether the
given year is a leap year or not.
t4=$(( t4 * sc ))
Code:
isqrt() {
#!/bin/bash
n=$1
echo "Enter a year:"
if [ "$n" -le 0 ]; then echo 0; return; fi
read y
x=$n
if ! [[ $y =~ ^[0-9]+$ ]]; then echo "Enter a
y=$(( (x + 1) / 2 ))
valid year"; exit 1; fi
while [ "$y" -lt "$x" ]; do
if (( (y % 400) == 0 )) || { (( (y % 4) == 0 ))
x=$y && (( (y % 100) != 0 )); }; then

y=$(( (x + n / x) / 2 )) echo "Leap Year"

done else

echo "$x" echo "Not a Leap Year"

} fi

root=$(isqrt "$t4") Sample Input/Output:

int=$(( root / 100 )) Input: 2000 → Leap Year


1

dec=$(( root % 100 )) Input: 1900 → Not a Leap Year


Page

Input: 2024 → Leap Year


Question: b=$((sum - b - c))
Write a shell program to accept 2 arguments c=$((sum - a - b))
and perform +,-,*,/,% using switch case.
echo "After rotation swap (a<-b, b<-c, c<-a):
Code: a=$a b=$b c=$c"
echo "Enter two integers:" Sample Input/Output:
read a b Input: 1 2 3 → After rotation swap (a<-b, b<-
c, c<-a): a=2 b=3 c=1
echo "[Link]"
Question:
echo "[Link]"
Write a shell program to check whether the
echo "[Link]"
inputted number ends with 5 or not.
echo "[Link]"
Code:
echo "[Link]"
echo "Enter a number:"
echo -n "Enter your choice: "
read n
read ch
last=$(( n % 10 ))
case "$ch" in
if [ "$last" -eq 5 ]; then
1) echo "Sum = $((a + b))" ;;
echo "Ends with 5"
2) echo "Sub = $((a - b))" ;;
else
3) echo "Mul = $((a * b))" ;;
echo "Does not end with 5"
4) if [ "$b" -eq 0 ]; then echo "Division by
fi
zero not allowed"; else echo "Div = $((a /
b))"; fi ;; Sample Input/Output:
5) if [ "$b" -eq 0 ]; then echo "Modulo by Input: 125 → Ends with 5
zero not allowed"; else echo "Rem = $((a %
Question:
b))"; fi ;;
Take salary as input and calculate TA(0.5%),
*) echo "Invalid choice" ;;
DA(10%), HRA(10.5%) and gross salary. Also
esac find net salary after EPF(12.5%) and
ESI(2.5%).
Sample Input/Output:
Code:
Example: a=10 b=3, choice=4 → Div = 3
\
Example: a=10 b=0, choice=4 → Division by
zero not allowed echo "Enter basic salary:"
Question: read sal
Write a shell program to swap 3 numbers ta=$(( sal * 5 / 1000 ))
without using temporary variable.
da=$(( sal / 10 ))
Code:
hra=$(( sal * 105 / 1000 ))
echo "Enter three integers (a b c):"
gross=$(( sal + ta + da + hra ))
read a b c
epf=$(( gross * 125 / 1000 ))
# Use arithmetic sum trick
2

esi=$(( gross * 25 / 1000 ))


Page

sum=$((a + b + c))
net=$(( gross - epf - esi ))
a=$((sum - a - b))
echo "TA=$ta DA=$da HRA=$hra" esac
echo "GROSS=$gross" else
echo "EPF=$epf ESI=$esi" echo "Please enter a single alphabet
character"
echo "NET=$net"
fi
Sample Input/Output:
Sample Input/Output:
Input: 20000
Input: E → Vowel
Output (integers):
Input: z → Consonant
TA=100 DA=2000 HRA=2100
Input: 5 → Please enter a single alphabet
GROSS=24200
character
EPF=3025 ESI=605
Question:
NET=20570
Write a shell program to find largest of 3
Question: numbers.

Write a shell program to check whether the Code:


inputted number is divisible by 5 & 11.
#!/bin/bash
Code:
echo "Enter three numbers:"
echo "Enter a number:"
read a b c
read n
max=$a
if (( n % 5 == 0 )) && (( n % 11 == 0 )); then
if (( b > max )); then max=$b; fi
echo "Divisible by 5 and 11"
if (( c > max )); then max=$c; fi
else
echo "Largest = $max"
echo "Not divisible by both 5 and 11"
Sample Input/Output:
fi
Input: 7 2 9 → Largest = 9
Sample Input/Output:
Question:
Input: 55 → Divisible by 5 and 11
Write a shell program to find multiplication
Input: 50 → Not divisible by both 5 and 11 table of a given number.

Question: echo "Enter a number:"

Write a shell program to check whether the read n


inputted char is vowel or consonant.
for ((i=1;i<=10;i++)); do
Code:
echo "$n x $i = $((n*i))"
echo "Enter a single alphabet:"
done
read ch
Question:
ch=$(echo "$ch" | tr 'A-Z' 'a-z')
Write a shell program to find first 10
if [[ "$ch" =~ ^[a-z]$ ]]; then Fibonacci numbers.

case "$ch" in a=0; b=1


3

a|e|i|o|u) echo "Vowel" ;; count=10


Page

*) echo "Consonant" ;; for ((i=1;i<=count;i++)); do


echo -n "$a " Write a shell program to reverse a given
number.
fn=$((a+b)); a=$b; b=$fn
echo "Enter a number:"
done
read n
echo
rev=0
Question:
while [ $n -gt 0 ]; do
Write a shell program to find sum of digits of
a number. d=$((n%10))
echo "Enter a number:" rev=$((rev*10 + d))
read n n=$((n/10))
sum=0 done
while [ $n -gt 0 ]; do echo "Reverse = $rev"
d=$((n%10)) Question:
sum=$((sum+d)) Write a shell program to find factorial.
n=$((n/10)) echo "Enter a non-negative integer:"
done read n
echo "Sum of digits = $sum" if [ "$n" -lt 0 ]; then echo "Invalid"; exit 1; fi
Question: fact=1
Check whether given number is Armstrong or for ((i=2;i<=n;i++)); do fact=$((fact*i));
not. done
echo "Enter a number:" echo "Factorial = $fact"
read n Question:
temp=$n Check whether a given number is prime.
# count digits echo "Enter a number:"
digits=${#n} read n
sum=0 if [ "$n" -le 1 ]; then echo "Not Prime"; exit 0;
fi
while [ $temp -gt 0 ]; do
i=2
d=$((temp%10))
while [ $((i*i)) -le $n ]; do
pow=1
if [ $((n%i)) -eq 0 ]; then echo "Not Prime";
for ((i=1;i<=digits;i++)); do pow=$
exit 0; fi
((pow*d)); done
i=$((i+1))
sum=$((sum+pow))
done
temp=$((temp/10))
echo "Prime"
done
Question:
if [ "$sum" -eq "$n" ]; then echo
"Armstrong"; else echo "Not Armstrong"; fi Check whether a number is palindrome or
4

not.
Page

Question:
echo "Enter a number:"
read n if [ $d -ne $i ]; then sum=$((sum+d)); fi
orig=$n fi
rev=0 done
while [ $n -gt 0 ]; do d=$((n%10)); rev=$ if [ "$sum" -eq "$n" ]; then echo "Perfect
((rev*10+d)); n=$((n/10)); done Number"; else echo "Not Perfect"; fi
if [ "$rev" -eq "$orig" ]; then echo Question:
"Palindrome"; else echo "Not Palindrome"; fi
Write a shell program sorting of array
Question: elements in ascending order.
Check whether a number is strong number or echo "Enter number of elements:"
not (sum of factorials of digits equals the
read n
number).
echo "Enter $n integers:"
echo "Enter a number:"
for ((i=0;i<n;i++)); do read a[i]; done
read n
# simple bubble sort
orig=$n
for ((i=0;i<n-1;i++)); do
sum=0
for ((j=0;j<n-1-i;j++)); do
while [ $n -gt 0 ]; do
if [ ${a[j]} -gt ${a[j+1]} ]; then
d=$((n%10))
t=${a[j]}; a[j]=${a[j+1]}; a[j+1]=$t
fact=1
fi
for ((i=2;i<=d;i++)); do fact=$((fact*i));
done done
sum=$((sum+fact)) done
n=$((n/10)) echo "Sorted:" "${a[@]}"
done Question:
if [ "$sum" -eq "$orig" ]; then echo "Strong Write a shell program to find smallest
Number"; else echo "Not Strong Number"; fi number in an unsorted array.
Question: echo "Enter number of elements:"
Check whether a number is perfect number read n
or not (sum of proper divisors equals the
number). echo "Enter $n integers:"

echo "Enter a number:" read -a a

read n min=${a[0]}

if [ "$n" -le 1 ]; then echo "Not Perfect"; exit for ((i=1;i<n;i++)); do


0; fi if [ ${a[i]} -lt $min ]; then min=${a[i]}; fi
sum=1 done
for ((i=2;i*i<=n;i++)); do echo "Smallest = $min"
if [ $((n%i)) -eq 0 ]; then Question:
5

sum=$((sum+i)) Write a shell program to check a string is


Page

d=$((n/i)) palindrome or not.


echo "Enter a string:" Find out the 2nd highest number in the
unsorted array.
read s
echo "Enter number of elements:"
rev=""
read n
len=${#s}
echo "Enter $n integers:"
for ((i=len-1;i>=0;i--)); do rev="$rev$
{s:i:1}"; done read -a a
if [ "$s" = "$rev" ]; then echo "Palindrome"; if [ "$n" -lt 2 ]; then echo "Need at least 2
else echo "Not Palindrome"; fi numbers"; exit 1; fi
Question: max1=${a[0]}; max2=-2147483648
Write a shell program to count number of for ((i=1;i<n;i++)); do
occurrence of a single character in a string.
x=${a[i]}
echo "Enter a string:"
if [ $x -gt $max1 ]; then
read s
max2=$max1; max1=$x
echo "Enter a character to count:"
elif [ $x -ne $max1 ] && [ $x -gt $max2 ];
read ch then
count=0 max2=$x
len=${#s} fi
for ((i=0;i<len;i++)); do done
if [ "${s:i:1}" = "$ch" ]; then count=$ if [ "$max2" = "-2147483648" ]; then echo
((count+1)); fi "No distinct 2nd highest"; else echo "Second
highest = $max2"; fi
done
Question:
echo "Occurrences of '$ch' = $count"
Generate the number triangle patterns:
Question:
A)
Write a shell program to count number of
occurrence of vowel characters in a string. 1
echo "Enter a string:" 121
read s 12321
s=$(echo "$s" | tr 'A-Z' 'a-z') 1234321
count=0 B)
len=${#s} 1
for ((i=0;i<len;i++)); do 22
c="${s:i:1}" 333
case "$c" in a|e|i|o|u) count=$((count+1));; 4444
esac
echo "Enter n:"
done
read n
6

echo "Total vowels = $count"


echo "Pattern A:"
Page

Question:
for ((i=1;i<=n;i++)); do
# increasing then decreasing echo "Sum of digits = $(sum_of_digs "$n")"
for ((j=1;j<=i;j++)); do echo -n "$j"; done Question:
for ((j=i-1;j>=1;j--)); do echo -n "$j"; done Write a function find_rev() that takes a
number and finds its reverse.
echo
find_rev() {
done
local n=$1 rev=0
echo "Pattern B:"
while [ $n -gt 0 ]; do d=$((n%10)); rev=$
for ((i=1;i<=n;i++)); do
((rev*10 + d)); n=$((n/10)); done
for ((j=1;j<=i;j++)); do echo -n "$i"; done
echo "$rev"
echo
}
done
echo "Enter a number:"; read n
Question:
echo "Reverse = $(find_rev "$n")"
Write a function even_odd() that takes array
Question:
as an argument and counts number of even
& odd numbers. Write a function is_prime() that finds number
of prime numbers present in an array.
even_odd() {
is_prime_one() {
local arr=("$@")
local n=$1
local e=0 o=0
if [ "$n" -le 1 ]; then return 1; fi
for x in "${arr[@]}"; do
local i=2
if (( x%2==0 )); then e=$((e+1)); else
o=$((o+1)); fi while [ $((i*i)) -le $n ]; do
done if [ $((n%i)) -eq 0 ]; then return 1; fi
echo "Even=$e Odd=$o" i=$((i+1))
} done
echo "Enter n:"; read n return 0
echo "Enter $n integers:" }
read -a a is_prime() {
even_odd "${a[@]}" local arr=("$@") cnt=0
Question: for x in "${arr[@]}"; do
Write a function sum_of_digs() that takes a if is_prime_one "$x"; then cnt=$((cnt+1));
number and finds sum of its digits. fi
sum_of_digs() { done
local n=$1 sum=0 echo "$cnt"
while [ $n -gt 0 ]; do sum=$((sum + n }
%10)); n=$((n/10)); done
echo "Enter n:"; read n
echo "$sum"
echo "Enter $n integers:"
7

}
Page

read -a a
echo "Enter a number:"; read n
echo "Prime count = $(is_prime "${a[@]}")"

Question:
Write a recursive function to find sum of n
natural numbers.
sum_n() {
local n=$1
if [ "$n" -le 0 ]; then echo 0; return; fi
local prev=$(sum_n $((n-1)))
echo $((n + prev))
}
echo "Enter n:"; read n
echo "Sum = $(sum_n "$n")"
Question:
Write a function to print Armstrong numbers
between 1 to 2000.
is_armstrong() {
local n=$1 temp=$1 sum=0 d=${#n}
while [ $temp -gt 0 ]; do
local dig=$((temp%10))
local p=1
for ((i=1;i<=d;i++)); do p=$((p*dig));
done
sum=$((sum+p))
temp=$((temp/10))
done
[ "$sum" -eq "$n" ]
}
for ((x=1;x<=2000;x++)); do
if is_armstrong "$x"; then echo -n "$x "; fi
8

done
Page

echo

You might also like