Anda di halaman 1dari 28

UNIX LAB MANUAL

PCS-402

B.C.T. KUMAON ENGINEERING COLLEGE DWARAHAT

Prepared by:Mr. Rajinder Sanwal


1

List Of Experiments

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

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

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

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

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

Adding and Deleting some text from the file using x command

Viewing contents by using cat command

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 doesnt 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

Creating a new file mytable using cat command

Opening the created file using cat

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

10

Correcting the errors of file mytable using vi editor

Sorting file mytable using sort command

11

Viewing the sorted file

12

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

13

Using cut command to cut fields 2&3

14

Saving the cut fields in file mytable using paste command

viewing contents of file mytable

15

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.

16

17

Using date and who command

Viewing contents by using more command

18

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

19

Shell script for the experiment

Viewing contents of file abc

20

Output of the shell script after execution

21

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

22

OUTPUT

23

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

24

Output:-

25

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

26

Output

27

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 "

28

Anda mungkin juga menyukai