0% found this document useful (0 votes)
48 views38 pages

UTP Lab

Lectures Notes

Uploaded by

v_u_ingle2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views38 pages

UTP Lab

Lectures Notes

Uploaded by

v_u_ingle2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

RAJEEV GANDHI MEMORIAL COLLEGE

OF ENGINEERING & TECHNOLOGY


(Autonomous)

III B.tech I SEMESTER

UNIX TOOLS AND PROGRAMMING LAB

DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
RAJEEV GANDHI MEMORIAL COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

VISION OF THE DEPARTMENT

 To empower students with cutting edge technologies in computer science


and engineering

 To train the students as entrepreneurs in computer science and


engineering to address the needs of the society

 To develop smart applications to disseminate information to rural people

MISSION OF THE DEPARTMENT

 To become the best computer science and engineering department in the


region offering undergraduate, post graduate and research programs in
collaboration with industry

 To incubate, apply and spread innovative ideas by collaborating with


relevant industries and R & D labs through focused research groups.

 To provide exposure to the students in the latest tools and technologies to


develop smart applications for the society
RGM-R-2015
RAJEEV GANDHI MEMORIAL COLLEGE OF ENGG.& TECH., NANDYAL-518 501
AUTONOMOUS
COMPUTER SCIENCE AND ENGINEERING
III B. Tech. I- Sem (CSE) P C
3 2
(A0582155) UNIX TOOLS AND PROGRAMMING LAB
OBJECTIVES:
 To teach students various unix utilities and shell scripting.
OUTCOMES
 Understand the basic UNIX process structure and the UNIX file system.
 Understand the roles of unix developers / systems programmers
 Good knowledge of simple UNIX filters.
 Familiar with pipes and redirection, imagine the UNIX environment.
 Practice various commands related to Signals, filter parameters and options,
 Differentiate shell scripting and commands practice with various options.
CO-PO MAPPING:
CO/PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO 1 PSO 2 PSO 3
CO1 2 1 3 1 2
CO2 1 3 2 1 2
CO3 1 2 1 2 3
CO4 1 2 1 1
CO5 2 1 3 1
CO6 1 2 2 2
Recommended Systems/Software Requirements:
 Intel based desktop PC with minimum of 166 MHZ or faster processor with at least 64 MB RAM and
100 MB free disk space LAN Connected
 Any flavour of Unix / Linux
Week1
SESSION-1
a) Log into the system.
b) Use vi editor to create a file called myfile.txt which contains some text.
c) Correct typing errors during creation.
d) Save the file e)logout of the system
SESSION-2
a) Log into the system
b) Open the file created in session 1
c) Add some text
d) Change some text
e) Delete some text.
f) Save the Changes
g) Logout of the system
Week2
a) Log into the system
b) Use the cat command to create a file containing the following data. Call it mytable use tabs to separate
the fields.
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
c) Use the cat command to display the file, mytable.
d) Use the vi command to correct any errors in the file, mytable.
e) Use the sort command to sort the file mytable according to the first field. Call the sorted file my table
(same name)
f) Print the file mytable
g) Use the cut and paste commands to swap fields 2 and 3 of mytable. Call it my table (same name)
h) Print the new file, mytable
i) Logout of the system.
Week3
a) Login to the system
b) Use the appropriate command to determine your login shell
c) Use the /etc/passwd file to verify the result of step b.

