Lab Manual It407linux
Lab Manual It407linux
SCIENCE
& TECHNOLOGY BHOPAL
DEPARTMENT OF INFORMATION
TECHNOLOGY
LAB MANUAL
NAME OF THE EXPERIMENT: Write a Shell Script that accepts a file name,
starting and ending line numbers as arguments and displays all lines between the
given line numbers.
AIM: By using bash shell for writing the shell scripts, first create any file in that
file read any line number as starting and ending line number and print all lines
between that line numbers.
ALGORITHM:
Step 1: Create a file with 5-6 lines of data
File can be created by vi text.txt or
cat text.txt Step 2:Now write a shell script
with
vi prg1.sh
step3:Check the no of arguments for
shell script if 0 arguments then
print no arguments else if 1
argument then print 1 argument
else if 2 arguments then print 2 arguments
else check for file is there or not(if file is not there print file does
not exists) else sed -ne ''$2','$3' p' $1
sed is one of powerful filter(stream editor)
-e default option (script on command line)
-n suppresses automatic output
$2 first line number passed $3 2nd line number passed
p is a print command (prints current content to the pattern space).
$1 is name of file from which we are printing data between the line
numbers.
SOURCE CODE
[lakshmi@localhost ~]$
vi prg1.sh if [ $# -eq 0 ]
then
echo no
arguements
elif [ $# -eq
1]
then
echo one
arguement
elif [ $# -eq
2]
then
echo two
arguements
else
i
f
-
e
$
1
t
h
e
n
echo file does
not exist else
sed -ne
''$2','$3' p'
$1 fi
fi
EXPECTED OUTPUT
Viva Questions
NAME OF THE EXPERIMENT: Write a shell script that deletes all lines containing a
specific word in one or more files supplied as arguments to it.
AIM: By using bash shell for writing the shell scripts, first create any file with 5-10 lines of
data, then specify any particular word, if that word is present in any lines of file those lines
will be deleted.
ALGORITHM :
Step 1: Create a file with 5-6 lines of data
Create the 2 file f1 and f2 as vi f1and vi f2
Step2: Now write a shell script with
vi prg2.sh
step3:Check the no of arguments for shell script
if 0 arguments then print no arguments
else pattern=$1(word will be stored in pattern)
for fname in $*
for every filename in given files
if it is a file if [ -f $fname ] then
SOURCE CODE:
EXPECTED OUTPUT
Viva Questions
NAME OF THE EXPERIMENT: Write a shell script that displays a list of files in current
directory to which the user has read, write and execute permissions.
AIM: In the current directory we are able know which files have read, write and execute
permissions.
ALGORITHM
Step1: Here we are working with current directory
Shell script will be written at command line
ls -l|grep '^.rwx'
ls is list command (list all the files in current directory)
-l long listing the files
grep acts as filter
it doesn’t print the files which does not have read, write and execute permissions
^ represents negation.
SOURCE CODE:
EXPECTED OUTPUT
METHOD 2:
echo "enter the directory name"
read dir
if [ -d $dir ]
then
cd $dir
ls > f
exec < f
while read line
do
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo "$line has all permissions"
else
echo "files not having all permissions"
fi
fi
done
fi
EXPECTED OUTPUT
student@ubuntu:~$sh prg3.sh
enter the directory name
dir1
ff has all permissions
files not having permissions
Method3:
echo "List of Files which have Read, Write and Execute Permissions in Current Directory"
for file in *
do if [ -r $file -a -w $file -a -x $file ]
then
echo $file
fi
done
INPUT: sh prog3.sh
OUTPUT: List of Files which have Read, Write and Execute Permissions in Current Directory f1 f2 f3
Viva Questions
1.What are states that the page can be in, after causing a page fault?
1. On a swap device and not in memory 2. On the free page list in the main memory,
3. In an executable file, 4. all
2.At what mode the fault handler executes?
1. execution mode 2. kernal mode
3. operation mode 4. none
3.Which one data structure is used for Demand Paging?
1. Page table entries, 2. Disk block descriptors,
3. Page frame data table (pfdata), 4. Sw
EXPERIMENT NO: 4
NAME OF THE EXPERIMENT: Write a shell script that receives any number of file
names as arguments checks if every argument is a file or directory, when it is a file,
report no of lines in it.
AIM: In this shell script we will supply the file names or directory names as arguments
if it is a file name then it prints no of lines in file, if it is a directory then it prints it is a
directory file.
ALGORITHM:
Step1: check files and directories in the current directory by ls command
Step2:create a shell script
vi prg3.sh
step3: Check the no of arguments for shell script
if 0 arguments then print no arguments
for fname in $*
for every filename in given files
if it is a file if [ -f $fname ] then
print it is a regular file
count=`wc -l $fname`
wc is used to count no of lines words characters
-l is used only to print lines.
print count
else
print it is a directory file.
SOURCE CODE
[lakshmi@localhost ~]$ vi prg3.sh
if [ $# -eq 0 ]
then
echo NO ARGUMENTS
else
for fname in $*
do
if [ -f $fname ]
then
echo $fname IS A REGULAR FILE
count=`wc -l $fname`
echo NUMBER OF LINES IN $fname ARE :
echo $count
else
echo $fname IS A DIRECTORY FILE
fi
done
fi
EXPECTED OUTPUT
Viva Questions
NAME OF THE EXPERIMENT: Write a shell script that accepts a list of file names as its
arguments, count and reports the occurrence of each word that is present in the first
argument file on other argument files.
AIM: In this shell script we will supply the file names as arguments, any word that is
present in first argument file can counted and printed if it present in other argument files.
ALGORITHM:
SOURCE CODE
if [ $# -eq 0 ]
then
echo “no arguments”
else
tr “ “ “\n”<$1>temp
shift
for i in $*
do
tr “””\n”<$i>temp1
y=`wc –l < temp`
j=1
while [ $j le $y ]
do
x=`head –n $j temp | tail –l`
c=`grep –c “$x” temp1`
echo $x $c
j=`expr $j + 1`
done
done
fi
EXPECTED OUTPUT
VIVA QUESTIONS
Shell scripting, in Linux or Unix, is programming with the shell using which you can
automate your tasks. A shell is the command interpreter which is the interface between the
User and the kernel. A shell script allows you to submit a set of commands to the kernel in
a batch. In addition, the shell itself is very powerful with many properties on its own, be it
for string manipulation or some basic programming stuff.
2. The command "cat file" gives error message "--bash: cat: Command not found". Why?
It is because the PATH variable is corrupt or not set appropriately. And hence the error
because the cat command is not available in the directories present PATH variable.
• Modification time:- Refers to the time when the file is last modified.
• Access time :- The time when the file is last accessed.
• Changed time :- The time when the attributes of the file are last changed.
$ ls -lrt | grep ^-
EXPERIMENT NO: 6
NAME OF THE EXPERIMENT: Write a shell script to list all of the directory files in a
directory.
AIM: In this shell script we will supply the directory name as an argument then our
program will return us the list of directories in the given directory
ALGORITHM:
SOURCE CODE
EXPECTED OUTPUT:
Viva Questions
NAME OF THE EXPERIMENT: Write a shell script to find factorial of a given number.
3.AIM: By using bash shell we will write a shell script to find the factorial of a given
number.
ALGORITHM
SOURCE CODE:
EXPECTED OUTPUT
Viva Questions
NAME OF THE EXPERIMENT: Write awk script to count no of lines in a file that do not
contain vowels.
AIM: By using bash shell we will write awk script to count the number of lines that do not
contain vowels
ALGORITHM :
5. SOURCE CODE:
awk ‘ $0 ~/aeiou/ ,print $0}’ file1
6. EXPECTED OUTPUT:
$vi file1
pvwxyz sssx
hhhh kkkk
zzzzz bbbbb
$ awk –f prg.awk file1
3
Viva Questions
NAME OF THE EXPERIMENT: Write awk script to count no of characters, words, lines in
a given file.
AIM: By using bash shell we will write awk script to count the number of lines, words,
characters in a given file.
ALGORITHM
SOURCE CODE:
EXPECTED OUTPUT
vi filename.txt
hai how r u
file thank u
bye
3 28 8
VIVA QUESTIONS:
1. How to find the last modified file or the newest file in a directory?
$ ls -lrt | grep ^- | awk 'END{print $NF}'
2. How to access the 10th command line argument in a shell script in Linux?
$1 for 1st argument, $2 for 2nd, etc... For 10th argument, ${10}, for 11th, ${11} and so on.
3. How to find the sum of all numbers in a file in Linux?$ awk '{x+=$0}END{print x}'
file
4. How to delete a file which has some hidden characters in the file name?
Since the rm command may not be able to delete it, the easiest way to delete a file with
some hidden characters in its name is to delete it with the find command using the inode
number of the file.
$ ls –li
EXPERIMENT NO: 10
NAME OF THE EXPERIMENT: Write C program that makes a copy of a file using
standard I/O and System calls.
AIM: By using bash shell we will write awk script to count the number of lines, words,
characters in a given file.
ALGORITHM :
SOURCE CODE:
#include "syscall.h"
#include "stdio.h"
#include "stdlib.h"
#define BUFSIZE 1024
char buf[BUFSIZE];
int main(int argc, char** argv) {
int src, dst, amount;
if (argc!=3) {
printf("Usage: cp <src> <dst>\n");
return 1;
}
src = open(argv[1]);
if (src==-1) {
printf("Unable to open %s\n", argv[1]);
return 1;
}
creat(argv[2]);
dst = open(argv[2]);
if (dst==-1) {
printf("Unable to create %s\n", argv[2]);
return 1;
}
while ((amount = read(src, buf, BUFSIZE))>0) {
write(dst, buf, amount);
}
close(src);
close(dst);
return 0;
}
EXPECTED OUTPUT:
Viva Questions
1. Using the grep command, how can you display or print the entire file contents?
2. What is the difference between a local variable and environment variable in Linux?
A local variable is the one in which the scope of the variable is only in the shell in
which it is defined. An environment variable has scope in all the shells invoked by the shell
in which it is defined.