Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Practical

File Of
LINUX

Submitted To: Submitted By:

Dr. Sanjay Tyagi Prince

ROLL NO - 07

SECTION-A
INDEX

1. Write a Shell program, which accepts the name of a file from the standard input
and perform the following tests on it:
(i) File existence
(ii) File readable
(iii) File writeable
(iv) Both readable and writeable

2. 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 of argument is entered.

3. Write a Shell program , which accepts the name of the file from the standard
input and the performs the following operation:
(i) Enter the 5 names in a file
(ii) Sort the names in existing file
(iii) List unsorted and sorted file
(iv) Quit

4. Write a menu driven Shell program to copy, edit, rename, and delete a file.

5. Write a menu driven Shell program to the performs the following tasks:
(i) Enter the sentence in file
(ii) Search a given whole word in an existing
file
(iii) Quit

6. (a) Write a Shell program to prepare the electricity bill based on the 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
(b) Write a Shell program to prepare the electricity bill based on the following
rules: For first 100 units - Rs. 0.75/unit For next 100 units - Rs. 1.50/unit Above
200 units - Rs. 3.00/unit

7. Write a Shell program to sum up the following series: (1 /1!) + (2/2!) + (3/3!) + ----
-
8. (a) Write a Shell script to display the result "PASS" or "FAIL" using the
information given below: Student Name, Student Register number, Marks 1 ,
Marks 2, Marks 3, Marks 4. The minimum pass for each subject is 50.

(b) Write a Shell script to display the result of a student in neat format using the
information given below: Student Name, Student Register Number, Marks 1 ,
Marks 2, Marks 3, Marks 4. The minimum pass for each subject is 50.
9. Write a menu driven Shell script for converting all the capital letters in a file to
small case letters and vice versa.

10. 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 the secondary sort key.

11. Write a Shell script to merge the contents of three given files, sort them and
display the sorted output screen page by page. Display the list of 20 files present
in the current directory. Also store this list in a file name-profile.

12. Write a Shell script to Enhance the cp command to copy files. Display the
necessary error message if error occurs.

13. Write a Shell script to check the user is eligible for vote or not. (one must attain
18 years for voting. Ignore month differences.)

14. Write a Shell script to do the following on the files of the current directory based
on file extensions.
• Move all the C language files into the subdirectory -c
• Move all the Shell file files into the subdirectory -shell
• Move all the Java files into the subdirectory - java
• Move all the Text files into the subdirectory - text

15. Write a Shell script to check whether a given string is Palindrome or not.
1. Write a Shell Program which accepts the name of a file from the standard input and
performs the following test on it:
i) File existence
ii)File readable
iii)File writeable
iv)Both readable and writeable

echo -n "Enter the filename "


read filename
if test -e $filename
then
echo "file exist"
else
echo "file does not exist "
fi

if test -r $filename
then
echo "file is readable"
else
echo "file is not readable"
fi

if test -w $filename
then
echo "file is writeable"
else
echo "file is not writeable"
fi

if test -r $filename -a -w $filename


then
echo "file is both readable and writeable"
else
echo "file is neither readable nor writeable"
fi

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

if test $# -eq 3
then
ifgrep "$1" $2 > $3
then
echo "pattern found "
echo " the content of the output file is :"
cat $3
else
echo "pattern not found "
fi
else
echo " Argument are not correctly passed "
fi
3. Write a Shell Program which accepts the name of a file from the standard input and
performs the following test on it:
i)Enter the five name in the file
ii)Sort the names in existing file
iii)List unsorted and sorted file
iv)Quit

echo -n "enter the file name "


readfname
echo " enter the five name "
cat> $fname
sort $fname>sortfile
echo " Sorted list "
cat sortfile
echo "Unsorted list "
cat $fname
4. Write a menu driven Shell Program to copy, edit, rename and delete a file.

choice=y
while test $choice = y
do
echo -n "Menu:
1. Copy
2. Edit
3. Rename
4. Delete
Enter your choice :"
readch
echo "enter source filename"
read source
case $ch in
1) echo " enter the target file name"
read target
cp $source $target
;;
2) vi $source
;;
3) echo "enter the new name "
read target
mv $source $target
;;
4) rm $source
;;
esac
echo -n " do you want to continue [y/n] "
read choice
Done
Before applying any operation the files are as follows:-