Detailed Syllabus 93
RGM-R-2015
RAJEEV GANDHI MEMORIAL COLLEGE OF ENGG.& TECH., NANDYAL-518 501
AUTONOMOUS
COMPUTER SCIENCE AND ENGINEERING
d) Use the who command and redirect the result to a file called myfile1. Use the more command to see the
contents of myfile1.
e) Use the date and who commands in sequence (in one line) such that the output of date will display on
the screen and the output of who will be redirected to a file called myfile2. Use the more command to
check the contents of myfile2.
Week4
a) Develop an interactive grep script that asks for a word and a file name and then tells how many lines
contain that word.
b) Repeat
c) Part using awk.
Week5
a) Write a shell script that takes a command –line argument and reports on whether it is directory, a file,
or something else.
b) 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.
Week6
a) Write a shell script that accepts a file name starting and ending line numbers as arguments and displays
all the lines between the given line numbers.
b) Write a shell script that deletes all lines containing a specified word in one or more files supplied as
arguments to it.
Week7
a) 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.
b) Write a shell script that accepts two integers as its arguments and computes the value of first number
raised to the power of the second number.
Week8
a) Write an interactive file-handling shell program. Let it offer the user the choice of copying, removing,
renaming, or linking files. Once the user has made a choice, have the program ask the user for the
necessary information, such as the file name, new name and so on.
b) Write shell script that takes a login name as command – line argument and reports when that person
logs in.
c) Write a shell script which receives two file names as arguments. It should check whether the two file
contents are same or not. If they are same then second file should be deleted.
Week9
a) 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.
b) 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.
c) 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.
Week10
a) Write a shell script to generate multiplication table.
b) Write a shell script to print sum of individual digits of a number.
Week11
Write C programs that simulate the following unix commands:
a) mv b) cp (Use system calls)
Week12
Write a C program that simulates ls Command(Use system calls / directory API)
REFERENCES:
1. Introduction to UNIX & SHELL programming, M.G. Venkatesh Murthy, Pearson Education.
2. Unix concepts and applications, Fourth Edition, Sumitabha Das, TMH.
3. Unix for programmers and users, 3rd edition, Gaham Glass & K. Ables, pearson education.
4. Unix and shell Programming –A text book, B.A. Forouzan & R.F. Giberg, Thomson.
5. Beginning shell scripting, E. Foster – Johnson & other, Wile Y- India.

Detailed Syllabus 94
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 1 Demonstrate Week – 1 questions using VI-Editor with suitable


examples.
Aim: Implementation of Week – 1 experiment using VI-Editor with
suitable examples.
Procedure:
WEEK1 :: Session 1
1) Log in to the system
Sol: $ login: <user name>
$ password: ******
2) Use Vi editor to create a file called myfile.txt which contain some text.
Sol: $ vi myfile
~ Unix is Case Sensitive
~ Never leave the Computer without logging out when you are working in a time sharing
or network environments.
Type <Esc>
: wq myfile
3) Correct typing errors during creation
Sol: $cat myfile
4) Save the file
Sol: : wq myfile
5) Logout of the file
Sol: $exit
Session 2
1. Log into the system
Sol: $ login: <user name>
$ password: ******
2. Open the file created in session 1
Sol: $ vi myfile
~ Unix is Case Sensitive
~ Never leave the Computer without logging out.
~ Shell Programming
3. Add some text
Sol: $ vi myfile
~ Unix is Case Sensitive
~ Never leave the Computer without logging out when you are working in a time sharing or
network environments.
~ Shell Programming
4. Change some text 5 delete some text
Sol: $ vi myfile
~ Unix is Case Sensitive
~ Shell Programming
5. Save the changes
Sol: : wq myfile
6. Logout of the system
Sol: $exit
Result: Week-1 questions are demonstrated with suitable examples successfully.
1
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 2 Demonstrate Week – 2 questions using VI-Editor with suitable


examples.

Aim: Implementation of Week-2 experiment using VI-Editor with suitable


examples.
Procedure:
WEEK2
1. Log into the system
Sol: $ login: <user name>
$ password: ******
2. Use the cat command to create a file containing the following data. Call it mytable use
tabs to separate the fields
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
Sol: $ cat > mytable
1425 <tab> Ravi <tab> 15.65 <tab>
4320 <tab> ramu <tab> 26.27 <tab>
6830 <tab> sita <tab> 36.15 <tab>
1450 <tab> Raju <tab> 21.86 <tab>
Press Ctrl+D to quit enter
3. Use the cat command to display the file, my table
Sol: $ cat mytable
1425 Ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 Raju 21.86
4. Use the vi command to correct any errors in the file, my table
Sol: $ vi mytable
1425 Ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 Raju 21.86
5. Use the sort command to sort the file my table according to the first field. Call the
sorted file my table(same name)
Sol: $ sort +0 -1 mytable
1425 Ravi 15.65
1450 Raju 21.86
4320 ramu 26.27
6830 sita 36.15
6. print the file my table
Sol: $ lpr mytable
7. Use the cut & paste commands to swap fields 2 and 3 my table. Call it mytable(same
name)
Sol: $ cut –f1 mytable > temp1
2
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

