0% found this document useful (0 votes)
22 views

linux_practical_new

This document is a practical file for a course on Linux and Shell Programming at Kurukshetra University, detailing various shell programming exercises. It includes a comprehensive index of programs that cover tasks such as basic arithmetic operations, string manipulation, file handling, and user input validation. The document is intended for students in the MCA program for the session 2022-2024.

Uploaded by

pankaj sharma
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)
22 views

linux_practical_new

This document is a practical file for a course on Linux and Shell Programming at Kurukshetra University, detailing various shell programming exercises. It includes a comprehensive index of programs that cover tasks such as basic arithmetic operations, string manipulation, file handling, and user input validation. The document is intended for students in the MCA program for the session 2022-2024.

Uploaded by

pankaj sharma
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
You are on page 1/ 46

Linux

Computer Engineering (Kurukshetra University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any colle


3,
Practical File
For
LINUX AND SHELL
PROGRAMMING
MCA-20-22

Department of Computer Science &


Applications Kurukshetra University,
Kurukshetra
Session(2022-2024)

Submitted to: Submitted By:


Dr. Ashish Girdhar Name – TANNU
Class – MCA- I (B)
ROLL NO. - 82

3,
INDEX

Sr Name of Program Page No Signature


No
Write a Shell script to ask your Name, Program Name, Enrollment 4
1 Number and print it on screen.
Write a Shell Program to find Sum, Product and Average of Four 5
2 Integers entered.
Write a Shell Program to Exchange values of two variables. 6
3
Write a Shell Program to display the digits which are at odd positions 7
4 in a given 5-digit number.
Write a Shell Program to reverse the digits of 5-digit integer. 8
5
Write a Shell Program to find Sum of all digits in a given 5-digit 9
6 number.
Write a Shell Program to find largest among 3 given numbers. 10
7
Write a Shell Program to find smallest of 3 given numbers. 11
8
Write a Shell Program to find Factorial of a given number. 12
9
Write a Shell Program to find position of substring in a given string. 13
10
Write a Shell Program, which accepts name of a file from the standard
11 input and perform the following tests on it:
a) File Existence
b) File Readable 14
c) File Writeable
d) Both Readable & Writeable

Write a Shell Program to check whether given year is a Leap year or 15


12 not.
Write a Shell script to find G.C.D for the 2 given numbers. 16
13
Write a Shell script to search a pattern using Grep Command. 17
14
Write a Shell script to search for a given number from list of numbers 18
15 provided using Binary Search Method.
Write a C program to perform Bubble Sort using gcc compiler. 19
16
Write a Shell script to find Average of the numbers entered in 20
17 Command Line.
Write a Shell Program using 3 arguments to take the pattern as well
18 as input and output file names. If the pattern is found, display 21
“Pattern Found” else display “Error Message”. Also check if right
number of argument is entered
1

3,
Write a Shell script to check whether entered character is a vowel or 22
19 not, using Positional Parameters.
20 Write a Shell script to use a command from the given Menu. 23

21 Write a Shell Program which accepts the name of the File from
standard input and performs the following operations:
a) Enter 5 names in a File.
b) Sort the names in existing File. 24
c) List Unsorted and Sorted File.
d) Quit

22 a) Write a Shell Program to prepare the ElectricityBill based on


fol- lowing rules:
For First 100 units – Rs. 1.00/unit
For Next 100 units – Rs. 2.00/unit
Above 200 units – Rs. 3.00/unit
25-26
b) Write a Shell Program to prepare the ElectricityBill based on
fol- lowing conditions(Illustrates bc utility)
For First 100 units – Rs. 0.75/unit
For Next 100 units – Rs. 1.50/unit
Above 200 units – Rs. 3.00/unit

23 Write a Shell Script to Sum up the following series: (1/1!) + (2/2!) + 27


(3/3!) + (4/4!) + .....

24 a) Write a Shell Script to display the Result “PASS” or “FAIL”