After Copy and Rename operation the data is :-


5. Write a menu driven Shell Program to perform the following tasks
i) Enter the sentence in file
ii) Search a given whole word in a existing fle
iii) Quit

choice=y
while test $choice = y
do
echo -n " Menu:
1. Enter the sentence in file
2. Search the word
3. Exit
Enter your choice :"
readch
case $ch in
1) echo -n "enter the file name "
readfname
echo "enter the whole sentence "
cat> $fname
;;
2) echo -n "enter the file name "
readfname
echo "enter the word you want to search"
read word
grep "$word" $fname|| echo "word not found"
;;
3) exit
;;
esac
echo -n "do you want to continue [y/n] "
read choice
done
6. (a) Write a Shell program to prepare the electricity bill based on the 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
#! /bin/sh
echo "Enter units"

read a

if [ $a -le 100 ]

then

echo "BILL: $a "

elif [ $a -le 200 ]

then

d=`expr $a -100`

c=`expr $d \* 2 + 100`

echo "Bill: $c"

else

d=`expr $a - 200`

c=`expr $d \* 3 + 300`

echo "Bill: $c"

fi
(b) Write a Shell program to prepare the electricity bill based on the following rules:
For first 100 units - Rs. 0.75/unit
For next 100 units - Rs. 1.50/unit
Above 200 units - Rs. 3.00/unit

#! /bin/sh

echo "Enter units"

read a

if [ $a -le 100 ]

then

e= `expr $a \* 0.75`

echo "BILL: $e "

elif [ $a -le 200 ]

then

d=`expr $a -100`

c=`expr $d \* 1.50 + 75`

echo "Bill: $c"

else

d=`expr $a - 200`

c=`expr $d \* 3 + 225`

echo "Bill: $c"

fi
7. Write a Shell script to sum up the following series:
( 1 / 1!) + ( 2 / 2!) + ( 3 / 3!)+……
echo -n "How many terms ? "
read n
fact=1
cnt=1
sum=0

while [ $cnt -le $n ]


do
fact=`expr $fact \* $cnt`
sum=`echo -e "scale=6 \n $sum + 1 / $fact" | bc`
cnt=`expr $cnt + 1`
done

echo "SUM OF THE GIVEN SERIES IS $sum "


8.(a) Write a Shell script to display the result of a student “PASS” or “FAIL” using the
information given below:
Student Name, Student Register Number, Mark1, Mark2 ,Mark3, Mark4. Minimum
passing marks for each subject is 50.
echo -n "enter the student name :"
read name
echo -n "enter the registration no :"
read no
echo " enter the marks of four subject: "
read m1
read m2
read m3
read m4

if [ $m1 -ge 50 -a $m2 -ge 50 -a $m3 -ge 50 -a $m4 -ge 50 ]


then
echo " PASS "
else
echo "FAIL "
fi
(b) Write a Shell script to display the result in neat format using the information given
below:
Student Name, Student Register Number, Mark1, Mark2,Mark3, Mark4. Minimum
passing marks for each subject is 50.

echo -n "enter the student name :"


read name
echo -n "enter the registration no :"
read no
echo " enter the marks of subject1: "
read m1
echo " enter the marks of subject2: "
read m2
echo " enter the marks of subject3: "
read m3
echo " enter the marks of subject4: "
read m4

if [ $m1 -ge 50 ]
then
r1=" PASS "
else
r1=" FAIL "
fi
if [ $m2 -ge 50 ]
then
r2=" PASS "
else
r2=" FAIL "
fi
if [ $m3 -ge 50 ]
then
r3=" PASS "
else
r3=" FAIL "
fi
if [ $m4 -ge 50 ]
then
r4=" PASS "
else
r4=" FAIL "
fi

if [ $m1 -ge 50 -a $m2 -ge 50 -a $m3 -ge 50 -a $m4 -ge 50 ]


then
result=" PASS "
else
result=" FAIL "
fi

echo -e "\n\t RESULT\n\t"