$ cut –f2 mytable > temp2


$ cut –f3 mytable > temp3
$ paste temp1 temp3 temp2 > mytable
8. print the new file, my table
Sol: $ lpr mytable
9. logout of the system
Sol: $exit

Result: Week-2 questions are demonstrated with suitable examples successfully.

3
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 3 Demonstrate Week – 3 questions using VI-Editor with suitable


examples.

Aim: Implementation of Week-3 experiment using VI-Editor with suitable


examples.
Procedure:
WEEK3

1. Log into the system


Sol: $ login: <user name>
$ password: ******

2. Use the appropriate commands to determine ur login shell


Sol: $echo “the login shell is $SHELL”

3. Use the /etc/passwd file to verify the result of step b.


Sol: $cat /etc/passwd

4. Use the who command redirect the result to a file called myfile1.Use the more
command to see the contents of myfile1.
Sol: $who > myfile1
$more myfile1

5. Use the date and who commands in sequence ?(in one line) such that the output of date
will display on the screen and the output of who will be redirected to a file called my
file2.Use the more command to check the contents of myfile2.
Sol: $date; who>myfile2
$more myfile2

6. write a sed command that deletes the first character in each line in a file
Sol: $cat > sample
my name is Ramu
I am studying B.Tech III year I am learning UNIX
^Z(press ctrl+Z)

$sed "s/.//" sample


7. write a sed command that deletes the character before the last character in each line in
a file.
Sol: $sed "s/.\(.\)$/\1/" sample

Result: Week-3 questions are demonstrated with suitable examples successfully.

4
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 4 Demonstrate Common Commands using VI-Editor with suitable


examples.

Aim: Implementation of Common Commands using VI-Editor with


suitable examples.
Procedure:
1. Date and Time Command: The date command displays the system date and time. If system is local-
it replies the current time. If system is remote, reply will contain the time where system is physically
located.

Eg: $date
Wed Mar 6 17:56:52 PST 2002

If '-u' option is used, the time is GMT.


$date -u
Wed Apr 3 08:24:19 GMT 2002

$ date “+Today’s day name is : %a”


Mon
$ date “+Today’s day name is : %A”
Monday
$ date “+Today’s month name is : %b”
5
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Dec
$ date “+Today’s month name is : %B”
December

2. CALENDER (cal) COMMAND: The calendar command, Cal, displays the calendar for specified
month or for a year. It is an example that has no options but uses arguments. The arguments are optional.
If no arguments are entered, the calendar for the current month is printed.

Eg: $cal
March 2013
S M T W TH F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

6
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

3. Who Command: The command displays all users currently logged into the system. The general format
of the who command is below:
Eg:
$who

$who –u

$who –uH

$who am i
gilberg ttyq5 Mar 15 16:34

4. Change Password (passwd) Command: The password command 'passwd' is used to change your
password. It has no options or attributes but rather does it work through a dialog of questions and
answers. It begins by asking you to enter your old password. After you verify your password, the system
asks you for a new password.

Eg:
$passwd
Changing password for......
Old password:
New password:
Re-enter new password:
5. Print message (echo) Command: The echo command copies the argument back to the terminal.

7
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Eg:
$echo Hello World
Hello World
$echo "Error 205: Invalid total sales"
Error 205: Invalid total sales
6. Online Documentation (man) Command: This is one of the most important UNIX commands. It
displays the online documentation command. This just like a user manual, which consists of
documentation for each and every command, even the man command itself.

Eg: $man cal


cal (1)
NAME
cal print calendar
SYNOPSIS
cal [[month] year]
DESCRIPTION
cal prints a calendar.......
7. Print (lpr) Command: lpr represents "line printer". This utility prints the contents of specified files to
other the default printer or to a specified printer. The multiple advantages here is that, multiple files
can be printed with the same command. If no file is specified, the input comes from standard input
(keyboard) unless it is redirected.

8
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Eg:
$lpr file1
-prints file1 to standard printer
$lpr file1 file2 file3
-prints file1, file2, file3 to standard printer
$lpr -plp0 file1 file2 file3
-prints file1, file2, file3 to lp0 printer

Result: Hence, Common Commands are demonstrated with suitable examples successfully.

9
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 5 Demonstrate Other Useful Commands using VI-Editor with suitable


