linux_practical_new
linux_practical_new
3,
INDEX
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
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
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-
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
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.
4
2. Write a Shell Program to find Sum, Product and Average of Four Integers entered.
5
3 .Write a Shell Program to Exchange values of two variables.
6
4 Write a Shell Program to display the digits which are at Odd positions in
a given 5 digit number.
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.
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"
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"
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"
14
read n
f=1
while [ 1 -le $n ]
do
f=`expr $f /* $n`
n=`expr $n - 1` done
echo "$f"
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
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.
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
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
#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");
// 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
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
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
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.
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.
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
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
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.)
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.
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.
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
\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.
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.
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
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