echo -e "========================================"
echo -e "STUDENT NAME : $name"
echo -e "STUDENT REGISTRAION NO : $no"
echo -e "========================================"
echo -e "SUBJECT \t MARKS \t RESULT"
echo -e "C \t $m1 \t $r1"
echo -e "C++ \t $m2 \t $r2"
echo -e "dos \t $m3 \t $r3"
echo -e "LINUX \t$ m4 \t $r4"
echo -e "========================================"
echo -e "OVERALL RESULT IS $result"

9. Write a menu driven Shell Script for converting all capital letter in a file to small letter
and vice versa.

choice=y
while test $choice = y
do
echo -n "Menu
1. UPPERCASE TO LOWERCASE
2. LOWERCASE TO UPPERCASE
Enter your choice : "
readch
echo -n "Enter Source File name :"
read s
echo -n "Enter target file name :"
read t
case $ch in
1) dd if=$s of=$t conv=lcase
;;
2) dd if=$s of=$t conv=ucase
;;
esac
echo -n "Do you want to continue [y/n] "
read choice
done

Before operation:-
After operation:-

10. Write a Shell script for a file contains record with each record containing name of a city,
name of state and name of county. How would you sort this file with country as the
primary sort key and state as the secondary sort key.

echo -n "enter the filename :"


readfname
choice=y
while test $choice = y
do
echo -n "Enter City : "
read city
echo -n "Enter State : "
read state
echo -n "Enter Country : "
read country
echo "$city $state $country " >> $fname
echo -n "Do you want to continue [y/n] "
read choice
done

echo "UNSORTED FILE "


echo "======================================================="
cat $fname
sort +2 +1 -2 $fname>sortfile
echo "SORTED FILE"
echo "======================================================="
cat sortfile
Output”-

11. Merge the content of three given file, sort them and display the sorted output on the
screen page by page.
Display the list of last 20 files present in the current directory. Also store this list in a file
name –profile.

echo -n " Enter the name of the first file "


read fl1
echo -n " Enter the name of the second file "
read fl2
echo -n " Enter the name of the third file "
read fl3
sort -m $fl1 $fl2 $fl3 > merge
echo "SORTED CONTENT IS : "
sort merge

echo -n " last 20 file present in this directory are "


ls | tail -20 | tee profile

Output:--
12. Enhance the copy command to copy files. Display the necessary error message if error
occurs.

if test $# -eq 0
then
echo " specify Source and Destination filename "
elif test $# -eq 1
then
echo " specify Destination filename "
elif test $# -gt 2
then
echo " more than enough argument "
elif test ! -e $1
then
echo " source file doesnot exists "
elif test -d $1
then
echo " source is a directory not file"
elif test ! -r $1
then
echo " source file is not readable "
elif test -e $2 -a ! -w $2
then
echo " Destination file is not writeable "

elif test -f $2
then
echo " Destination file exists. Overwrite [y|n] "
readch
if test $ch = y
thencp $1 $2
fi
fi

13. Write a Shell script to check if the user is eligible for vote or not.(age > 18)

echo -n "Enter your birth year "


read year

cyear=`date +%Y`
age=`expr $cyear - $year`

if test $age -gt 18


then
echo " Your are eligible for vote "
else
echo " Your are not eligible for vote "
fi
14. Write a Shell script to do following on the files of the current directory based on file
extensions.
 Move all the C language files in the subdirectory –c.
 Move all the Shell script in the subdirectory –shell.
 Move all the Text files in the subdirectory –text.
 Move all the Java files in the subdirectory –java.

for file in *
do
case $file in
*.c) mv $file $HOME/c;;
*.sh) mv $file $HOME/shell;;
*.txt) mv $file $HOME/text;;
*.java) mv $file $HOME/java;;
esac

done

15. Write a Shell script to check whether a given string is palindrome or not.

echo -n "Enter the String :"


readstr
instr=$str

count=`echo $str | wc -c`


while test $count -gt 0
do
ch=`echo $str | cut -c $count`
count=`expr $count - 1`
rev=`echo $rev$ch`
done

echo -e "\n GIVEN STRING: $instr"


echo "REVERSE OF THE STRING: $rev"

if test $instr = $rev


then
echo " THE GIVEN STRING IS PALINDROME."
else
echo " THE GIVEN STRING IS NOT PALINDROME."
fi

You might also like