examples.

Aim: Implementation of Other Useful Commands using VI-Editor with


suitable examples.
Procedure:
1. tty Command: The tty utility is used to show the name of the terminal you are using. UNIX treats
each terminal as a file and the name of your terminal is actually the name of a file. The tty command
shows the name of the terminal.

Eg:
$tty
/dev/ttyq0 -> name of the terminal. In UNIX, the name of the terminal has the prefix tty.
2. CLEAR SCREEN (clear) COMMAND: The clear command clears the screen and puts the
cursor at the top.

Eg:
$clear
3. Set terminal (stty) COMMAND: The set terminal (stty) command sets or unsets selected
terminal input or output options. This command is very useful to reconfigure the terminal,
whenever is not working properly.

WITHOUT OPTION OR ARGUMENT:

If we don't use any option or argument with the stty command, it simply shows some communication
settings such as baud rate.

10
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Eg:
$stty
Speed 9600 baud; -parity hupel clocal
Line=1; intr=^ A; erase=DEL; old-swtch=^@; dsusp=^@; brkint=-inpck icrnl enlar tab 3
SET TERMINAL WITH ARGUMENTS:
Many of the terminal settings should be set only by a super user.
Set Erase and Kill (ek): The ek argument sets the defaults erase (Delete Key ctrl+h) and Kill
(ctrl+c) to their defaults.
Set Terminal to General Configuration (sane): The sane argument sets the terminal configuration to
reasonable settings that can be used with a majority of terminal.
Example: $stty sane
Set Erase Key (erase): We can configure the keyboard to use another key as the Delete Key with
the erase argument.
$stty erase ^e //Hence ctrl+e combination deletes/erases the previous character typed.

To reset this to ctrl + h, we can again make use of ek or the same argument.
Set Kill (kill): The kill key deletes a whole line. By default, it is ctrl+u. We can change it using the
set terminal command.
Eg: $stty kill 9 //In the example, it is not a control key combination, but just the single key 9.

Set Interrupt Key (intr) : The interrupt key interrupts or suspends a command. It can be reset using
the intr argument.
Eg: $stty intr ^ 9

4. RECORD SESSION (script) COMMAND: The script command can be used to record an
interactive session. When you want to start recording, key the command. To record whole session,
including the logout, make it the first command of the session. To stop the recording, key exit.
The session log in a file called typescript. Some of the lengthy output, such as for the who and
is commands has been edited to reduce the number of lines.
Eg: $script
script started, file is typescript
$date
11
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Mon May 28 13:40:59 PDT 2001


$exit
Script done, file is typescript.
 Each script command execution erases the old script file output.
 To append to the file, rather than erase it, we make use of the append option (-a).
Eg: $script -a

5. SYSTEM NAME (uname) COMMAND: Each UNIX system stores data, such as its name,
about itself. To see this data, we can use uname command.

Eg: $uname
IRI*64
$uname -s
IRI *64
$uname -r
6.5
$uname -n
Challenger
$uname -sr
IRI*64 6.5
$uname –a
IRI *64 challenger 6.5 0.4191225 IP19
6. CALCULATOR (bc) COMMAND: The bc command is one of the most interesting. In UNIX;
it returns UNIX into a calculator. The bc command of UNIX turns it into a calculator. It‘s not just
a calculator, but it is similar to C, with a powerful match library.
To start the calculator, we simply key the bc command. To terminate it, we use ctrl+d.
SIMPLE ARITHMETIC:

12
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Simple arithmetic is done on command line, which supports addition (+), Subtraction (-),
Multiplication (*), Divison (/), Modulus (%) and Power (^).

Eg: $bc
12+8
20
45-56
-11
34+34*3
136
Ctrl + d
FLOATING POINT CALCULATIONS: To use floating point arithmetic, we have to specify the
number of decimal points to be used beforehand. This is done using SCALE expression, which sets
the number of digits after the decimal in a floating point number.
Eg: $bc
19/3
6
Scale=2
19/3
6.33
Ctrl+d
ARITHMETIC BASE CALCULATIONS: The bc calculator can be used in binary, decimal,
hexadecimal, octal bases. The base is specified by one of the two expressions: base and obase.
ibase: This expression specifies the base of the input.
Obase: This expression specifies the base of the output. If no base is specified, it is assumed to be
decimal (base 10).
Eg: $bc
Ibase=2
111
7
111*111
49
Ibase=8
10
8
10*11
72
Ctrl+d

