Unix Lab Handbook
Unix Lab Handbook
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Page 1 of 13
SESSION-2
1. Create a file ADDRESS and store the names of your friends, their addresses and phone numbers.
Each line should contain a friend's detail.
2. Display the number of lines, words and characters in the file.
3. Display only the number of lines in the file.
4. View the file one page at a time.
5. Create a directory called Friends and copy this file into the directory.
6. Display a banner that reads Welcome Friends.
7. Create 2 files with the names of your favourite cricketers and try out cmp, comm and diff on them.
8. Display the time when you logged on to the system.
9. Display the names of all those who are logged on to the system and store the result in a file
WHOLIST.
10. View the calendar for the previous , current and next months of the current year with a single
command.
11. Display the date in the format DATE IS dd-mmm-yyyy ( Ex. DATE IS 12-dec-2000 )
12. Store the details of all those logged on to the same file WHOLIST with the existing details.
13. Use man and check out the other options with date.
14. Display the number of files present in your current directory
15. Display the listing of all filenames starting with Z followed by any digit and ending with S.There can
be any number of characters between the digit and S.
Page 2 of 13
SESSION-3
Create a file ITEMS having the following data. Fields are item code-description-rate-QOH-order
placed (n for no and y for yes)
i006-rexin-100-100-y
i002-screws-12-23-n
i001-bolts-123-120-n
i005-rubber-10-10-y
i003-plastic-10-100-n
i004-nuts-200-100-y
i002-screws-12-23-n
i001-bolt-123-120-n
1. Display items for which order is not placed.
2. Display the description and order placed fields for all the items.
3. Display the description and order placed fields for items for which order is placed.
4. Arrange the file in the order of description.
5. Display duplicate lines if any in the file ITEMS
6. Display lines that contain only 10 in them
7. Display all the items with item number displayed with only one zero (ex. i001 as i01)
8. Display all lines where QOH is less than 50
9. Display the last 2 lines of the file ITEMS
10. Sort the file in the order of rate and store it in a file RATE
11. Change the field seperator from - to : in the file RATE
12. Display rate and description of item (in that order ) from file RATE
13. Display the count of lines that have screws in them.
14. Display the lines that donot contain bolts in them.
Create a file called STORES which has the following data :
i006:ammonia:100:100:123:n
i004:rexin:23:12:12:y
i002:screws:200:100:175:y
i001:nuts:10:100:175:n
i003:bolts:123:120:120:n
i005:rubber:10:10:10:y
i002:screws:200:100:175:y
i007:bolt:123:120:120:n
The fields are item number, description, QOH, ROL, ROQ, status(y of those items for which an order has
been placed. N denotes that no order has been placed for that item.)
1. Arrange the file in the order of item number.
2. Display the item number, description and the reorder quantity.
3. Display the item number, description and the reorder quantity only for those items for which an
order is placed.
4. 4. Display only the description (in the ascending alphabetical order) and store the sorted result in
5. a file called sorted.
6. Sort the file in the order of QOH and display item number and QOH in that order.
7. Display the duplicate lines in the files.
Page 3 of 13
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
Page 4 of 13
SESSION-4
1. Open vi editor and create a file STUD containing the following data
s002:akash:unix:89:78
s004:zeenat:c:34:90
s005:zaira:vb:56:82
s001:puja:oracle:90:63
2. Save the file.
3. Copy line number 1 below line number 4.
4. Above the line 1 add the text STUDENT DETAILS
5. Move line 2 below the line containing code s004.
6. Copy lines 1,2 & 3 at the end of existing lines.
7. Search for the pattern puja (using editor command) in the file and delete it.
8. Save the file and quit editor.
9. Sort the file STUD(in the background) on student code and store the result in the same file.
10. Give the ps command and observe the output.
11. Give the command ls l and observe the permissions for the file STUD.
12. Give execute permissions for all the category of users for file STUD, and observe the same with ls -l.
13. Give write permissions for group and others for file STUD(use octal notation ), and observe the same
with ls -l.
14. Remove the write permission for the user (owner) for STUD and try modifying the file.
15. Create a directory named DIR .Remove execute permissions for DIR for the user(owner)
16. Change the directory to DIR. Is it possible .If no , what is the solution.
17. Create a student data file in vi which contains the details of all your friends (a minimum of 25
records). Each record should contain the following details : Student Number, Student Name, Batch
Number, Marks In Exam1, Marks In Exam2. The field separator should be a colon.
18. Give the command to sort the above mentioned file in the order of Batch Code in the
background.
Make sure that the sorted data gets stored in the same file.
19. Display a long listing of your home directory. Store the details in a file called list by giving a
20. command in the same line.
21. Check whether the sorting is still going on, in the background. If so, terminate it.
22. Display only the filename and the size of all the ordinary files in your home directory.
23. Repeat the above command by taking the input from the file list instead of directly taking it
24. from the Is command.
25. Display the names of all files which has a rwx permission for the group.
26. Display the names of only those files that are executable.
27. Display the number of ordinary files in your directory.
28. Check what are the processes that are running now.
29. Issue the command to terminate the entry with the name sh.
30. Send a message to another user asking for his floppy diskette.
31. Check whether you have a mail.
32. Utilise some time in getting accustomed to the internal commands of mail.
33. Display the contents of student data file without any colons.
34. Display the contents of student file in the alphabetical order of Student Name (Only the Batch
35. Code and Student Name has to be displayed).
Page 5 of 13
SESSION-5
I
II
at the specified place and move the named file from your home directory to this newly created
directory.
4) To display a directory listing as follows :
Your Home Directory is <home directory name>
Filename1 Size Date Time
Filename2 Size Date Time
Filename3 Size Date Time
:
Total number of files : <total number>
5) Accept a filename as the command line argument. Validate for the number of arguments. If it is
not equal to one, display
Usage : count <filename>
and terminate execution.
Check whether the file is existing and it is an ordinary file. If there is an error, display
a suitable message and terminate execution.
If there are no errors, display the output as :
The statistics of the file <filename> are as follows :
<no. of lines> number of lines.
<no. of words> number of words.
<no. of characters> number of characters.
Page 7 of 13
SESSION-6
I
II
Write shell scripts for accepting the following information and storing it in a file.
Cassette No.
Movie Name
Language
Price
Date of release.
The user should be given the facility to enter as many records as he wants.
2. Write a shell to accept the details for READWELL LIBRARY. The details to be stored are :
Book No
Name of the book
Author
Publishers Name
Price
Year of release
Subject Code
Validations
Book No. should not be existing in the file.
Name of the book, author and the publishers name entered cannot be blank.
Year of release should be less than 1996.
Price entered cannot be zero.
Subject Code can only be either UNIX or C.
The user should be able to repeatedly enter the data until it is valid. He should also be able to
enter multiple records.
3.
Write a shell script which will do the following: The first parameter has to be a directory name. It
should be followed by filenames. There can be more than 10 filenames. These specified files
should be copied to the directory name passed as the first parameter. The script should also
check whether the directory is already existing. If not it has to be created. The program should
also check for the existence of individual files whether they are existing and are readable.
Page 8 of 13
4.
Write a shell procedure to delete the specified record from the library file. It should accept a book
number, check whether it is existing. If so, display the current details, ask for confirmation and
then delete it from the file.
5.
Write a shell procedure to update the price field in the library file. The script should accept a valid
book number. The script should add 100 to the existing price of the book and update the file.
Page 9 of 13
SESSION-7
1. The file EMPLOYEE contains the employee code, employee name, designation and salary of the
employees
0001-satya-clerk-3000
0002-nisha-manager-10000
0003-suman-executive-6000
0004-vidya-g.m-12000
0005-sandhya-exec-10000
0006-pawan-m.d-12000
0007-ashwin-exec-4000
0008-madhu-director-2000
0009-madhoo-manager-7000
0010-sangli-executive-13000
2. Write an awk script to print all the employee names and their salary.
3. Print all the records in a formatted manner as below.
0001 satya
clerk
3000
0002 nisha
manager
10000
4. Print all the records where the salary is greater than 6000.
5. Print all the records where salary is greater than 6000 and designation is director.
6. Print all the records that contain madhu or madhoo in them.
7. Print the longest record in the file.
8. Print the records with record/line numbers.
9. To the above script add a heading EMPLOYEE DETAILS and the total number of employees in the
end.
10. Make changes to the above script such that all the details are centered across the output screen
11. Accept a word and display its length. Write a shell script for the above.
12. Write a shell script for accepting a word from the user and display the reverse of the word.
Example : If the user enters TULEC GK2, display it as 2KG CELUT.
13. Write a script for the following :
Accept a .c filename and an executable file name as command line arguments. Activate the vi
editor to create/edit the specified .c file. Once when the user comes out of vi, the script should
compile the .c file to give an executable file of the specified name. Command to compile a .c
file is
cc -o <executable file> <.c filename>
All errors should be redirected to a file named errlist.
If there are errors, display the error listing one page at a time after displaying the message The
following are the errors encountered in the file <.c filename>. Then open vi to edit the .c file.
If there are no errors, run the executable file after giving executable permission for it. Once
When the user finishes running the file, ask a question Do you want to continue (Y/N) ? and
accept an answer from the user. Depends on the users wish, either continue execution or
terminate it.
14. Accept a filename. Assign a read and write permission to others for that file, only if it is not
having these permissions at present. The above mentioned tasks should be achieved with the
help of a shell script.
15. Write shell scripts for the following :
Page 10 of 13
(a) Locate the files under your home directory that have not been modified for the last one week.Copy all
those files to a subdirectory called Oldies. If the subdirectory is not existing, create it. Before
copying individual files, ask the user Do you want to copy <filename> to Oldies or delete it
(C/D) ?. If the user enters C (Capital or small letter), copy the file. If the user enters D or d,
delete the file.
(b) The script should display the name of individual files in your directory one after the other in
the following format :
<filename> Copy/Move/Delete/Link (C/M/D/L) ?.
The user should be able to enter either C, M or D as per his/her wish. Depends on the option
entered by the user, the action has to be taken.
In the case of C, M and L, ask for a destination directory. Once when the directory name is
accepted, check whether the directory is already existing. If it is not existing, ask the user
<directory name> not found - Want to create (Y/N) ?. If the user enters Y or y, prompt for
the name of the parent directory under which the new directory has to be created. Continue
accepting the name of the parent directory until the user enters a valid directory name that
is existing. Then create the directory and proceed with Copy, Move or Link. If the option
entered by the use is for deletion i.e. (D), delete the file after getting a confirmation from the
user.
(c) Create a student file which contains the following data :
batch code : student code : average marks : status
Accept Batch Code, Student Code, Marks in exam 1 and Marks in exam 2. Calculate the
average marks. Find out status as passed or failed depending on whether the student got
above 50 or not. The shell should continue execution till the user responds by saying N to
the question More (Y/N) prompted by your shell script.
Incorporate as much validations as possible.
Once when the program terminates, it should display
This program started running at <starting time>.
And finished its execution at <ending time>.
(d) The script should display the following information :
The free disk space available on the file system is <number>. and the space used by the
current directory and its sub-directories is <number>.
Page 11 of 13
SESSION-8
Contents of the file PHONE are
Smith, Terry,
Bob, Wills,
Steve, Waugh,
Brown, Sparrow,
Brown, Hedge,
Louis, Chris,
I
7-7898
2-3454
3-5978
4-5548
7-3456
4-6233
The following input file employee.dat contains the names, designation, basic, HRA and allowances for
the employees of same organisation.
Ramesh SSA 2000 1000 1000
Krishna PSA 3000 1500 1500
Ramaswamy SSA 2200 1100 1000
Choudhary SA 1500 750 750
Renuka SSA 1200 700 1200
Revathi SSA 1500 800 800
Radha PSA 2800 1500 1500
Kadambari SA 1500 750 750
1. Write awk script that will print the employee name along with the total salary.
2. Print all the records in reverse order of their fields (i.e. fifth field followed by the fourth, then
the third, then the second and finally the first.)
3. Print each record twice. First with fields in reverse order (of fields) and subsequently in the
original order.
4. Print the output in the following manner.
Ramesh SSA 2000
5. The fields of the file/etc./passwd are listed below :
File : /etc/passwd
Fields : login name:password:user id:group id:user name:home directory:login shell
5.1 Print out all login names where login name with user id less than 100.
5.2 Print all those login names which begin with an UPPER case alphabet.
Page 12 of 13
Give the awk statement for printing the shortest record of the file batsman
Give the statement to print just the first three characters of the name and country fields.
Print only the last digit of the last field.
Write an awk script to print the longest name.
Print the first five records of the file.
11.Display the details of the file batsman after sorting it in the numerical order of the fourth field.
By making use of the batsman file, write an awk script which will store the records related to the
country Australia in a file Aus and that of England in Eng.
13. Print each line from the file batsman with record number.
Page 13 of 13