using information given below: Student Name, Student Register
Number, Marks 1, Marks 2, Marks 3, Marks 4. The Minimum passing
marks for each subject is 50.
28-29
b) Write a Shell Script to display the Result of a Student in Neat
Format using information given below: Student Name, Student Registe
r Number, Marks 1, Marks 2, Marks 3, Marks 4. The Minimum passin
g marks for each subject is 50.
25 a) Write a Shell Script to check whether a Number is Palindrome or
not.
b) Write a Shell Script to check whether a String is a Palindrome or 30-31
not.

26 Write a Shell Script to Check the User is eligible for Vote or Not.
(One must attain 18 years for Voting . Ignore Month differences.) 32

27 Write a Shell Script to merge the Contents of Three given files, Sort
them and display the sorted output on screen page by page. Display
the list of last 20 files present in current directory. Also, Store this list 33
in a File name -profile.

3,
28 Write a Shell Script to Enhance cp command to Copy files. Display

the necessary error message if error occurs. 34

29 Write a Shell Script for a file contains record with each containing
name of City, State, Country. How would you Sort this file with
Coun-

try as Primary Sort Key and State as Secondary Sort Key. 35

30 Write a Menu driven Shell Program to perform the following tasks:


a) Enter Sentence in File

b) Search a given Whole Word in an existing File. c) 36


Quit.

31 Write a Menu driven Shell Program to Copy, Edit, Rename and De-

lete a File. 37

32 Write a Menu driven Shell Script for Converting all Capital letters in

a File to Small case letters and vice-versa. 38

33 Write a Shell Script to do following on files of current directory


based on file extensions.

Move all the C language files into the subdirectory –c. Move
all the Shell Scripts into the Subdirectory –shell. 39
Move all the Text Files into the Subdirectory –text.
Move all the Java Files into the Subdirectory –java.

3,
3
1. Write a Shell script to ask your Name, Program Name, Enrollment Number and
print it on screen.

echo "enter the name" read


name
echo "enter the program_name" read
pn
echo "enter the enrollment_number"
read en echo "$name" echo "$pn"
echo "$en"

4
2. Write a Shell Program to find Sum, Product and Average of Four Integers entered.

echo "enter the numbers"


read a read
b read c
read d
sum=`expr $a + $b + $c + $d`
echo "sum is = $sum"
avr=`expr $sum / 4` echo
"average = $avr" prd=`expr
$a \* $b \* $c \* $d` echo
"product = $prd"

5
3 .Write a Shell Program to Exchange values of two variables.

echo "enter two value"


read a b echo "before
swap" echo "$a $b"
temp=$a a=$b
b=$temp echo "after
the swap" echo "$a $b"

6
4 Write a Shell Program to display the digits which are at Odd positions in
a given 5 digit number.

echo "enter the numbers="


read a n=1
while [ $n -le 5 ] do
b=`expr $a | cut -c $n`
n=`expr $n + 2` echo
"$b"
done

7
8
5. Write a Shell Program to reverse the digits of 5 digit integer.

Echo
read a
num=0
while [ $a -ne 0 ] do
b=`expr $a % 10` num=`expr
$num \* 10 + $b`
a=`expr $a / 10`
done echo
"$num"

9
6 .Write a Shell Program to find Sum of all digits in a given 5 digit number.

echo "enter the numbers="

10
read a
sum=0
while [ $a -ne 0 ] do
b=`expr $a % 10`
sum=`expr $sum + $b`
a=`expr $a / 10` done
echo "$sum"

7. Write a Shell Program to find largest among 3 given numbers.

11
echo "enter three numbers"

12
read a read
b read c
gr=$a
if [ $b -gt $gr ]
then gr=$b
fi
if [ $c -gt $gr ]
then gr=$c
fi
echo " the largest of three no. is : $gr"

8. Write a Shell Program to find smallest of 3 given numbers.