Result: Hence, Other Useful Commands are demonstrated with suitable examples successfully.
13
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 6 Demonstrate Vi- Editor Commands using VI-Editor with suitable


examples.

Aim: Implementation of VI-Editor Commands using VI-Editor with


suitable examples.
Procedure:
VI editor:
The VI editor is a screen editor available on most UNIX systems. When you invoke the VI editor, it
copies the contents of a file to a memory space known as a buffer. Once the data have been loaded
into the buffer, the editor presents a screen full of the buffer to the user for editing. If the file does not
exist, an empty buffer is created. Buffer is only a temporary version of the file. When we exit VI, the
buffer is erased.
At exit time we have two choices:
1. We can quit VI without saving the file, which means that the contents of the original file remain
unchanged.
2. We can save the file, which means that the contents of the original file are replaced by the new
version.

MODES

The VI editor uses two basic modes: the command mode and the text mode.

1. Command Mode: When the VI editor is in the command mode, any key that is pressed by the
user is considered a command. Commands are used to move the cursor, to delete or change part
of the text, or to perform many other operations. As soon as the command is entered, it is
executed the return key is not required. On some systems, commands are known as hot keys.
2. Text Mode: When the VI editor is in the text mode, any key is pressed by the user is considered
text. The keyboard acts as a typewriter.
In the text mode, the characters typed by the user, if they are printable characters, are inserted
into the text at the cursor. This means that to add text in a document, we should first place the
cursor at the desired location.
To place the cursor, however, we must be in the command mode. The typical operation,
14
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

therefore, is to place the cursor with a command, switch to the text mode and edits the text, then
switch back to the command mode for the next operation.

15
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Changing Modes: It is clear that we must switch back and forth between VI command and text
modes. To tell VI to do something, it must be in command mode, to edit text; it must be in the text mode.

Summary:
To invoke VI, you type the following command at UNIX prompt:
Eg: $VI filename

 When you invoke VI, you are always in the command mode. During the session, you can
move back and forth between the command mode and the text mode.
 To exit VI, you must be in the command mode.
 There are six commands that take you to the text mode (a, A, i, I, o and O).
 When you are in the text mode, you press the Escape key (esc) to go to the command mode.

Result: Hence, Vi-Editor Commands are executed with suitable examples successfully.

16
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 7 Develop shell scripts for Week-4 questions and execute them in the shell
with suitable examples.

Aim: Implementation of Week-4 questions using VI-Editor with suitable


examples.
Week4
a) Develop an interactive grep script that asks for a word and a file name and then tells how
many lines contain that word.
b) Repeat
c) Part using awk.
Procedure:
a)
echo "Enter a word"
read word
echo "Enter the filename"
read file1
not="$(grep -c $word $file1)"
echo "The word $word repeated $not times in the file $file1"
Execution: Create a sample file like myfile which contains a word like unix
sh week4a.sh
Enter a word
UNIX
Enter the filename
grep1
The word UNIX repeated 3 times in the file grep1

c)
echo Enter a word
read word
echo Enter the filename
read file1
awk "/$word/{ print NR }" $file1
echo "are the line numbers for the word $word in the file $file1"

Execution: Create a sample file like myfile which contains a word like unix

sh week4c.sh
Enter a word
UNIX
Enter the filename
grep1
1
3
4
are the line numbers for the word UNIX in the file grep1
Result: Hence, Week – 4 questions are developed in shell scripts and executed with suitable
examples successfully.
17
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 8 Develop shell scripts for Week-5 questions and execute them in the shell
with suitable examples.

Aim: Implementation of Week-5 questions using VI-Editor with suitable


examples.
Week5:
a) Write a shell script that takes a command-line argument and reports on whether it is directory,
a file or something else.
b) Write a shell script that accepts one or more file name as arguments and converts all of them
to upper case, provided they exists in the current working directory.

Procedure:
a) echo "enter a file name"
read f
if [ -f $f ]
then
echo "$f is a file"
elif [ -d $f ]
then
echo "$f is a directory"
else
echo "Not"
fi

Execution:
1) sh week5a.sh
enter a file name
grep1
grep1 is a file

