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

LINUX PROGRAMMING LAB MANUAL

The document outlines several shell scripting exercises for a Linux programming course, including scripts for counting word occurrences in files, listing directory files, and calculating the factorial of a number. It also provides theoretical explanations of commands like 'tr', 'grep', and 'egrep'. Additionally, there are viva questions related to shell scripting concepts and commands.

Uploaded by

e0323040
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

LINUX PROGRAMMING LAB MANUAL

The document outlines several shell scripting exercises for a Linux programming course, including scripts for counting word occurrences in files, listing directory files, and calculating the factorial of a number. It also provides theoretical explanations of commands like 'tr', 'grep', and 'egrep'. Additionally, there are viva questions related to shell scripting concepts and commands.

Uploaded by

e0323040
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Science and Engineering Linux Programming

WEEK3
Programs:
AIM: 5. Write a Shell script that accepts a list of file names as its arguments, counts and
reports the occurrence of each word that is present in the first argument file on other argument
files.?
THEORY:
Tr: The Tr command can be used for to translate the characters that means the given string is
replaced with the replacement string.
Syntax: Tr [original string] [replacement string]
Grep: The Grep command can be used for to search the regular pattern on given file.
Syntax: Grep [ pattern ] file name

if [ $# -eq 0 ]
then
echo "no arguments"
else
tr " " "
" < $1 > temp
shift
for i in $*
do
tr " " "
" < $i > temp1
y=`wc -l < temp`
j=1
while [ $j -le $y ]
do
x=`head -n $j temp | tail -1`
c=`grep -c "$x" temp1`

Narsimha Reddy Engineering College Page -12


Computer Science and Engineering Linux Programming

echo $x $c
j=`expr $j 1`
done
done
fi

Narsimha Reddy Engineering College Page -13


Computer Science and Engineering Linux Programming

6.Write a Shell script to list all of the directory files in a directory.


Program:

# !/bin/bash
echo "enter directory name"
read dir
if[ -d $dir]
then
echo "list of files in the directory"
ls –l $dir|egrep „^d‟
else
echo "enter proper directory name"
fi

Narsimha Reddy Engineering College Page -14


Computer Science and Engineering Linux Programming

7.Write a Shell script to find factorial of a given integer?

# !/bin/bash
echo "enter a number"
read num
fact=1
while [ $num -ge 1 ]
do
fact=`expr $fact\* $num`
num=‟expr $num – 1‟
done
echo "factorial of $n is $fact"

VIVA QUESTIONS
1.why we are using grep command?
2.what is an shell script?
3.why we are using egrep command?
4.what is the purpose of fgrep?
5.write a syntax of grep?
6.what is an shell variable?

Narsimha Reddy Engineering College Page -15

You might also like