13
echo "enter three numbers"
read a read
b read c
gr=$a
if [ $b -lt $gr ]
then gr=$b
fi
if [ $c -lt $gr ]
then gr=$c
fi
echo "the smallest of three no. is :$gr"

9. Write a Shell Program to find Factorial of a given number.

echo "enter a number"

14
read n
f=1
while [ 1 -le $n ]
do
f=`expr $f /* $n`
n=`expr $n - 1` done
echo "$f"

10. Write a Shell Program to find position of substring in a given string.

15
echo "enter the string=" read
a
echo "enter the substring position=" read
b
s1=`expr $a | wc -c`
s2=`expr $b | wc -c`
s1=`expr $s1 - 1`
s2=`expr $s2 - 1`
i=1 j=1
pos=0
while [ $i -le $s1 ] do
c=`expr $a | cut -c $i`
d=`expr $b | cut -c $j`
if [ $c = $d ] then
i=`expr $i + 1`
j=`expr $j + 1`
pos=`expr $i - $s2`
r=`expr $j - 1` if [ $r
-eq $s2 ]
then break
fi
else pos=0 j=1
i=`expr $i + 1`
fi
done echo "position of substring in
string=$pos"

16
11. Write a Shell Program, which accepts name of a file from the standard input
and perform the following tests on it :

17
a) File Exsitence
b) File Readable
c) File Writeable
d) Both Readable & Writeable

echo "enter filename"