2) sh week5a.sh
enter a file name
grep1
grep1 is a file

3)sh week5a.sh
enter a file name
file5
Not

b) echo enter the filename


read filename
echo contents of $filename before converting to uppercase
echo ---------------------------------------------
cat $filename
echo contents of $filename after converting to uppercase
echo ---------------------------------------------
18
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

echo "[a-z]" "[A-Z]" < $filename


echo ---------------------------------------------
echo $filename

Execution:

sh week5b.sh
enter the filename
grep1
Contents of grep1 before converting to uppercase
---------------------------------------------
Only one UNIX
DOS only here
Mac os is UNIX
Linux is UNIX
contents of grep1 after converting to uppercase
---------------------------------------------
[a-z] [A-Z]
---------------------------------------------
grep1

Result: Hence, Week – 5 questions are developed in shell scripts and executed with suitable
examples successfully.

19
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 9 Develop shell scripts for Week-6 questions and execute them in the shell
with suitable examples.

Aim: Implementation of Week – 6 questions using VI-Editor with suitable


examples.
Week6
a) Write a shell script that accepts a file name starting and ending line numbers as arguments and
displays all the lines between the given line numbers.
b) Write a shell script that deletes all lines containing a specified word in one or more files
supplied as arguments to it.
Procedure:
a)
create a sample (for ex like myfile or mytable etc) in your working directory.
Code:

if [ ! -e $1 ]
then
echo "$1 does not exist"
else
echo " displayed lines from 2 to 3"
head -3 $1 | tail -2
fi
Output:
$sh file.sh mytable
displayed lines from 2 to 3
2
3

b) create a sample file which contains a line UNIX in it in your working directory
code:
for demo in $* ; do
if [ ! -e $demo ]
then
echo "$file doesn‟t exist"
else
egrep -v UNIX $demo > temp
echo "Original File"
cat $demo
echo
echo "Modified file i.e., lines which do not have word UNIX"
cat temp
fi
done

20
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Output:
sh week6b.sh grep1
Original File
Only one UNIX
DOS only here
Mac os is UNIX
Linux is UNIX

Modified file i.e., lines which do not have word UNIX


DOS only here

Result: Hence, Week – 6 questions are developed in shell scripts and executed with suitable
examples successfully.

21
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 10 Develop shell scripts for Week-7 questions and execute them in the
shell with suitable examples.

Aim: Implementation of Week – 7 questions using VI-Editor with suitable


examples.
Week7
a) 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.
b) Write a shell script that accepts two integers as its arguments and computes the value of first
number raised to the power of the second number.
Procedure:
a) echo "Enter ur basic salary "
read sal
if [ $sal -lt 1500 ]
then
da=`expr $sal \* 90 / 100`
hra=`expr $sal \* 20 / 100`
gsal=`expr $sal + $da + $hra`
echo "your Basic Salary $sal "
echo "your Dearness Allowance $da "
echo "your House rent $ha "
echo " ------------"
echo "your gross salary is $gsal"
fi
if [ $sal -gt 1500 ]
then
da=`expr $sal \* 98 / 100`
hra=500
gsal=`expr $sal + $da + $hra`
echo "your Basic Salary $sal "
echo "your Dearness Allowance $da "
echo "your House rent $ha "
echo " ------------"
echo "your gross salary is $gsal"
fi
output:
1) sh week7a.sh
Enter ur basic salary
1400
your Basic Salary 1400
your Dearness Allowance 1260
your House rent
------------
22
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

your gross salary is 2940

2) sh week7a.sh
Enter ur basic salary
1400
your Basic Salary 1400
your Dearness Allowance 1260
your House rent
------------
your gross salary is 2940

b)
a=$1
b=$2
pow=1
count=1
if [ ! $b -eq 0 ]
then
while [ $count -le $b ]
do
pow=`expr $pow \* $a`
count=`expr $count + 1`
done
fi
echo "$a power $b is : $pow"

Output: sh week7b.sh 2 5
2 power 5 is : 32

sh week7b.sh 5 2
5 power 2 is : 25

Result: Hence, Week – 7 questions are developed in shell scripts and executed with suitable
examples successfully.

