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

Linux Lab

This document contains descriptions of 16 programming tasks or exercises involving shell scripting and C programming in Linux. Some of the tasks include developing scripts to search files for words, check file types, convert filenames to uppercase, perform string operations, calculate gross salary, raise a number to a power, compare file contents, count word occurrences in a file, and get file permissions. Other tasks involve using system calls like fork, read/write to files, and stat to get file metadata.

Uploaded by

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

Linux Lab

This document contains descriptions of 16 programming tasks or exercises involving shell scripting and C programming in Linux. Some of the tasks include developing scripts to search files for words, check file types, convert filenames to uppercase, perform string operations, calculate gross salary, raise a number to a power, compare file contents, count word occurrences in a file, and get file permissions. Other tasks involve using system calls like fork, read/write to files, and stat to get file metadata.

Uploaded by

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

1 Develop an interactive grep script that asks for a word and a file name and then tells how

many lines contain that word.


Cat>filename
Day by day week by end
Week by week month by end
Month by month year by end
But friendship is never end

$ vi grep.sh
echo "Enter the pattern to be searched: "
read pattern
echo "Enter the file to be used: "
read filename
echo "Searching for $pattern from file $filename"
echo "The selected records are: "
grep "$pattern" $filename
echo "The no.of lines contains the word( $pattern ) :"
grep -c "$pattern" $filename

2 Write a shell script that takes a command –line argument and reports on whether it is
directory, a file, or something else.
Solution:
echo " enter file"
readstr
if test -f $str
then echo "file exists n it is an ordinary file"
elif test -d $str
then echo "directory file"
else
echo "not exists"
fi
if test -c $str
then echo "character device files"
fi

3.Write a shell script that accepts one or more file name as arguments and converts all of
them to uppercase, provided they exist in the current directory.
# get filename
echo -n "Enter File Name : "
readfileName
# make sure file exits for reading
if [ ! -f $fileName ]
then
echo "Filename $fileName does not exists"
exit 1
else
# convert uppercase to lowercase using tr command
tr '[a-z]' '[A-Z]' < $fileName
fi
4) Write a shell script to perform the following string operations:
i) To extract a sub-string from a given string.
ii) To find the length of a given string

Solution:
i) Extract a Substring from a given String
Bash provides a way to extract a substring from a string.

${string:position}

ii) Extract substring from $string at $position

${string:position:length}

Extract substring of length $LEN from $STRING starting after position $POS. Note that first
position is 0.

STRING="this is a string"
POS=1
LEN=3
echo${STRING:$POS:$LEN}# his
echo ${#STRING} # 16 # 1234567890123456
STRING="this is a string"
echo${#STRING}# 16
5)Write a shell script that deletes all lines containing a specified word in one or more files
supplied as arguments to it.
## for this program we have to create one or more files (optional),
## I am creating two files names are del ,dell.

[root@localhost ~]# vi del


unix is os
dos is also os
here using unix
unix is powerful os
~
[root@localhost ~]# vi dell
windowsnt is also os
there are some difference between unix and windowsnt
butunix is great among all os
## after creation two files now we have to write sed script file name is del.sed using vi editor.
[root@localhost ~]# videl.sed
{
/os/d
}
6)Write a shell script that computes the gross salary of a employee according to the
following rules:
i) If basic salary is < 1500 then HRA =10% of the basic and DA =90% of the basic.
ii) If basic salary is >=1500 then HRA =Rs500 and DA=98% of the basic
The basic salary is entered interactively through the key board.
Solution:
echo "enter the basic salary:"
readbsal
if [ $bsal -lt 1500 ]
then
gsal=$((bsal+((bsal/100)*10)+(bsal/100)*90))
echo "The gross salary : $gsal"
fi
if [ $bsal -ge 1500 ]
then
gsal=$(((bsal+500)+(bsal/100)*98))
echo "the gross salary : $gsal"
fi
7)Write a shell script that accepts two integers as its arguments and compute the value of
first
number raised to the power of the second number.
Solution:
echo "Enter the integer value :"
read int1
echo "Enter the power of that integer:"
read int2
pv=$int1
i=1
while [ $i -lt $int2 ]
do
pv=`expr $pv \* $int1`
i=`expr $i + 1 `
done
echo "The value of first number to the power of the second number :"
echo "$pv
8)Write a shell script which receives two file names as arguments. It should check whether
thetwo file contents are same or not. If they are same then second file should be deleted.
Solution:
echo "enter the first file name"
read file1
echo "enter the second file name"
read file2
cmp $file1 $file2 &&rm $file2
if [ -e $file1 ]
then
if [ ! -e $file2 ]
then
echo "the two files contents are same. so $file2 is deleted"
else
echo "the two file contents are not same and $file2 not deleted"
fi
else
echo "$file1 is not existed"
fi
9)Develop an interactive script that ask for a word and a file name and then tells how many
times that word occurred in the file.
echo "Enter the word to be searched"
read word
echo "Enter the filename to be used"
readfname
echo "the no. of times the wored ['$word'] occured in the file"
grep -o $word $fname|wc -l
echo "the no of lines that contains ['$word'] is"
grep -c $word $flname
10) Write a shell script that displays a list of all the files in the current directory to which
the user has read, write and execute permissions.
a) Solution:
echo "The List of File Names in the current directory"
echo "which have Read, Write and Execute Permissions"
for file in *
do
if [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then
ls -l $file
fi
fi
done
11)Write a C program that takes one or more file or directory names as command line
input and reports the following information on the file:
i) Size
ii) inode
(Note: Use stat/fstat system calls)

Solution:
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(intargc, char *argv[])
{
int fd;
struct stat X;
fd=open("abc.txt",O_RDONLY);
if((fstat(fd,&X) <0)
{
printf("file not found")
}
else printf("size %ld inode %ld ",X.st_size,X.st_ino)
}
12)Use of fork system call
13) write 10 basic commands in Linux with syntax
14)Read content of file and print on monitor using read/write system call
15) Shell script to check no is prime or not
16)shell script to check armstrong number

You might also like