unix lab manual pcs-402

28
1 UNIX LAB MANUAL PCS-402 B.C.T. KUMAON ENGINEERING COLLEGE DWARAHAT Prepared by:- Mr. Rajinder Sanwal

Upload: rajinder-sanwal

Post on 27-Oct-2015

62 views

Category:

Documents


8 download

DESCRIPTION

Unix lab manual

TRANSCRIPT

Page 1: Unix Lab Manual Pcs-402

1

UNIX LAB MANUAL

PCS-402

B.C.T. KUMAON ENGINEERING COLLEGE

DWARAHAT

Prepared by:- Mr. Rajinder Sanwal

Page 2: Unix Lab Manual Pcs-402

2

List Of Experiments

1. Use Vi editor to create a file called myfile.txt which contain some text.

Correct typing errors during creation, Save the file & Logout of the file

2.Open the file created in Exp 1, Add, Change, delete & Save the changes

3. Use the cat command to create a file containing the following data. Call it

mutable use tabs to separate the fields 1425 ravi 15.65, 4320 ramu 26.27,

6830 sita 36.15, 1450 raju 21.86

4. Use the cat command to display the file, my table, use vi command to correct

any errors in the file, my table, use the sort command to sort the file my table

according to the first field. Call the sorted file my table(same name) & print the

file my table.

5. Use the cut & paste commands to swap fields 2 and 3 my table. Call it

mytable(same name) & print the new file, my table

6. 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.

7. Develop an interactive grep script that asks for a word and a file name and

then tells how many lines contain that word.

8. Write A shell script that takes a command –line argument and reports on

whether it is directry ,a file,or something else

9. Write a shell script that accepts one or more file name as a arguments and

converts all of them to uppercase,provided they exits in the current directory

10. Write a shell script to know the login time for a particular user.

11. Write a shell script that determines the period for which a specified user is

working on the system

Page 3: Unix Lab Manual Pcs-402

3

Experiment no. 1:

Use vi editor to create a file called myfile.txt which contains some text.

Correct typing errors during creation, save the file and logout of the

file.

Description:

The vistands for Visual Editor which is one of the three editors available in UNIX. The vi program has three modes of operation:(a)command mode (b) insert mode (c) ex command mode

For creating any file you need to invoke vi. To invoke vi type vi

and name of the file you want to create. The syntax is:

$ vi filename

For writing text in the file you need to enter the insert mode. For

this press the i key to enter the insert mode of vi (do not press

Enter key).

For saving and logging out from the file press esc key which

returns you to the Command Mode and then press the command

:wq[Enter]

For viewing the contents of the file the syntax followed is

$cat filename

Page 4: Unix Lab Manual Pcs-402

4

Creating a file (myfile.txt) and use of :x command to save and quit from

insertion mode

Viewing content of file by using cat command

Page 5: Unix Lab Manual Pcs-402

5

Experiment no. 2:

Open the file created in exp.1, add, change, delete and save the changes.

Description:

For opening the file in vi editor we use the following syntax

$ vi filename

For adding text we use the append command by pressing Ctrl key

and A(it takes the cursor to the end of line from current cursor

position), a(appends text after the cursor),I(inserts text at the

beginning of the current line).

For deleting the contents, move the cursor to the character that

needs to be deleted, then press nx where n is the number of

character to be deleted next to the current cursor position.

For deleting the entire line we use the dd command in the

command mode.

For saving and logging out from the file press esc key which

returns you to the Command Mode and then press the command

:wq[Enter]

For viewing the contents of the file the syntax followed is

$ cat filename

Page 6: Unix Lab Manual Pcs-402

6

Opening the file created in 1st exp. (myfile.txt)

Adding and Deleting some text from the file using x command

Page 7: Unix Lab Manual Pcs-402

7

Viewing contents by using cat command

Page 8: Unix Lab Manual Pcs-402

8

Experiment no. 3:

Use cat command to create a file containing the following data. Call it

mutable use tabs to separate the fields 1425 ravi 15.65, 4320 ramu

26.27, 6830 sita 36.15, 1450 raju 21.86 .

DESCRIPTION:

cat is one of the most well-known commands of the UNIX system. cat

is a versatile command. It can be used to create, display, concatenate

and append to files. More important, it doesn’t restrict itself to handling

files only; it also acts on a stream. You can supply the input to cat not

only by specifying a filename, but also from the output of another

command.

Syntax for creating a file through ‘cat’ command

$cat >>filename

Syntax for opening a file through ‘cat’ command

$cat filename

Page 9: Unix Lab Manual Pcs-402

9

Creating a new file mytable using cat command

Opening the created file using cat

Page 10: Unix Lab Manual Pcs-402

10

Experiment no. 4:

Use the cat command to display the file, my table, use vi command to correct any errors in the file, my table, use the sort command to sort the file my table according to the first field. Call the sorted file my table (same name) & print the file my table.

DESCRIPTION:

For viewing the contents of the file use the cat

$cat filename

For correcting the errors by using vi command the syntax is

$vifilename

This opens the vi editor and allows the user to correct the errors

using the vi commands.

The sorting the file by using the sort command and saving the