23
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 11 Develop shell scripts for Week-8 questions and execute them in the
shell with suitable examples.
Aim: Implementation of Week – 8 questions using VI-Editor with suitable
examples.
Week8
a) Write an interactive file-handling shell program. Let it offer the user the choice of copying,
Removing, renaming, or linking files. Once the user has made a choice, have the program ask the
User for the necessary information, such as the file name, new name and so on.
b) Write shell script that takes a login name as command – line argument and reports when that
Person logs in.
c) Write a shell script which receives two file names as arguments. It should check whether the
two file contents are same or not. If they are same then second file should be deleted.
Procedure:
a) ch=1
while [ $ch -eq 1 ]
do
echo "1.copy"
echo "2.remove"
echo "3.rename"
echo "4.linking file"
echo "enter your choice"
read choice
case "$choice" in
1)echo "enter source file"
read source
echo "enter destination file"
read dest
cp $source $dest
echo "source copied successfully to $dest" ;;
2)echo "enter filename to remove"
read file
rm $file
echo "$file is successfully removed" ;;
3)echo "enter filename to remove"
read file
echo "enter new name"
read new
mv $file $new
echo "$file is removed as $new" ;;
4)echo "enter filename"
read file
echo "enter link file"
read lname
ln $file $lname
echo "link is provided between $file and $lname" ;;
*)echo "invalid option"
esac
24
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

echo "do u want to continue \n 1.contine \n 2.stop"


read ch
done
output:
sh week8a.sh
1.copy
2.remove
3.rename
4.linking file
enter your choice
1
enter source file
grep1
enter destination file
grep2
source copied successfully to grep2
do u want to continue
1.contine
2.stop
2
[2]- Done gedit week8a.sh

b) who | grep $1 > temp


echo "$1 logs at "
cat temp

output:
sh week8b.sh student
student logs at
student :0 2017-02-03 10:34 (:0)
student pts/1 2017-02-03 11:48 (:0)

c) cmp -s $1 $2
if [ $? -eq 0 ] #the result of cmp stores in special variable $?
then
echo "two files are identical"
rm $2
echo "second file is deleted"
else
echo "two files are not identical"
fi
output:
1) sh week8c.sh grep1 grep2
two files are identical
second file is deleted
2) sh week8c.sh grep1 temp
two files are not identical
Result: Hence, Week – 8 experiments are executed with suitable examples successfully.
25
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 12 Develop shell scripts for Week-9 questions and execute them in the
shell with suitable examples.

Aim: Implementation of Week – 9 questions using VI-Editor with suitable


examples.
Week9
a) 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.
b) 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.
c) 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.
Procedure:
a) code:
for File in *
do
if [ -r $File -a -w $File -a -x $File ]
then
echo $File
fi
done
output: Execute using the following command
bash ./week9a.sh
DC
DMDW
DMKD
DSthroughC++
Desktop
Documents
Downloads

b) echo "enter the filename "


read fn
echo "enter the word"
read wd
grep -o -w $wd $fn | wc -w

output:
sh week9b.sh
enter the filename
myfile
enter the word
unix
3
26
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

c) i) #!/bin/bash
echo " enter a string"
read str
echo enter the starting position and no of characters to extract
read start nc
test1=${str:$start:$nc}
echo $test1
output:
$ bash ./week9ci.sh
enter a string
welcome to the land of unix
enter the starting position and no of characters to extract
07
welcome

ii) #!/bin/bash
echo " enter a string"
read str
echo "the length of the given string '$str' is ${#str} characters"
output:
bash ./week9cii.sh
enter a string
welcome to the land of unix
the length of the given string 'welcome to the land of unix' is 27 characters

Result: Hence, Week – 9 questions are developed in shell scripts and executed with suitable
examples successfully.

27
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 13 Develop shell scripts for Week-10 questions and execute them in the
shell with suitable examples.

Aim: Implementation of Week – 10 questions using VI-Editor with suitable


examples.
Week10
a) Write a shell script to generate multiplication table.
b) Write a shell script to print sum of individual digits of a number.
Procedure:
a) echo "Enter a Number"
read n
echo "Enter Range"
read r
i=0
while [ $i -le $r ]
do
echo " $n x $i = `expr $n \* $i`"
i=`expr $i + 1`
done

output: sh week10a.sh
Enter a Number
5
Enter Range
10
5x0=0
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

