UNIX SHELL PROGRAMMING
UNIX SHELL PROGRAMMING
1.1. Unix Unix SheShell ll proprogramgrams us using sing ExprExpressioessionsns a.
a. Write a Shell program to find theWrite a Shell program to find the area and circumference of a circle area and circumference of a circle.. 2.
2. Unix Unix SheShell pll progrrograms ams usinusing Cg Condionditiontionalal Statements
Statements a.
a. WriWrite te a Sha Shell ell proprogram gram to fto find ind thethe largest among three numbers. largest among three numbers. b.
b. WriWrite a Ste a Shelhell prl progrogram tam to cheo check tck thehe given number is even or odd.
given number is even or odd. c.
c. WrWritite a She a Shelell prl progrogram tam to cho chececk k whether given year is leap or no whether given year is leap or not.t. d.
d. WriWrite a te a SheShell ll proprogram gram to dto dispisplaylay student grades.
student grades. e.
e. WrWritite a She a Shelell prl progrogram tam to fio find tnd thehe roots of a quadratic equation. roots of a quadratic equation. f.
f. WriWrite te a Sa Shelhell pl prorogrgram am to to exeexecutcutee various UNIX commands using case various UNIX commands using case statements.
statements. 3.
3. Unix Unix SheShell prll prograograms usms using ling loopiooping Stng Statematements.ents. a.
a. WriWrite te a Sha Shell ell proprogram gram to fto find ind thethe factorial of a number using for loop. factorial of a number using for loop. b.
b. WriWrite a Ste a Shelhell prl progrogram tam to geo genernerateate Fibonacci series.
Fibonacci series. c.
c. WriWrite te a Sha Shell ell proprogram gram to gto geneeneratratee prime numbers between 1 and 50. prime numbers between 1 and 50. d.
d. WriWrite a Ste a Shelhell prl progrogram tam to cheo check tck thehe given integer is prime or not
given integer is prime or not e.
e. WriWrite te a Sha Shell ell proprogram gram to pto prinrint tt thehe reverse of a number.
reverse of a number. f.
f. WrWritite a Se a Shehell ll prprogrogram tam to cho chececk thk thee given string is palindrome or not. given string is palindrome or not. g.
g. WriWrite a te a SheShell ll proprogram gram to cto checheck thk thee given integer is Armstrong number given integer is Armstrong number or not.
or not. h.
h. WriWrite a te a SheShell ll proprogram gram to fto find ind thethe sum of square of individual digits o sum of square of individual digits of af a number.
number. i.
i. WrWritite a Se a Shehell ll prprogrogram am to fto finind td thehe factorial of a number using for loop. factorial of a number using for loop. j.
j. Write a Shell program tWrite a Shell program to find sum of o find sum of individual digits of a number.
individual digits of a number. k.
k. WriWrite a Ste a Shelhell prl progrogram tam to fio find tnd the suhe summ of digits of a number until a single digit of digits of a number until a single digit is obtained.
is obtained. l.
l. WrWritite a She a Shelell prl progograram to m to fifind tnd thehe largest digit of a number.
largest digit of a number. m.
m. WritWrite a She a Shell prell prograogram to m to find find thethe smallest digit of a number.
smallest digit of a number. n.
n. WriWrite a Ste a Shelhell prl progrogram tam to fio find tnd thehe second largest digit from a
second largest digit from a number.number.
o.
o. WrWrite ite a Shea Shell prll progrogram tam to fio find tnd the suhe summ of all numbers between 50 and 100, of all numbers between 50 and 100, which are divisible by 3 and not which are divisible by 3 and not divisible by 5.
divisible by 5. p.
p. Write a SheWrite a Shell program ll program to count theto count the number of vowels in a line of text. number of vowels in a line of text. 4.
4. UnUnix shix shell ell proprogrgrams uams usinsing arrg arraysays.. a.
a. WrWrite ite a Sha Shell ell prprogrogram tam to soo sort ‘rt ‘n’n’ different numbers.
different numbers. b.
b. WriWrite te a Sha Shell ell proprogram gram to fto find ind thethe largest and smallest among ‘n’ largest and smallest among ‘n’ different numbers
different numbers c.
c. WrWrite ite a Sha Shell ell prprogrogram tam to fio find tnd the suhe summ of ‘n’ different numbers.
of ‘n’ different numbers. d.
d. WriWrite te a Sha Shell ell proprogram gram to fto find ind thethe sum of odd and even from a set of sum of odd and even from a set of numbers.
numbers. 5.
5. UnUnix shix shell pell prorogrgrams usams usining Fung Functctionionss a.
a. WriWrite te a Sha Shell ell proprogram gram to fto find ind thethe largest number between two numbers largest number between two numbers using function.
using function. b.
b. Write a SheWrite a Shell program ll program to find the suto find the summ of two numbers using function
of two numbers using function program
programming.ming. c.
c. WrWritite a Se a Shehell ll prprogrogram tam to fio findnd
factorial of a number using recursive factorial of a number using recursive function.
#Ex.No - 1a : To find the area and #Ex.No - 1a : To find the area and circumference of a circle.
circumference of a circle. #circle.sh
#circle.sh
echo -n "Enter Radius of the
echo -n "Enter Radius of the Circle : "Circle : " read r read r area=$(echo "3.14*$r*$r"|bc -1 ) area=$(echo "3.14*$r*$r"|bc -1 ) cf=$(echo "2*3.14*$r"|bc -l ) cf=$(echo "2*3.14*$r"|bc -l ) echo echo "==========================="============================"=" echo "AREA OF CIRCLE : $area"
echo "AREA OF CIRCLE : $area" echo
echo "CIRCUMFERENCE "CIRCUMFERENCE : : $cf"$cf" echo
echo "==========================="============================"="
OUTPUT: OUTPUT:
#Ex.2a Greatest of three numbers #Ex.2a Greatest of three numbers #largest3.sh
#largest3.sh
echo "Enter Three Numbers A,B,C" echo "Enter Three Numbers A,B,C" read a b c read a b c if [ $a -gt $b -a $a -gt $c ] if [ $a -gt $b -a $a -gt $c ] then then
echo "$a is greater" echo "$a is greater" elif [ $b -gt $c ] elif [ $b -gt $c ] then then echo "$b is greater" echo "$b is greater" else else echo "$c is greater" echo "$c is greater" fi fi OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh large3.sh [CPL_2_lab@localhost SH]$ sh large3.sh Enter Three Numbers A,B,C
Enter Three Numbers A,B,C 78 79 90 78 79 90 90 is greater 90 is greater [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.2b Check odd or even number #Ex.No.2b Check odd or even number #check_even.sh
#check_even.sh
echo -n "Enter the number : " echo -n "Enter the number : " read num read num if [ $(($num%2)) -eq 0 ] if [ $(($num%2)) -eq 0 ] then then
echo "The number $num is even" echo "The number $num is even" else
else
echo "The number $num is odd" echo "The number $num is odd" fi fi OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh [CPL_2_lab@localhost SH]$ sh check_even.sh check_even.sh
Enter the number : 235 Enter the number : 235 The number 235 is odd The number 235 is odd
[CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.2c: Check Leap year or not #Ex.No.2c: Check Leap year or not #check_leap.sh
#check_leap.sh
echo -n "Enter the year : " echo -n "Enter the year : "
read year read year n=$(($year%4)) n=$(($year%4)) if [ if [ $n -eq $n -eq 0 ]0 ] then then
echo "$year is Leap year" echo "$year is Leap year" else
else
echo "$year is not leap year" echo "$year is not leap year" fi fi OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh [CPL_2_lab@localhost SH]$ sh check_leap.sh check_leap.sh
Enter the year : 1996 Enter the year : 1996 1996 is Leap year 1996 is Leap year
[CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.2d Display Student grade #Ex.No.2d Display Student grade #grade.sh
#grade.sh
echo "Enter Marks in five subjects" echo "Enter Marks in five subjects" read m1 m2 m3 m4 m5 read m1 m2 m3 m4 m5 tot=$(($m1+$m2+$m3+$m4+$m5)) tot=$(($m1+$m2+$m3+$m4+$m5)) average=$(echo "$tot/5"|bc -l) average=$(echo "$tot/5"|bc -l) avg=$average avg=$average echo "$avg" echo "$avg" if [ $m1 -lt 50 -o $m2 -lt 50 if [ $m1 -lt 50 -o $m2 -lt 50 -o $m3 -lt 50 -o $m4-o $m3 -lt 50 -o $m4 -lt 50 -o $m5 -lt 50 ] -lt 50 -o $m5 -lt 50 ] then then grade="FAIL" grade="FAIL" [CPL_2_lab@localhost SH]$ sh circle.sh [CPL_2_lab@localhost SH]$ sh circle.sh Enter Radius of the Circle : 7
Enter Radius of the Circle : 7
============================ ============================ AREA OF CIRCLE : 153.86 AREA OF CIRCLE : 153.86 CIRCUMFERENCE CIRCUMFERENCE : : 43.9643.96 ============================ ============================ [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
elif [ $(echo "$avg >= 75"|bc) -eq 1 ] elif [ $(echo "$avg >= 75"|bc) -eq 1 ] then
then
grade="FIRST CLASS WITH DISTINCTION" grade="FIRST CLASS WITH DISTINCTION" elif [ $(echo "$avg >= 60"|bc) -eq 1 ]
elif [ $(echo "$avg >= 60"|bc) -eq 1 ] then
then
grade="FIRST CLASS" grade="FIRST CLASS"
elif [ $(echo "$avg >= 50"|bc) -eq 1 ] elif [ $(echo "$avg >= 50"|bc) -eq 1 ] then then grade="SEOCND CLASS" grade="SEOCND CLASS" else else grade="THIRD CLASS" grade="THIRD CLASS" fi fi echo "=========================" echo "=========================" echo
echo "TOTAL "TOTAL MARKS MARKS : : $tot"$tot" echo "AVERAGE MARKS : $avg" echo "AVERAGE MARKS : $avg" echo
echo "GRADE "GRADE : : $grade"$grade"
echo "=========================" echo "=========================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh grade.sh [CPL_2_lab@localhost SH]$ sh grade.sh Enter Marks in five subjects
Enter Marks in five subjects 95 90 75 68 82 95 90 75 68 82 82.00 82.00 ================================== ================================== TOTAL
TOTAL MARKS MARKS : : 410410 AVERAGE MARKS : 82.00 AVERAGE MARKS : 82.00 GRADE
GRADE : : FIRST FIRST CLASS CLASS WITHWITH DISTINCTION DISTINCTION ================================== ================================== [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.2e.Roots of Quadratic Equation #Ex.No.2e.Roots of Quadratic Equation #quad_roots.sh
#quad_roots.sh
echo "Enter the Co-ordinates A, B, echo "Enter the Co-ordinates A, B, C : "C : " read a b c read a b c d=`expr $b \* $b - 4 \* $a \* $c` d=`expr $b \* $b - 4 \* $a \* $c` echo echo "================================== "================================== "" if [ $d -lt 0 ] if [ $d -lt 0 ] then then
echo "Roots are Imaginary..!" echo "Roots are Imaginary..!" elif [ $d -eq 0 ]
elif [ $d -eq 0 ] then
then
echo "Roots are equal..!" echo "Roots are equal..!" r1=`expr
r1=`expr -1 -1 \* \* $b $b / / 2 2 \* \* $a $a `` r2=$r1
r2=$r1
echo "The Roots are R1 = $r1 R2 = $r2" echo "The Roots are R1 = $r1 R2 = $r2"
else else
echo "Roots are Different..!" echo "Roots are Different..!" r=$(echo "scale=2;sqrt($d)" | bc) r=$(echo "scale=2;sqrt($d)" | bc) x=$(echo "-1*$b"|bc) x=$(echo "-1*$b"|bc) r1=$(echo "scale=2;($x+$r)/(2*$a)"|bc) r1=$(echo "scale=2;($x+$r)/(2*$a)"|bc) r2=$(echo "scale=2;($x-$r)/(2*$a)"|bc) r2=$(echo "scale=2;($x-$r)/(2*$a)"|bc) echo "The Roots are R1 = $r1 R2 = $r2" echo "The Roots are R1 = $r1 R2 = $r2" fi fi echo echo "================================== "================================== "" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh quad_roots.sh [CPL_2_lab@localhost SH]$ sh quad_roots.sh Enter the Co-ordinates A, B, C :
Enter the Co-ordinates A, B, C : 1 -3 -4
1 -3 -4
================================== ================================== Roots are Different..!
Roots are Different..!
The Roots are R1 = 4.00 R2 = -1.00 The Roots are R1 = 4.00 R2 = -1.00
================================== ================================== [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$
#Ex.No:3a.Find Factorial using for loop #Ex.No:3a.Find Factorial using for loop #fact.sh
#fact.sh
echo -n "Enter the Number : " echo -n "Enter the Number : " read n read n f=1 f=1 for((i=1;i<=$n;i++)) for((i=1;i<=$n;i++)) do do f=$(($f*$i)) f=$(($f*$i)) done done echo "===========================" echo "===========================" echo "The factorial $n! = $f"
echo "The factorial $n! = $f"
echo "===========================" echo "===========================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh fact.sh [CPL_2_lab@localhost SH]$ sh fact.sh Enter the Number : 7
Enter the Number : 7
=========================== =========================== The factorial 7! = 5040 The factorial 7! = 5040 =========================== =========================== [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3b. Generate Fibonacci series #Ex.No.3b. Generate Fibonacci series #fibo.sh
#fibo.sh
echo -n "Enter the Limit : " echo -n "Enter the Limit : " read n
read n a=0 a=0
b=1 b=1
echo "FIBONACCI SERIES" echo "FIBONACCI SERIES" echo echo "================================== "================================== "" while [ $a -le $n ] while [ $a -le $n ] do do echo -n " $a" echo -n " $a" c=$(($a+$b)) c=$(($a+$b)) a=$b a=$b b=$c b=$c #c=$(($a+$b)) #c=$(($a+$b)) done done echo "" echo "" echo echo "================================== "================================== "" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh fibo.sh [CPL_2_lab@localhost SH]$ sh fibo.sh Enter the Limit : 8
Enter the Limit : 8 FIBONACCI SERIES FIBONACCI SERIES ================================== ================================== 0 1 1 2 3 5 8 0 1 1 2 3 5 8 ================================== ================================== [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3c Check given number is Prime or #Ex.No.3c Check given number is Prime or not
not
#check_prime.sh #check_prime.sh
echo -n "Enter the number : " echo -n "Enter the number : " read num read num for((i=2;i<$num;i++)) for((i=2;i<$num;i++)) do do if [
if [ $(($num%$i)) $(($num%$i)) -eq 0 -eq 0 ]] then then break; break; fi fi done done
if [ $i -eq $num -o $num -eq 1 ] if [ $i -eq $num -o $num -eq 1 ] then
then
echo "The number $num is prime" echo "The number $num is prime" else
else
echo "The number $num is not prime" echo "The number $num is not prime" fi fi OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh [CPL_2_lab@localhost SH]$ sh check_prime.sh check_prime.sh
Enter the number : 71 Enter the number : 71 The number 71 is prime The number 71 is prime [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3d Generate Prime numbers between #Ex.No.3d Generate Prime numbers between 1-50
1-50
#check_prime.sh #check_prime.sh
echo "PRIME NUMBERS BETWEEN 1 and 50 echo "PRIME NUMBERS BETWEEN 1 and 50 ARE : " ARE : " echo echo "================================== "================================== =" =" for((n=1;n<=50;n++)) for((n=1;n<=50;n++)) do do for((i=2;i<$n;i++)) for((i=2;i<$n;i++)) do do if [
if [ $(($n%$i)) -$(($n%$i)) -eq 0 eq 0 ]] then then break; break; fi fi done done if [ $i -eq $n -o $n -eq 1 ] if [ $i -eq $n -o $n -eq 1 ] then then echo -n " $n " echo -n " $n " fi fi done done echo "" echo "" echo echo "================================== "================================== =" =" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh gen_primes.sh [CPL_2_lab@localhost SH]$ sh gen_primes.sh PRIME NUMBERS BETWEEN 1 and 50 PRIME NUMBERS BETWEEN 1 and 50 ARE : ARE : ================================== ================================== 1 2 3 5 7 11 13 17 19 23 29 31 37 41 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 43 47 ================================== ================================== [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3e.Reverse the Given Number #Ex.No.3e.Reverse the Given Number #rev_num.sh
#rev_num.sh
echo -n "Enter the number : " echo -n "Enter the number : " read num
read num n=$num n=$num
rev=0 rev=0 while [ $n -gt 0 ] while [ $n -gt 0 ] do do a=`expr $n % 10` a=`expr $n % 10`
rev=`expr $(($rev*10)) + $a` rev=`expr $(($rev*10)) + $a` n=`expr $n / 10` n=`expr $n / 10` done done echo echo "=================================" "=================================" echo "The Reverse of $num is $rev"
echo "The Reverse of $num is $rev" echo echo "=================================" "=================================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh rev_num.sh [CPL_2_lab@localhost SH]$ sh rev_num.sh Enter the number : 980022
Enter the number : 980022
================================ ================================ The Reverse of 980022 is 220089 The Reverse of 980022 is 220089 ================================ ================================ [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3f.Check whether given number is #Ex.No.3f.Check whether given number is Palindrome or not
Palindrome or not #palin_num.sh #palin_num.sh
echo -n "Enter the number : " echo -n "Enter the number : " read num read num n=$num n=$num rev=0 rev=0 while [ $n -gt 0 ] while [ $n -gt 0 ] do do a=`expr $n % 10` a=`expr $n % 10`
rev=`expr $rev \* 10 + $a` rev=`expr $rev \* 10 + $a` n=`expr $n / 10`
n=`expr $n / 10` done
done
if [ $num -eq $rev ] if [ $num -eq $rev ] then
then
echo "The number $num is Palindrome" echo "The number $num is Palindrome" else
else
echo "The number $num is not Palindrome" echo "The number $num is not Palindrome" fi fi OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh palin_num.sh [CPL_2_lab@localhost SH]$ sh palin_num.sh Enter the number : 22155122
Enter the number : 22155122
The number 22155122 is Palindrome The number 22155122 is Palindrome [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$
#Ex.No.3g Check Armstrong number or not #Ex.No.3g Check Armstrong number or not #check_arm.sh
#check_arm.sh
echo -n "Enter the number : " echo -n "Enter the number : " read num read num n=$num n=$num arm=0 arm=0 while [ $n -gt 0 ] while [ $n -gt 0 ] do do a=`expr $n % 10` a=`expr $n % 10`
arm=`expr $arm + $a \* $a \* $a` arm=`expr $arm + $a \* $a \* $a` n=`expr $n / 10`
n=`expr $n / 10` done
done
if [ $num -eq $arm ] if [ $num -eq $arm ] then
then
echo "The number $num is Armstrong" echo "The number $num is Armstrong" else
else
echo "The number $num is not Armstrong" echo "The number $num is not Armstrong" fi
fi
OUTPUT: OUTPUT:
[CPL_2_lab@localho
[CPL_2_lab@localhost SH]$ st SH]$ sh check_arm.shsh check_arm.sh Enter the number : 153
Enter the number : 153
The number 153 is Armstrong The number 153 is Armstrong [CPL_2_lab@localho
[CPL_2_lab@localhost st SH]$SH]$
#Ex.No.3h Find sum of square of individual #Ex.No.3h Find sum of square of individual digits
digits
# sum_digit_square.sh # sum_digit_square.sh
echo -n "Enter the number : " echo -n "Enter the number : " read num read num n=$num n=$num sum=0 sum=0 while [ $n -gt 0 ] while [ $n -gt 0 ] do do a=`expr $n % 10` a=`expr $n % 10` sum=`expr $sum + $a \* $a ` sum=`expr $sum + $a \* $a ` n=`expr $n / 10` n=`expr $n / 10` done done
echo "Sum of Squares of Digits is : $sum" echo "Sum of Squares of Digits is : $sum"
OUTPUT: OUTPUT:
[CPL_2_lab@localhost SH]$ sh sum_digit_square.sh [CPL_2_lab@localhost SH]$ sh sum_digit_square.sh Enter the number : 154
Enter the number : 154
Sum of Squares of Digits is : 42 Sum of Squares of Digits is : 42 [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3i Sum of Individual Digits #Ex.No.3i Sum of Individual Digits #sum_digits.sh
echo "ENTER THE NUMBER :" echo "ENTER THE NUMBER :" read NUM read NUM sum=0 sum=0 while [ $NUM -gt 0 ] while [ $NUM -gt 0 ] do do a=`expr $NUM % 10 ` a=`expr $NUM % 10 ` sum=`expr $sum + $a ` sum=`expr $sum + $a ` NUM=`expr $NUM / 10 ` NUM=`expr $NUM / 10 ` done done echo echo "---"---"---" echo " SUM
echo " SUM OF DIGITS OF DIGITS : $sum : $sum ""
echo "===========================" echo "===========================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh sum_digits.sh [CPL_2_lab@localhost SH]$ sh sum_digits.sh ENTER THE NUMBER :
ENTER THE NUMBER : 5923
5923
---SUM
SUM OF DIGIOF DIGITS TS : 19: 19
=========================== =========================== [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$
#Ex.No.3j Sum of Individual Digits to Single #Ex.No.3j Sum of Individual Digits to Single digit
digit
#single_digit_sum.sh #single_digit_sum.sh
echo "ENTER THE NUMBER :" echo "ENTER THE NUMBER :" read N read N sum=$N sum=$N while [ $sum -gt 9 ] while [ $sum -gt 9 ] do do NUM=$sum NUM=$sum sum=0 sum=0 while [ $NUM -gt 0 ] while [ $NUM -gt 0 ] do do a=`expr $NUM % 10 ` a=`expr $NUM % 10 ` sum=`expr $sum + $a ` sum=`expr $sum + $a ` NUM=`expr $NUM / 10 ` NUM=`expr $NUM / 10 ` done done done done echo echo "---"---"---" echo " SUM
echo " SUM OF DIGITS OF DIGITS : $sum : $sum ""
echo "===========================" echo "===========================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh single_digit_sum.sh [CPL_2_lab@localhost SH]$ sh single_digit_sum.sh ENTER THE NUMBER :
ENTER THE NUMBER : 97364
97364
---SUM O
SUM OF DIGITF DIGITS S : : 22
=========================== =========================== [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$
#Ex.No.3k Find largest digit from a number #Ex.No.3k Find largest digit from a number #large_digit.sh
#large_digit.sh
echo "ENTER THE NUMBER :" echo "ENTER THE NUMBER :" read NUM read NUM max=0 max=0 while [ $NUM -gt 0 ] while [ $NUM -gt 0 ] do do a=`expr $NUM % 10 ` a=`expr $NUM % 10 ` if [ $a -gt $max ] if [ $a -gt $max ] then then max=$a max=$a fi fi NUM=`expr $NUM / 10 ` NUM=`expr $NUM / 10 ` done done echo echo "---"---"---" echo " LARGE
echo " LARGEST DIGIT ST DIGIT IS IS : $max : $max ""
echo "===========================" echo "===========================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh large_digit.sh [CPL_2_lab@localhost SH]$ sh large_digit.sh ENTER THE NUMBER :
ENTER THE NUMBER : 239284
239284
---LARGEST DI
LARGEST DIGIT IGIT IS S : 9: 9
=========================== =========================== [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$
#Ex.No.3l Find smallest digit from a number #Ex.No.3l Find smallest digit from a number #small_digit.sh
#small_digit.sh
echo "ENTER THE NUMBER :" echo "ENTER THE NUMBER :" read NUM read NUM min=10 min=10 while [ $NUM -gt 0 ] while [ $NUM -gt 0 ] do do a=`expr $NUM % 10 ` a=`expr $NUM % 10 ` if [ $a -lt $min ] if [ $a -lt $min ] then then min=$a min=$a fi fi NUM=`expr $NUM / 10 ` NUM=`expr $NUM / 10 `
done done echo
echo "---"---"--" echo " SMALLEST DI
echo " SMALLEST DIGIT IS GIT IS : $min ": $min " echo "===========================" echo "===========================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh small_digit.sh [CPL_2_lab@localhost SH]$ sh small_digit.sh ENTER THE NUMBER :
ENTER THE NUMBER : 986435
986435
---SMALLEST
SMALLEST DIGIT DIGIT IS IS : 3: 3
=========================== =========================== [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$
#Ex.No.3m Find second largest digit from a #Ex.No.3m Find second largest digit from a number
number
#max2_digit.sh #max2_digit.sh
echo "ENTER THE NUMBER :" echo "ENTER THE NUMBER :" read NUM read NUM max=0 max=0 max2=0 max2=0 while [ $NUM -gt 0 ] while [ $NUM -gt 0 ] do do a=`expr $NUM % 10 ` a=`expr $NUM % 10 ` if [ $a -gt $max ] if [ $a -gt $max ] then then max2=$max max2=$max max=$a max=$a elif [ $a -gt $max2 ] elif [ $a -gt $max2 ] then then max2=$a max2=$a fi fi NUM=`expr $NUM / 10 ` NUM=`expr $NUM / 10 ` done done echo echo "---" "---" echo
echo " " LARGEST LARGEST DIGIT DIGIT IS IS : : $max $max ""
echo " SECOND LARGEST DIGIT IS : $max2" echo " SECOND LARGEST DIGIT IS : $max2" echo echo "=================================" "=================================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ sh max2_digit.sh [CPL_2_lab@localhost SH]$ sh max2_digit.sh ENTER THE NUMBER :
ENTER THE NUMBER : 37295
37295
---LARGEST
LARGEST DIGIT DIGIT IS IS : : 99 SECOND
SECOND LARGEST LARGEST DIGIT DIGIT IS IS : : 77
================================= ================================= [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$
#Ex.No.3n. Sum of number between 50-100 #Ex.No.3n. Sum of number between 50-100 divisible by 3 not by 5 divisible by 3 not by 5 #sum_50_100.sh #sum_50_100.sh sum=0 sum=0
echo "NUMBERS DIVISIBLE BY 3 and NOT echo "NUMBERS DIVISIBLE BY 3 and NOT BY 5 ARE" BY 5 ARE" echo echo "================================== "================================== =" =" for((i=50;i<=100;i++)) for((i=50;i<=100;i++)) do do
if [ $(($i%3)) -eq 0 -a $(($i%5)) -ne 0 ] if [ $(($i%3)) -eq 0 -a $(($i%5)) -ne 0 ] then then echo -n " $i " echo -n " $i " sum=$(($sum+$i)) sum=$(($sum+$i)) fi fi done done echo "" echo "" echo echo "================================== "================================== =" ="
echo "SUM OF NUMBERS IS : $sum" echo "SUM OF NUMBERS IS : $sum" echo echo "================================== "================================== =" =" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh sum_50_100.shsh sum_50_100.sh NUMBERS DIVISIBLE BY 3 and NOT BY 5 NUMBERS DIVISIBLE BY 3 and NOT BY 5
ARE ARE ==================================== ==================================== 51 54 57 63 66 69 72 78 81 84 87 93 96 99 51 54 57 63 66 69 72 78 81 84 87 93 96 99 ==================================== ==================================== SUM OF NUMBERS IS : 1050 SUM OF NUMBERS IS : 1050 ==================================== ==================================== [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3o Check given string is Palindrome #Ex.No.3o Check given string is Palindrome or not
#palin_str.sh #palin_str.sh
echo -n "Enter String : " echo -n "Enter String : " read str read str len=${#str} len=${#str} i=1 i=1 j=$len j=$len while [ $i -lt $j ] while [ $i -lt $j ] do do c1=`expr substr $str $i 1` c1=`expr substr $str $i 1` c2=`expr substr $str $j 1` c2=`expr substr $str $j 1` if [ $c1 != $c2 ] if [ $c1 != $c2 ] then then break break fi fi i=`expr $i + 1` i=`expr $i + 1` j=`expr $j - 1` j=`expr $j - 1` done done echo echo "================================== "================================== =" =" if [ $i -lt $j ] if [ $i -lt $j ] then then
echo "The string $str is NOT Palindrome" echo "The string $str is NOT Palindrome" else
else
echo "The string $str is Palindrome" echo "The string $str is Palindrome" fi fi echo echo "================================== "================================== =" =" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh palin_str.shsh palin_str.sh Enter String : ATOYOTA
Enter String : ATOYOTA
==================================== ==================================== The string ATOYOTA is Palindrome
The string ATOYOTA is Palindrome
==================================== ====================================
[CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.3p Count No of Vowels in a text #Ex.No.3p Count No of Vowels in a text #vowels.sh
#vowels.sh
echo -n "Enter String : " echo -n "Enter String : " read str read str len=${#str} len=${#str} i=1 i=1 count=0 count=0 vow=aeiouiAEIOU vow=aeiouiAEIOU while [ $i -le $len ] while [ $i -le $len ] do do ch=`expr substr $str $i 1` ch=`expr substr $str $i 1` for((j=1;$j<=10;j++)) for((j=1;$j<=10;j++)) do do
c2=`expr substr $vow $j 1` c2=`expr substr $vow $j 1` if [ if [ $ch == $ch == $c2 $c2 ]] then then count=$(($count+1)) count=$(($count+1)) break break fi fi done done i=`expr $i + 1` i=`expr $i + 1` done done
echo "No of Vowels is : $count" echo "No of Vowels is : $count"
OUTPUT: OUTPUT:
[CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh vowels.shsh vowels.sh Enter String : entertainment
Enter String : entertainment No of Vowels is : 5
No of Vowels is : 5
[CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.4a. Sort N numbers #Ex.No.4a. Sort N numbers #sort_n.sh
#sort_n.sh
echo "ENTER NO OF ELEMENTS" echo "ENTER NO OF ELEMENTS" read N
read N
echo "ENTER NUMBERS TO BE SORTED" echo "ENTER NUMBERS TO BE SORTED" for i in $(seq 1 1 $N) for i in $(seq 1 1 $N) do do read a[i] read a[i] done done
for ((i=1; i<=$N; i++ )) for ((i=1; i<=$N; i++ )) do do for((j=$i; j<=$N; j++ )) for((j=$i; j<=$N; j++ )) do do if [ ${a[i]} -gt ${a[j]} ] if [ ${a[i]} -gt ${a[j]} ] then then t=${a[i]} t=${a[i]} a[i]=${a[j]} a[i]=${a[j]} a[j]=$t a[j]=$t fi fi done done done done echo "======================" echo "======================" echo "SORTED LIST : ${a[*]} " echo "SORTED LIST : ${a[*]} " echo "======================" echo "======================"
OUTPUT: OUTPUT:
[CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh sort_n.shsh sort_n.sh ENTER NO OF ELEMENTS
ENTER NO OF ELEMENTS 5
5
ENTER NUMBERS TO BE SORTED ENTER NUMBERS TO BE SORTED 98 98 -3 -3 45 45 78 78 23 23 ========================= ========================= SORTED LIST : -3 23 45 78 98 SORTED LIST : -3 23 45 78 98 ========================= ========================= [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.4b. Find largest among N numbers #Ex.No.4b. Find largest among N numbers #max_min_n.sh
#max_min_n.sh
echo "ENTER NO OF ELEMENTS" echo "ENTER NO OF ELEMENTS" read N
read N
echo "ENTER THE NUMBERS : " echo "ENTER THE NUMBERS : " for i in $(seq 1 1 $N) for i in $(seq 1 1 $N) do do read a[i] read a[i] done done max=${a[1]} max=${a[1]} min=$max min=$max
for ((i=2; i<=$N; i++ )) for ((i=2; i<=$N; i++ )) do do if [ ${a[$i]} -gt $max ] if [ ${a[$i]} -gt $max ] then then max=${a[$i]} max=${a[$i]} fi fi if [ ${a[$i]} -lt $min ] if [ ${a[$i]} -lt $min ] then then min=${a[$i]} min=${a[$i]} fi fi done done echo "========================" echo "========================" echo " LAR
echo " LARGEST NUMBER GEST NUMBER : $max : $max "" echo " SMALLEST NUMBER : $min" echo " SMALLEST NUMBER : $min" echo "========================" echo "========================"
OUTPUT: OUTPUT:
[CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh max_min_n.shsh max_min_n.sh ENTER NO OF ELEMENTS
ENTER NO OF ELEMENTS 5
5
ENTER THE NUMBERS : ENTER THE NUMBERS : 34 34 67 67 -34 -34 56 56 37 37 ======================== ======================== LARGEST
LARGEST NUMBER NUMBER : : 6767 SMALLEST NUMBER : -34 SMALLEST NUMBER : -34 ======================== ======================== [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.4c. Find Sum of N numbers #Ex.No.4c. Find Sum of N numbers #sum_n.sh
#sum_n.sh
echo "ENTER NO OF ELEMENTS" echo "ENTER NO OF ELEMENTS" read N
read N
echo "ENTER THE NUMBERS : " echo "ENTER THE NUMBERS : " for i in $(seq 1 1 $N) for i in $(seq 1 1 $N) do do read a[i] read a[i] done done sum=0 sum=0
for ((i=1; i<=$N; i++ )) for ((i=1; i<=$N; i++ )) do do sum=$(($sum+${a[$i]})) sum=$(($sum+${a[$i]})) done done echo "========================" echo "========================" echo " SUM
echo " SUM IS IS : $sum : $sum ""
echo "========================" echo "========================"
OUTPUT: OUTPUT:
[CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh sum_n.shsh sum_n.sh ENTER NO OF ELEMENTS
ENTER NO OF ELEMENTS 5
5
ENTER THE NUMBERS : ENTER THE NUMBERS : 98 98 45 45 34 34 56 56 67 67 ======================== ======================== SUM SUM IS IS : : 300300 ======================== ======================== [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.4d. Find Sum of ODD and EVEN #Ex.No.4d. Find Sum of ODD and EVEN numbers
numbers
#sum_odd_even.sh #sum_odd_even.sh
echo "ENTER NO OF ELEMENTS" echo "ENTER NO OF ELEMENTS" read N
read N
echo "ENTER THE NUMBERS : " echo "ENTER THE NUMBERS : " for i in $(seq 1 1 $N) for i in $(seq 1 1 $N) do do read a[i] read a[i] done done sum_odd=0 sum_odd=0 sum_even=0 sum_even=0
for ((i=1; i<=$N; i++ )) for ((i=1; i<=$N; i++ )) do do if [ $((${a[$i]}%2)) -eq 0 ] if [ $((${a[$i]}%2)) -eq 0 ] then then sum_even=$(($sum_even+${a[$i]})) sum_even=$(($sum_even+${a[$i]})) else else sum_odd=$(($sum_odd+${a[$i]})) sum_odd=$(($sum_odd+${a[$i]})) fi fi done done echo echo "=================================" "=================================" echo "
echo " SUM SUM OF OF ODD ODD NUMBERS NUMBERS : : $sum_odd$sum_odd ""
echo " SUM
echo " SUM OF EVEN OF EVEN NUMBERS NUMBERS :: $sum_even " $sum_even " echo echo "=================================" "=================================" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$ shsh sum_odd_even.sh sum_odd_even.sh ENTER NO OF ELEMENTS ENTER NO OF ELEMENTS 4 4
ENTER THE NUMBERS: ENTER THE NUMBERS: 12 12 45 45 52 52 56 56 ================================= ================================= SUM
SUM OF OF ODD ODD NUMBERS NUMBERS : : 4545 SUM OF
SUM OF EVEN NUMBEEVEN NUMBERS RS : 120: 120
================================= =================================
[CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.5a.Largest of two using Function #Ex.No.5a.Largest of two using Function #largest_2.sh #largest_2.sh max() max() { { if [ $1 -gt $2 ] if [ $1 -gt $2 ] then then return $1 return $1 else else return $2 return $2 fi fi } }
echo "Enter two numbers : " echo "Enter two numbers : " read a b
read a b max $a $b max $a $b
echo "MAXIMUM AMONG ( $a and $b echo "MAXIMUM AMONG ( $a and $b ) IS :) IS : $?"
$?"
OUTPUT: OUTPUT:
[CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh largest_2.shsh largest_2.sh Enter two numbers :
Enter two numbers : 56 23
56 23
MAXIMUM AMONG ( 56 and 23 ) IS : MAXIMUM AMONG ( 56 and 23 ) IS : 5656
[CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.5b.Sum of two using Function #Ex.No.5b.Sum of two using Function #sum_2_func.sh #sum_2_func.sh sum() sum() { { return $(($1+$2)) return $(($1+$2)) } }
echo "Enter two numbers : " echo "Enter two numbers : " read a b read a b sum $a $b sum $a $b echo "SUM ( $a + $b ) IS : $?" echo "SUM ( $a + $b ) IS : $?" OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh sum_2_func.shsh sum_2_func.sh Enter two numbers :
Enter two numbers : 56 34 56 34 SUM ( 56 + 34 SUM ( 56 + 34 ) IS : 90) IS : 90 [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
#Ex.No.5c.Find Factorial using Recursive #Ex.No.5c.Find Factorial using Recursive Function Function #fact_func.sh #fact_func.sh fact() fact() { { if [ $1 -gt 1 ] if [ $1 -gt 1 ]
then then i=`expr $1 - 1 ` i=`expr $1 - 1 ` j=`fact $i ` j=`fact $i ` x=`expr $1 \* $j ` x=`expr $1 \* $j ` echo $x echo $x else else echo 1 echo 1 fi fi } }
echo "ENTER THE NUMBER : " echo "ENTER THE NUMBER : " read num read num fact $num fact $num OUTPUT: OUTPUT: [CPL_2_lab@localhost SH]$
[CPL_2_lab@localhost SH]$ sh fact_func.shsh fact_func.sh ENTER THE NUMBER :
ENTER THE NUMBER : 5 5 120 120 [CPL_2_lab@localhost SH]$ [CPL_2_lab@localhost SH]$
UNIX ‘C’ PROGRAMMING
UNIX ‘C’ PROGRAMMING
1.
1. UniUnix ‘x ‘C’ C’ proprogragrams ms usiusing Fng FILEILES.S. a.
a. WriWrite a te a C prC progrogram tam to peo perforform frm fileile copy operation.
copy operation. b.
b. Write a C program to create two filesWrite a C program to create two files and perform merge operation on it and perform merge operation on it c.
c. WrWritite a C pre a C progrogram wam witith an ah an appppendend operation on an existing file.
operation on an existing file. d.
d. WrWritite e a a C C prprogogrram to cram to creaeatte e a a ffilile,e, wh
whicich h cocontntaiains ns reregigistster er nunumbmberer,, na
nammee, , aage ge eettcc. . aand nd ddiissppllaay y tthhee contents of the file.
contents of the file. e.
e. WrWritite e a a C C prprogogrram to cram to creaeatte e a a ffilile,e, which contains two numbers followed which contains two numbers followed by an operator in
by an operator in a line. Read a a line. Read a line of line of the file, while reading it; with respect the file, while reading it; with respect to the operator perform the operation to the operator perform the operation against the numbers.
against the numbers.
2.
2. UniUnix ‘C’ prx ‘C’ progrogramams usins using FUNg FUNCTICTIONONS.S.
a.
a. WriWrite a te a C prC progrogram tam to fio find tnd the she sum oum of f digits of a number using function. digits of a number using function. b.
b. Write a C program to check whether Write a C program to check whether the given number and its reverse are the given number and its reverse are same using function.
same using function. c.
c. WriWrite a te a C prC progrogram tam to fio find tnd the she sum oum of f square of individual digits of a
square of individual digits of a number using function.
number using function. d.
d. WriWrite a te a C prC progrogram tam to fio find tnd the she sum oum of f cube of individual digits of a number cube of individual digits of a number using function.
using function. e.
e. WriWrite a te a C prC progrogram tam to fio find tnd the lhe largargestest digit from a number using function. digit from a number using function. f.
f. WrWritite a C pre a C progrogram tam to fio find tnd the she sececonondd largest digit from a number using largest digit from a number using function.
function. g.
g. WriWrite a C pte a C progrogram ram to fto find tind the smhe smallallestest digit from a number using function. digit from a number using function. h.
h. WriWrite a te a C prC progrogram tam to fio find tnd the she sum oum of f ‘n’ numbers using function.
‘n’ numbers using function.
3.
3. Unix Unix ‘C’ ‘C’ proprogramgrams usis using ang arrayrrays wits with Dyh Dynaminamicc Memory Allocation.
Memory Allocation.
a.
a. WrWritite a C proe a C progrgram uam usising dyng dynanamimicc memory allocation to sort ‘n’ names memory allocation to sort ‘n’ names in ascending order.
in ascending order. b.
b. Write a C program using dynamicWrite a C program using dynamic memory allocation to sort ‘n’ names memory allocation to sort ‘n’ names in descending order.
in descending order.
cc.. WWrriitte e a a C C prprooggrraam m uussiinng g ddyynnaammiicc m
meemmoorry y aallllococaattiion on tto o ffiinnd d tthhee
minimum and maximum of a set of minimum and maximum of a set of numbers.
numbers. d.
d. WriWrite te a C a C proprogragram um usinsing dyg dynamnamicic memory allocation to perform linear memory allocation to perform linear search operation.
search operation. e.
e. WrWritite a C proe a C progrgram uam usising dyng dynamnamicic memory allocation to perform binary memory allocation to perform binary search operation.
search operation. f.
f. WrWritite a e a C proC progrgram usam usining poig pointnterers tos to al
allolocacate te a a mememomory ry fofor r ththe e ststriringng “U
“Uniniveverrsisityty” ” anand d rerealalllococatate e tthehe memory for the string to “University memory for the string to “University Examinations”.
Examinations”. 4.
4. Unix ‘C’ programs using Matrix with DynamicUnix ‘C’ programs using Matrix with Dynamic Memory Allocation.
Memory Allocation.
aa.. WWrriitte ae a CC prograprogram m usiusing ng dyndynamiamicc m
meemmoorry y aallllooccaattiioon n tto o aaddd d ttwwoo matrices.
matrices.
b.
b. Write Write a a C C program program using using dynamicdynamic me
memomory ry alallolocacatition on to to susubtbtraract ct twtwoo matrices.
matrices.
c.
c. WrWritite a C proe a C progrgram uam usising dyng dynamnamicic memory allocation to multiply of two memory allocation to multiply of two matrices
matrices d.
d. WriWrite te a C a C proprogragram um usinsing dyg dynamnamicic memory allocation to perform memory allocation to perform transpose of a given matrix. transpose of a given matrix. e.
e. WrWritite a C proe a C progrgram uam usising dyng dynamnamicic memory allocation to copy one matrix memory allocation to copy one matrix to another matrix
to another matrix f.
f. WrWritite a C pe a C prorogrgram uam usising dng dynynamamicic memory allocation to find the sum of memory allocation to find the sum of elements of a matrix.
elements of a matrix.
5.
5. UniUnix ‘Cx ‘C’ p’ progrrograms ams usinusing Stg Strucructuretures wis withth Dynamic Memory Allocation.
Dynamic Memory Allocation.
a.
a. WrWritite a C proe a C progrgram uam usising dyng dynamnamicic memory allocation to develop a mark memory allocation to develop a mark sheet of 5 students.
sheet of 5 students. b.
b. Write a C program using dynamicWrite a C program using dynamic memory allocation to develop salary memory allocation to develop salary details of 5 employees
details of 5 employees 6.
6. UniUnix ‘Cx ‘C’ pr’ progrograms ams usiusing pong pointintersers.. a.
a. WriWrite a te a C prC progrogram uam usinsing pog pointinters ers toto combine two array elements without combine two array elements without duplicate
duplicate b.
b. Write a C program using pointers toWrite a C program using pointers to copy the content of one array to copy the content of one array to another
another c.
c. WriWrite a te a C prC progrogram uam usinsing pog pointinters ers toto swap two numbers.