UTP Lab
UTP Lab
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
RAJEEV GANDHI MEMORIAL COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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
3
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester
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)
4
UNIX TOOLS AND PROGRAMMING LAB III – B.Tech I- Semester
Eg: $date
Wed Mar 6 17:56:52 PST 2002
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.
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
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.
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
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
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.
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.
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
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.
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
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.
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
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.
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.
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
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.
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
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.
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
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.
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.