b) echo "Enter number : "


read n
sd=0
sum=0
# use while loop to caclulate the sum of all digits
while [ $n -gt 0 ]
do
28
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

sd=$(( $n % 10 )) # get Remainder


n=$(( $n / 10 )) # get next digit
sum=$(( $sum + $sd )) # calculate sum of digit
done
echo "Sum of all digit is $sum"

output: sh week10b.sh
Enter number:
544
Sum of all digit is 13

Result: Hence, Week – 10 questions are developed in shell scripts and executed with
suitable examples successfully.

29
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 14 Develop shell scripts for Week-11 questions and execute them in the
shell with suitable examples.

Aim: Implementation of Week – 11 questions using VI-Editor with suitable


examples.
Week 11
Write C programs that simulate the following unix commands:
a) mv
b) cp (Use system calls)

Procedure:
a)
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
int main(int argc, char **argv)
{
if(argc>3 || argc<3)
{
printf("Please Provide two arugments \n");
}
else{
int fd1,fd2;
int n,count=0;
if(access(argv[1],F_OK)<0)
{
printf("%s not found \n ",argv[1]);
}
if(rename(argv[1],argv[2])==0)
printf(" %s is moved or renamed to %s \n successfully \n",argv[1],argv[2]);
return (0);
}
}
Execution:
g++ week11a.c
$ ./a.out file5 file8
file5 is moved or renamed to file8 successfully

b)
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
30
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

int f1, f2;


char buff[50];
long int n;
if(((f1 = open(argv[1], O_RDONLY)) == -1 || ((f2=open(argv[2], O_CREAT |
O_WRONLY | O_TRUNC, 0700))== 1)))
{
perror("problem in file");
exit(1);
}
while((n=read(f1, buff, 50))>0)
if(write(f2, buff, n)!=n)
{
perror("problem in writing");
exit(3);
}
if(n==-1)
{
perror("problem in reading");
exit(2);
}
close(f2);
exit(0);
}
Execution:
g++ week11b.c
./a.out grep1 grep2
cat grep2
Only one UNIX
DOS only here
Mac os is UNIX
Linux is UNIX
raja@raja-Opti

Result: Hence, Week – 11 questions are developed in shell scripts and executed with
suitable examples successfully.

31
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester

Experiment No : 15 Develop shell scripts for Week-12 questions and execute them in the
shell with suitable examples.

Aim: Implementation of Week – 12 questions using VI-Editor with suitable


examples.
Week 12
Write a C program that simulates ls Command (Use system calls / directory API)

Procedure:
#include<stdio.h>
#include<dirent.h>
#include<stdlib.h>
int main()
{
char dirname[10];
DIR*p;
struct dirent *d;
printf("Enter directory name\n");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find directory");
exit(-1);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
return 0;
}
Execution:
$g++ week12.c
$./a.out
Enter directory name
UNIX
low
wc.sh
unix mid 1
fact.sh~
loop1.sh
file1
...

Result: Hence, Week – 12 questions are developed in shell scripts and executed with
suitable examples successfully.
32
RAJEEV GANDHI MEMORIAL COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Evaluation Procedure for Internal Laboratory Examinations:

1. Of the 25 marks for internal, 10 marks will be awarded for day-to-day work and 10 marks
to be awarded for the Record work and 5 marks to be awarded by conducting an internal
laboratory test.
2. Concerned Teachers have to do necessary corrections with explanations.
3. Concerned Lab teachers should enter marks in index page.
4. Internal exam will be conducted by two Staff members.

Dr.K. Subba Reddy


Professor & Head Dept. of CSE.
RAJEEV GANDHI MEMORIAL COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Evaluation Procedure for External Laboratory Examinations:

1. For Practical subjects there is a continuous evaluation during the semester for 25 Sessional
marks and 50 end examination marks.
2. The end examination shall be conducted by the teacher concerned (Internal Examiner) and
another External Examiner, recommended by Head of the Department with the approval of
principal.

Evaluation procedure for external lab examination:


1. Procedure for the program ---------- 20M
2. Execution of the program ---------- 15M
3. Viva voce ---------- 15M
----------------------------
Total 50M
-----------------------------

Dr.K. Subba Reddy


Professor & Head Dept. of CSE.

You might also like