sorted file the syntax is

$sort -0file2 file1

This command sorts the file1 and saves it to the file2.

For viewing the contents of the file use the cat command again.

The syntax is

$cat filename

Page 11: Unix Lab Manual Pcs-402

11

Correcting the errors of file mytable using vi editor

Sorting file mytable using sort command

Page 12: Unix Lab Manual Pcs-402

12

Viewing the sorted file

Page 13: Unix Lab Manual Pcs-402

13

Experiment no. 5:

Q5:- Use the cut & paste commands to swap fields 2 and 3 my table. Call it

mytable(same name) & print the new file, my table.

Creating file mytable and saving and quitting using :x

Page 14: Unix Lab Manual Pcs-402

14

Using cut command to cut fields 2&3

Page 15: Unix Lab Manual Pcs-402

15

Saving the cut fields in file mytable using paste command

viewing contents of file mytable

Page 16: Unix Lab Manual Pcs-402

16

EXPERIMENT NO -6

Q6:- 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.

THEORY

The UNIX system maintains an internal clock meant to run perpetually. You can

display the current date with the date command, which shows the date and time

to the nearest second.

UNIX maintains an account of all the users who are logged on to the system

using who command. This command does have a header option (-H). This

option prints the column headers, and when combined with the –u option,

provides a more detailed list.

DESCRIPTION

To display the current date and users who are logged in use the following

syntax:

$date; who

To redirect the output ofwho command use redirection operator with the file

name using the following syntax:

$who -Hu >>filename

To display current date and redirect the output of who command in

myfile2.

Page 17: Unix Lab Manual Pcs-402

17

Page 18: Unix Lab Manual Pcs-402

18

Using date and who command

Viewing contents by using more command

Page 19: Unix Lab Manual Pcs-402

19

Experiment no. 7:

Q7:- Develop an interactive grep script that asks for a word and a file

name and then tells how many lines contain that word.

Description:

• For writing a script firstly create a .scr file using the vi editor,

the syntax is

$vi filename.scr

• Provide the path of the shell in the insertion mode of the vi editor

$#!/usr/bin/sh

• For searching the occurrence of a pattern we use the grep

command. The syntax is

grep –c $word $filename

• For executing the shell script the syntax of command is

$sh filename

Page 20: Unix Lab Manual Pcs-402

20

Shell script for the experiment

Viewing contents of file abc

Page 21: Unix Lab Manual Pcs-402

21

Output of the shell script after execution

Page 22: Unix Lab Manual Pcs-402

22

Experiment No. 8

Q8. Write A shell script that takes a command –line argument and reports on

whether it is directry ,a file,or something else.

Code:-

#!/usr/bin/bash

if [ $# -eq 0 ] ; then

echo " you have not entered filename as command line argument"

exit 1

fi

if [ ! -e $1 ]; then

echo "File $1 does not exists... enter some other file name NEXT TIME"

exit 1

fi

if [ -d $1 ] ; then

echo "$1 is a directory"

else

echo "$1 is a simple file"

fi

Page 23: Unix Lab Manual Pcs-402

23

OUTPUT

Page 24: Unix Lab Manual Pcs-402

24

Experiment No 9

Q9:-Write a shell script that accepts one or more file name as a arguments and

converts all of them to uppercase,provided they exits in the current directory.

Code:-

#!/usr/bin/bash

if [ $# -eq 0 ] ; then

echo " you have not entered filename as command line argument"

exit 1

fi

if [ ! -e $1 ]; then

echo "File $1 does not exists... enter some other file name NEXT TIME"

exit 1

fi

filename=$1

tr '[a-z' '[A-Z' <$filename

Page 25: Unix Lab Manual Pcs-402

25

Output:-

Page 26: Unix Lab Manual Pcs-402

26

Experiment No 10

10. Write a shell script to know the login time for a particular user

Code:-

#!/usr/bin/sh

echo "enter the user name whose login time you want to find out"

read name

who | cut -d " " -f1 > check

if grep "$name" check

then

echo " the user you entered is currently logged in "

else

echo " the user you entered is not currently logged in"

exit 1

fi

Page 27: Unix Lab Manual Pcs-402

27

Output

Page 28: Unix Lab Manual Pcs-402

28

Experiment no 11

Q11:- Write a shell script that determines the period for which a specified user

is working on the system.

Code:-

#!/usr/bin/sh

t1=` who | grep "$1" | tr -s " " | cut -d " " -f 5 | cut -d ":" -f 1 `

t2=` who | grep "$1" | tr -s " " | cut -d " " -f 5 | cut -d ":" -f 2 `

echo "$tr1"

echo "$tr2"

t1=`expr $t1 \* 60 `

min1=`expr $t1 + $t2`

d1=`date +%H`

d2=`date +%M`

d1=`expr $d1 \* 60`

min2=`expr $d1 + $d2`

sub=`expr $min2 - $min1`

p=`expr $min2 - $min1`

p=`expr $p / 60`

p1=`expr $min2 - $min1`

p1=`expr $p1 % 60`

echo " The user $1 has been working since : $pr Hrs $pr1 minutes "