read filename a=0
if test -e $filename then
echo "file exist" else
echo "file does not exist" fi
if test -r $filenmae
then echo "file
readable" a=`expr
$a + 1`
else
echo "files is not readable" fi
if test -w $filename
then echo "file
writeable" a=`expr
$a + 1`
else
echo "file is not writeable" fi
if [ $a = 2 ] then
echo "file is both readable and writeable"
fi

12. Write a Shell Program to check whether given year is a Leap year or not.

18
echo "enter the year" read
a
if [ `expr $a % 4` = 0 ] then
echo "$a is a leap year" else
echo "$a is not leap year"
fi

13. Write a Shell script to find G.C.D for the 2 given numbers.

echo "enter two numbers:"

19
read a read
b temp=$a

if [ $b -lt $temp ]
then temp=
$b
fi
while [ $temp -ne 0 ] do
m=`expr $a % $temp`
n=`expr $b % $temp` if
[ $m -eq 0 -a $n -eq 0 ]
then
echo "GCD of two numbers is: $temp" break
fi
temp=`expr $temp - 1` done

14. Write a Shell script to search a pattern using Grep Command.

20
cow is a animal cow
has two horns cow
has a long tail cow
has two ears cow
gives us milk
i love my cow

15. Write a Shell script to search for a given number from list of numbers
provided using Binary Search Method.

21
echo -n "enter the number you can get:" read
n
echo "enetr number" i=0
while [ $i -lt $n ]
do read m
a[i]=$m
i=`expr $i + 1`
done
echo "enter the element to find"
read b l=0 c=0 u=`expr $n - 1`
while [ $l -le $u ] do mid=$
(((($l + $u))/2)) if [ $b -eq $
{a[$mid]} ] then c=1 break
elif [ $b -lt ${a[$mid]} ]
then u=$(($mid - 1))
else l=$(($mid + 1))
fi
done
if [ $c -eq 1 ] then
echo "element is found in position $(($mid + 1))" else
echo "element not found" fi

16. Write a C program to perform Bubble Sort using gcc compiler.

#include <stdio.h>

22
int main()
{
int a[10],i,j,n,temp;
printf("Enter the size of array:
"); scanf("%d",&n);

for (i=0;i<n;i++)
scanf("%d",&a[i]);

printf("\nArray: ");
for (i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");

// code for bubble sort starts


now for (i=1;i<=n-2;i++)
{
for (j=0;j<n-1;j++)
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

// sorted array
printf("Sorted Array:
"); for (i=0;i<n;i++)
printf("%d ",a[i]);

printf("\n");
return 0;
}

17.Write a Shell script to find Average of the numbers entered in Command Line.

avg() {

23
res=$(( ($1 + $2 + $3 + $4)/ 3 )) return
$res
}
avg $1 $2 $3 $4 res=$?
echo "result : $res"

18. Write a Shell Program using 3 arguments to take the pattern as well as input and
output file names. If the pattern is found, display “Pattern Found” else display
“Error Message”. Also check if right number of argument is entered.

24
if test $# -eq 3
then
if grep "$1" $2 > $3 then
echo "pattern found"
echo "the content of output file is"
cat $3 else
echo "error message"
fi else
echo "argument are not correectly passed"
fi

19. Write a Shell script to check whether entered character is a vowel or not,
using Positional Parameters.
if [[ $1 == [AEIOUaeiou] ]]
then

25
echo "it is vowel"
else echo "not
vowel"
fi

20 .Write a Shell script to use a command from the given Menu.

echo "enter your choice [y/n]"


read ch while [ $ch = 'y' ] do

26
echo -e "menu\n1.copy\n2.Edit\n3.Rename\n4.Delete\n Enter your choice" read
op
echo "Enter source file name"
read src case
$op in
1)
echo "enter destination file name" read
dest
cp $src $dest
;;
2) vi
$src
;;
3) echo "enter new name of
file" read new
mv $src $new
;;
4) rm $src
;;
esac
echo "do yo want to continue?[y/n]" read
ch
done

21. Write a Shell Program which accepts the name of the File from standard input
and performs the following operations:
a) Enter 5 names in a File.
b) Sort the names in existing File.

27
c) List Unsorted and Sorted File.
d) Quit

echo "Enter the filename"


read file
echo "enter five name and press ^d"
cat>$file sort $file>target echo
"unsorted list" cat $file echo "sorted
list" cat target

22. a) Write a Shell Program to prepare the ElectricityBill based on following rules:
For First 100 units – Rs. 1.00/unit
For Next 100 units – Rs. 2.00/unit
Above 200 units – Rs. 3.00/unit

28
echo -n "enter the customer number" read
no
echo -n "enter the customer name" read
name
echo -n "enter the previous reading" read
prev
echo -n "enter the current reading" read
cur
units=`expr $cur - $prev`
if [ $units -gt 200 ] then
temp=`expr $units - 200`
bill=$(($temp * 3))
units=`expr $units - $temp` fi
if [ $units -gt 100 ] then
temp=`expr $units - 100` bill=`expr
$bill + $(($temp * 2))`
units=`expr $units - $temp` fi
if [ $units -le 100 ]
then
bill=`expr $bill + $units` fi
echo "Electicity bill" echo
"customer number:"$no
echo "customer name:"$name echo
-n "total billof this month:"$bill

22. b) Write a Shell Program to prepare the ElectricityBill based on


following conditions(Illustrates bc utility)
1. For First 100 units – Rs. 0.75/unit
2. For Next 100 units – Rs. 1.50/unit
3.Above 200 units – Rs. 3.00/unit

29
echo -n "enter the customer number" read
no
echo -n "enter the customer name" read
name
echo -n "enter the previous reading" read
prev
echo -n "enter the current reading" read
cur
units=`expr $cur - $prev`
if [ $units -le 100 ] then
charge=$(echo -e "scale =2\n $units * 0.75" | bc)
elif [ $units -le 200 ] then
consume=$( expr $units - 100 ) charge=$(echo -e
"scale =2\n $consume * 1.50" | bc) charge=$(echo -e
"scale =2\n $charge + 75" | bc) elif [ $units -gt 200 ]
then
consume=$( expr $units - 200 ) charge=$(echo -e
"scale =2\n $consume * 3" | bc) charge=$(echo -e
"scale =2\n $charge + 225" | bc) fi
echo "Electicity bill" echo
"customer number:"$no echo
"customer name:"$name
echo "total bill:"$charge

23. Write a Shell Script to Sum up the following series: (1/1!) + (2/2!) +
(3/3!) + (4/4!) + .....

30
echo -n "enter numbers of terms:"
read n fact=1 count=1 sum=0
while [ $count -le $n ] do
fact=$(expr $fact \* $count) sum=$(echo -e "scale=2\n
$sum + $count / $fact" | bc) count=$(expr $count + 1)
done echo -e "the sum of given series:"$sum

24. a) Write a Shell Script to display the Result “PASS” or “FAIL” using
information given below: Student Name, Student Register Number, Marks 1,
Marks 2, Marks 3, Marks 4. The Minimum passing marks for each subject is 50.

echo -n " enter the student name :" read


name
echo -n "enter the registration number :" read
rno

31
echo " enter marks of foue sub one by one"
read m1 read m2 read m3 read m4
echo -n "name :" $name
echo -ne "\n registration number :" $rno if [ $m1 -ge 50 -a
$m2 -ge 50 -a $m3 -ge 50 -a $m4 -ge 50 ] then
echo -ne "\n result : pass" else
echo -ne "\n result : failed"
fi

24.b) Write a Shell Script to display the Result of a Student in Neat Format using
information given below: Student Name, Student Register Number, Marks 1,
Marks 2, Marks 3, Marks 4. The Minimum passing marks for each subject is 50.

echo -n " enter the student name :" read


name
echo -n "enter the registration number :" read
rno
echo " enter marks of foue sub one by one"
read m1 read m2 read m3 read m4

32
echo -n "name :" $name
echo -ne "\n registration number :" $rno
echo -e "\nSubject\tObtained marks\tMaximum marks" echo -e "\nSubject1\t$m1\t100\
nSubject2\t$m2\t100\nSubject3\t$m3\t100\nSubject4\t$m4\t100" if [ $m1 -ge 50 -a $m2 -
ge 50 -a $m3 -ge 50 -a $m4 -ge 50 ] then
echo -ne "\n result : pass" else
echo -ne "\n result : failed"
fi

25. a) Write a Shell Script to check whether a Number is Palindrome or not.

echo -n "ENTER THE NUMBER:"


read str ins=$str
count=`echo $str | wc -c`
while [ $count -gt 0 ] do
ch=`echo $str | cut -c $count`
count=`expr $count - 1`
rev=`echo $rev$ch` done

33
echo -e "Given string:"$ins echo
"Rever of the string:"$rev
if [ $ins = $rev ]
then
echo "The given number is PALINDROME." else
echo "The given number is NOT PALINDROME."
Fi

25 b) Write a Shell Script to check whether a String is a Palindrome or not.

echo -n "ENTER THE STRING:"


read str ins=$str
count=`echo $str | wc -c`
while [ $count -gt 0 ] do
ch=`echo $str | cut -c $count`
count=`expr $count - 1`
rev=`echo $rev$ch` done
echo -e "Given string:"$ins echo
"Rever of the string:"$rev
if [ $ins = $rev ]

34
then
echo "The given string is PALINDROME." else
echo "The given string is NOT PALINDROME."
Fi

26. Write a Shell Script to Check the User is eligible for Vote or Not. (One must
attain 18 years for Voting . Ignore Month differences.)

echo -n "enter your birth year:"


read year cyear=`date +%Y`
age=`expr $cyear - $year` if
test $age -ge 18 then
echo "you are eligible for vote" else
echo "you are not eligible for vote"
fi

35
27. Write a Shell Script to merge the Contents of Three given files, Sort them and
display the sorted output on screen page by page. Display the list of last 20 files
present in current directory. Also, Store this list in a File name -profile.

echo "enter the name of file 1" read


file1
echo "enter the name of file 2" read
file2
echo "enter the name of file 3" read
file3
sort -m $file1 $file2 $file3 > mergefile
echo -e "\nSorted content:\n" sort
mergefile | more

36
echo -e "\nLast 20 file present in current directory are:\n" ls
| tail -20 | tee profil

28. Write a Shell Script to Enhance cp command to Copy files. Display the
necessary error message if error occurs.
if test $# -eq 0
then
echo "specify source and destination filename" echo
"usage: $0<source filename><destination filename>" elif
test $# -eq 1
then
echo "specify destination filename"
echo "usage: $0<source filename><destination filename>"
elif test $# -gt 2
then
echo "argument list is too long"

37
echo "usage: $0<source filename><destination filename>"
elif test ! -r $1 then
echo "source file not exists" elif
test -d $1
then
echo "source $1 is directory , not a file" elif
test ! -r $1
then
echo "source file not readable" elif
test -c $2 -a ! -w $2
then
echo "destination not writable"
elif test -f $2 then
echo -n "destination file exists overwrite [y/n]:"
read choice if test $choice = "y" then cp $1 $2 fi
else cp $1 $2 fi

29. Write a Shell Script for a file contains record with each containing name of
City, State, Country. How would you Sort this file with Country as Primary Sort
Key and State as Secondary Sort Key.

echo -n "enter the file name"


read fname choice="y"
while [ $choice = "y" ]
do
echo -ne "\n enter the city:" read
city
echo -n "enter states :" read
state
echo -n "enter country :" read
country
echo "$city $state $country" >> $fname

38
echo -ne "\n Do you want to add another record [y/n] :"
read choice done
echo -e "\n given input records are: \n" cat
$fname
sort +2 +1 -2 $fname > sortfile
echo -e "\n sorted content is :\n" cat
sortfile

30. Write a Menu driven Shell Program to perform the following


tasks: a)
b) Sentence in File
c) Search a given Whole Word in an existing File.
d) Quit.

\while true do
echo -e "\nmenu\n1.enter sentence\n2.search word\n3.exit\nenter ur choice"
read ch case $ch in
1)
echo "enter file name"
read file

39
echo "enter the sentence and press^d" cat
> $file
;;
2)
echo "enter file name"
read file
echo "enter word to search"
read word if grep $word
$file then
echo "word found" else
echo "word not found"
fi ;;
3) exit
;;
esac done

31. Write a Menu driven Shell Program to Copy, Edit, Rename and
Delete a File.

echo "enter your choice [y/n]"


read ch while [ $ch = 'y' ] do
echo -e "menu\n1.copy\n2.Edit\n3.Rename\n4.Delete\n Enter your choice" read
op
echo "Enter source file name"
read src case $op in
1)
echo "enter destination file name" read
dest
cp $src $dest

40
;;
2) vi
$src
;;
3) echo "enter new name of
file" read new
mv $src $new
;;
4) rm $src
;;
esac
echo "do yo want to continue?[y/n]" read
ch
done

32. Write a Menu driven Shell Script for Converting all Capital letters in a
File to Small case letters and vice-versa.

choice="y" while [ $choice = "y" ] do echo -e "\t\tMenu\n1.UPPERCASE TO LOWERCASE\


n2.LOWERCASE TO
UPPERCASE\nEnter your choice" read
ch
echo -n "enter source file" read
src
echo -e "enter target file"
read trgt case $ch in
1)
dd if=$src of=$trgt conv=lcase 2>> /dev/null
;;

41
2)
dd if=$src of=$trgt conv=ucase 2>> /dev/null
;;
esac
echo -ne "\nDo you want to continue [y/n]:" read
choice
done

33. Write a Shell Script to do following on files of current directory based on


file extensions.
• Move all the C language files into the subdirectory –c.
• Move all the Shell Scripts into the Subdirectory –shell.
• Move all the Text Files into the Subdirectory –text.
• Move all the Java Files into the Subdirectory –java.

for file in * do
case $file in
*.c) mv
$file C/
;;

42
*.sh)
mv $file Shell/
;;
*.txt)
mv $file TEXT/
;;
*.java) mv $file
JAVA/
;;
esac done

43

You might also like