UNIX-LabBook Solutions
UNIX-LabBook Solutions
(Version: 1.5)
Lab Book
Copyright © 2011 IGATE Corporation (a part of Capegemini Group). All rights reserved.
No part of this publication shall be reproduced in any way, including but not limited to photocopy,
photographic, magnetic, or other record, without the prior written permission of IGATE Corporation (a
part of Capegemini Group).
IGATE Corporation (a part of Capegemini Group) considers information included in this document to
be confidential and proprietary.
Document Data Unix
Page 2 of 29
Unix
Table of Contents
Document Revision History ................................................................................................ 2
Table of Contents ............................................................................................................... 3
Getting Started ................................................................................................................... 5
Overview ...................................................................................... 5
Setup Checklist .............................................................................. 5
Instructions ................................................................................... 5
Learning More (Bibliography if applicable) ............................................ 5
Lab 1. Connecting to the Unix Server ............................................................................. 6
1.1: Connecting to the Unix Server ...................................................... 6
1.2: Logging out of the system ........................................................... 6
Lab 2. Unix Basic Command .......................................................................................... 7
2:1 Executing basic commands: ........................................................... 7
Lab 3. UNIX File System & Permissions ...................................................................... 10
3.1: Viewing the File System and Granting/Removing Permissions ............. 10
(Note: Create required files if doesn’t exists.) ..................................... 10
Lab 4. Simple and Advance Filetrs .............................................................................. 11
4.1: Using Pipes and Filters: ............................................................ 11
Lab 5. Vi Editor ............................................................................................................. 15
5.1: Working wth Vi Editor .............................................................. 15
Lab 6. SED Commands ................................................................................................ 16
6.1: Using SED Commands ............................................................... 16
Lab 7. Process Related Commands ............................................................................. 17
7.1: Using Process-Related Commands ................................................ 17
Lab 8. AWK ................................................................................................................... 18
8.1: Writing awk-Scripts ................................................................. 18
EmpName:EmpId:Subject:ObtMarks:TotMarks:Result .............................. 18
Lab 9. Shell Script ......................................................................................................... 22
9.1: Writing Shell-Scripts ................................................................ 22
Lab 10. Source Code Control System ........................................................................ 25
11.1: Repository Creation ............................................................... 25
11.2: Initial Project Setup ............................................................... 25
11.3: Creating the working copy of project (Check out) ........................... 26
11.4: Modifying, updating, creating new file in the project ...................... 27
11.5: The lock-modify-unlock Cycle ................................................... 28
Page 3 of 29
Capgemini Public
Unix
Page 4 of 29
Capgemini Public
Unix
Getting Started
Overview
This lab book is a guided tour for learning Unix. It comprises ‘To Do’ assignments. Follow
the steps provided and work out the ‘To Do’ assignments.
Setup Checklist
Here is what is expected on your machine in order for the lab to work
Instructions
• For all coding standards refer Appendix A. All lab assignments should refer
coding standards.
• Create a directory by your name in drive <drive>. In this directory, create a
subdirectory html_assgn. For each lab exercise create a directory as lab <lab
number>
Page 5 of 29
Capgemini Public
Unix
Page 6 of 29
Capgemini Public
Unix
Page 7 of 29
Capgemini Public
Unix
18. Change to the directory /etc and display the files present in it.
19. List the names of all the files that begin with a dot in the /usr/bin directory.
20. Create a file first.unix with the following contents.
Hi! Good Morning everybody.
Welcome to the First exercise on UNIX.
Hope you enjoy doing the assignments.
21. Copy the file first.unix in your home directory to first.unics.
(Note: checked using ls, first.unix file also should exist along with first.unics)
22. List the contents of first.unix and first.unics with a single command.
23. Create a new directory under the temp directory.
24. From your home directory, copy all the files to the directory created under the temp
sub directory.
25. Move the file first.unix to the directory temp as second.unix
26. Remove the file called first.unics from the home directory.
27. Change your directory to temp and issue the command rm *. What do you observe?
28. Move all files whose names end with a, c and o to the HOME directory.
29. Copy all files that end with a ‘UNIX’ to the temp directory.
30. Issuing a single command, remove all the files from the directory temp and the
directory itself.
31. Try commands cp and mv with invalid number of arguments and note the results.
32. Use the cat command to create a file friends, with the following data:
Madhu 6966456 09/07/68
Jamil 2345215 08/09/67
Ajay 5546785 01/04/66
Mano 7820022 09/07/68
David 8281292 09/09/60
Simmi 7864563 12/12/70
Navin 2224311 30/05/68
The fields should be separated by a tab.
33. Display contents of the file friends.
34. Copy contents of friends to newfriend without using the cp command.
35. Display contents of the file friends and newfriends in a single command.
36. Find all users currently working on the system and store the output in a file named as
users.
37. Append contents of friends file to the file, users.
Page 8 of 29
Capgemini Public
Unix
38. Display current system date and time and record your observations. How is the time
displayed?
39. Display calendar for the month and year of your birth.
40. Try following commands and record your observations.
date “+ %”
date “+%m”
date “+%D”
date “+%/%Training Activity”
date “+%Training Activity”
date “+%r”
Page 9 of 29
Capgemini Public
Unix
Goals • Learn to grant and to remove permissions and to view the file system
Time 15 min
Lab Setup Telnet with Unix Server
1. Give the execute permission for the user for a file chap1
2. Give the execute permission for user, group and others for a file add.c
3. Remove the execute permission from user, give read permission to group and others
for a file aa.c
4. Give execute permission for users for a.c, kk.c, nato and myfile using single
command
5. Change the directory to root directory. Check the system directories, like bin, etc, usr
etc
Page 10 of 29
Capgemini Public
Unix
1: Redirect the content of the help document ls, into a file called as lsdoc.
6: There will be B’day celebration from the friends file, find how many B’day parties will
be held. If two of the friends have the B’date on the same day, then we will be having
one party on that day.
8: Display the lines starting with Ma, ending with i or ending with id, in the file friends.
9: Print all the files and the directory files from the current directory across all the sub
directories, along with its path
11: Display the files starting with chap, along with its path.
14: Store the contents of your home directory in a file called dir.
15: From the above file dir, display the file permissions and the name of the file only.
16: From the same dir file, store only the file names in a file called files.
Page 11 of 29
Capgemini Public
Unix
17: From the same dir file, store only the permissions of files in a file called perms.
18: From the same dir file, store only the file sizes in a file called sizes.
19: Display the file names, sizes and permissions from your directory in that order.
22: Display the total number of lines present in the file friends.
23:Create the following fixed record format files (with “|” delimiter between fields) with the
structure given below, and populate them with relevant data use these files to solve
following questions
emp.lst: Empid(4),Name(18),Designation(9),Dept(10),Date of Birth(8),Salary(5)
dept.lst : Dept.Code(2),Name(10),Head of Dept’s id(4)
desig.lst: Designation Abbr.(2), Name (9)
1. Find the record lengths of each file.
2. Display only the date of birth and salary of the last employee record.
3. Extract only employee names and designations. (Use column specifications).
Save output as cfile1.
4. Extract Emp.id, dept, dob and salary. (Use field specifications). Save output as
cfile2.
5. Fix the files cfile1 and cfile2 laterally, along with the delimiter.
6. Sort the emp.lst file in reverse order of Emp. Names.
7. Sort the emp.lst file on the salary field, and store the result in file srtf.
8. Sort the emp.lst file on designation followed by name.
9. Sort the emp.lst file on the year of birth.
10. Find out the various designations in the employee file. Eliminate duplicate listing
of designations.
11. Find the non-repeated designation in the employee file.
12. Find the number of employees with various designations in the employee file.
13. Create a listing of the years in which employees were born in, along with number
of employees born in that year.
14. Use nl command to create a code table for designations to include designation
code (Start with dept. code 100, and subsequently 105, 110 …).
Page 12 of 29
Capgemini Public
Unix
24: PCS has its offices at Pune, TTC and Mumbai. The employees’ data is stored
separately for each office. Create appropriate files (with same record structure as in
previous assignment) and populate with relevant data.
Stretched assignments:
25: Write a command sequence that prints out date information in this order: time, day of
week, day number, month, year:
13:44:42 IST Sun 16 Sept 1994
26: Write a command sequence that prints the names of the files in the current directory
in the descending order of number of links
27: Write a command sequence that prints only names of files in current working
directory in alphabetical order
28: Write a command sequence to print names and sizes of all the files in current
working directory in order of size
Page 13 of 29
Capgemini Public
Unix
Page 14 of 29
Capgemini Public
Unix
Lab 5. Vi Editor
Time 30 min
Lab Setup Telnet with Unix Server
2: Create the data files, used in the previous lab sessions using vi editor.
Page 15 of 29
Capgemini Public
Unix
Time 15 min
Lab Setup Telnet with Unix Server
a) Write a sed command to print only the lines starting at line 2 and ending with the letters
“Pune”
b) Write a sed command that will display the top 5 lines from the file
c) Write a sed command that will substitute the word “Chennai” for "Pune" used in all
instance of the word
d) Write a sed command that will replace occurrence of the character e with the string
UNIX in all lines. (Use –e option)
2: Create a new file “PACE.dat which has only the lines that contain the word
“PACE” from Employee.dat
Page 16 of 29
Capgemini Public
Unix
Time 15 min
Lab Setup Telnet with Unix Server
2. Find out the information about all the processes that are currently active
3. Start a different process in the background. Find out the status of the background
process using the PID of the same.
Page 17 of 29
Capgemini Public
Unix
Lab 8. AWK
Time 90 min
Lab Setup Telnet with Unix Server
2. Write a script to get the report of the users logged on to the System in the
following formats. (Records should be sorted on logging time.)
Header must include company name and Date
Records in the format
Username Logged-in-time Terminal
Tailor should include total number of the users logged in.
3. Consider a text file containing the records (colon separated fields) in the format:
EmpName:EmpId:Subject:ObtMarks:TotMarks:Result
Write a script to get the result of “UNIX” Subject in the format (Considering the
data file has TotMarks=50 for UNIX)
EmpName:ObtMark:MarksOutof35
Page 18 of 29
Capgemini Public
Unix
The header of the report must contain total marks and the tailor must specify the
percentage result for that subject.
Also generate another summary result containing total number of participants
appeared, total number of participants passed, and Name of the participants
ranked Ist IInd, IIIrd with their total score.
4 Consider the Arizona roaster as an input data file for this exercise. Using any
combination of the text-processing utilities listed below, write a one-line shell
command that performs each of the following tasks
empno:name:job:deptno
deptno:deptname
Page 19 of 29
Capgemini Public
Unix
Generate the pay slip as shown below by passing the empno as parameter to the
awk script.
Empno 1001
Deptno 10
Job MGR
Curency INR
-----------------------------------------------------------------------------------------------
Earnings
-----------------------------------------------------------------------------------------------
BASIC 10,000.00
HRA 4,000.00
CONVEYANCE 9,600.00
MEDICAL 5,000.00
-----------------------------------------------------------------------------------------------
Total Earnings : 28,000.00
-----------------------------------------------------------------------------------------------
Deductions
-----------------------------------------------------------------------------------------------
PF 1200.00
PROF_TAX 200.00
TDS 1600.00
-----------------------------------------------------------------------------------------------
Net Pay : 25000.00
-----------------------------------------------------------------------------------------------
1. Generate the report as given below by passing the deptno to awk script
-----------------------------------------------------
Empno Name Job
-----------------------------------------------------
1001 Ketan MGR
1002 Sachin CLERK
----------------------------------------------------
Page 20 of 29
Capgemini Public
Unix
Page 21 of 29
Capgemini Public
Unix
2: As soon as you login, the prompt should be changed to your name: also the name of
the home directory should be automatically displayed.
Note: Type every line and press enter, do not type the entire code in a vi editor.
$continent=”Africa”
$echo “$continent”
------------→ Africa
$sh
$echo “$continent”
------------→ No Response
$continent=”Asia”
$echo “$continent”
------------→ Asia
$ctrl + d
$echo “$continent”
------------→ Africa
$sh
$echo “$continent”
------------→ No Response
$ctrl + d
5: Try the below exercise and check the output. (Export variables)
Note: Type every line and press enter, do not type the entire code in a vi editor.
$continent=”Africa”
Page 22 of 29
Capgemini Public
Unix
export continent
$echo “$continent”
------------→ Africa
$sh
$echo “$continent”
------------→ Africa
$continent=”Asia”
$echo “$continent”
------------→ Asia
$ctrl + d
$echo “$continent”
------------→ Africa
6: Write a shell script that takes the user name as input and reports whether he / she has
logged in or not.
7: Write a shell script to display the file name and its contents of all the files that is there
in the current directory.
8: Write a shell script, which will take a file name as argument and check whether the file
exists and display its access permissions for user.
9: Pass three numbers as command line arguments and display the largest number in the
given three numbers.
10: Write a shell script which will accept a pattern and a file name. The pattern will be
searched in the file provided. Display appropriate messages and perform necessary
validations on file.
11: To create a menu program for a) creating a file, b) Creating a directory, c) copying a
file, d) moving a file. (use functions)
a. If the file exists already give the appropriate message
b. If the dir exists already give the appropriate error message
c. Source file should exist if not give a message, It should have read permission if
not another message, Destination file either there or not, if not there then create
it and copy it. If there, then ask whether to overwrite or not, if yes then overwrite it
or else give a message file exists already and not overwritten.
12: Write a function yesno() to display question to user and accept answer as y/n. If
answer to the question is y the function should return 0 otherwise 1.
Use yesno functions for asking different questions. Question will be passed as
parameter to the function.
Accept filename from user check whether it is file or directory. Use yesno() function
to display question do you really want to delete file? If the ans is y, then delete the file
or directory.
Page 23 of 29
Capgemini Public
Unix
13: Write a shell script to store names of four employees and check whether those
employees are currently logged in or not. Display appropriate message.
14: Accept the user's first and last name and the echo the entire name along with some
suitable comment.
15: List all files that have been modified today.
16: Display long listing of only the regular files in the current directory.
17: Display details of all files in the 2 “paths” accepted from user. The display should be
screen by screen.
18: Let the script display its name and its PID.
19: Get the concatenated o/p of 2 files into a third file: Take 3 command line arguments:
The first argument is the name of a destination file, and the other two arguments are
names of files whose contents are to be placed in the destination file.
Stretched Assignments:
22: Display the contents of all .lst files in the current directory.
24: For a student file with the following fields, rollno, name, marks, Generate 2 files ‘Pass’
and ‘Fail’ containing records of student who have passed or failed. Also count the
number of students who have passed or failed.
25: Accept a date string from terminal and display employees born after the input date.
Page 24 of 29
Capgemini Public
Unix
Time 60 min
Lab Setup Linux Operating System and SVN client
Committed revision 1.
Deleting a folder
[root@pace ~]# svn rm
file:///home/demo_project_repository/Analysis -m "Removing
Project Analysis directory"
Committed revision 2.
Option 2: Using client (if the directory structure of project is already defined)
Page 25 of 29
Capgemini Public
Unix
Committed revision 3.
The project was imported to the SVN, now delete the local directories
Page 26 of 29
Capgemini Public
Unix
A demo_project_repository/Query_Tracking_Sheet
A demo_project_repository/Analysis
A
demo_project_repository/Analysis/Software_Requirement_Specificat
ion.docx
A demo_project_repository/Analysis/ReadMe.txt
A demo_project_repository/Coding
A demo_project_repository/Timesheet
A demo_project_repository/Design
A demo_project_repository/Design/High_Level_Design_1.2.docx
A demo_project_repository/Design/Low_Level_Design_1.2.docx
A demo_project_repository/Testing
A demo_project_repository/Testing/test_case-lv1.0.xls
Checked out revision 3.
[testuser2@pace project]$ tree ./demo_project_repository
./demo_project_repository
|-- Analysis
| |-- ReadMe.txt
| `-- Software_Requirement_Specification.docx
|-- Coding
|-- Design
| |-- High_Level_Design_1.2.docx
| `-- Low_Level_Design_1.2.docx
|-- Query_Tracking_Sheet
|-- Testing
| `-- test_case-lv1.0.xls
`-- Timesheet
Page 27 of 29
Capgemini Public
Unix
Committed revision 4.
11.5: The lock-modify-unlock Cycle
Login to the client with user1, go to the folder where working copy is stored, lock the file
on which user1 want to work
[testuser1@pace demo_project_repository]$ svn lock
Analysis/ReadMe.txt -m "Locking a File for Change"
'ReadMe.txt' locked by user 'testuser1'.
Login to the client with user2, go to the folder where working copy is stored, try to change
the same file which has been locked by user1.
[testuser2@pace demo_project_repository]$ cd Analysis
[testuser2@pace Analysis]$ vi ReadMe.txt
[testuser2@pace Analysis]$ svn commit -m 'User Confirmation
Document Added'
Sending Analysis/ReadMe.txt
Transmitting file data .svn: Commit failed (details follow):
svn: User testuser2 does not own lock on path
'/Analysis/ReadMe.txt' (currently locked by testuser1)
Now user1 can either perform the changes in the file and commit it (This will
automatically release the lock) or just unlock the file so that user2 will start working on it.
[testuser1@pace demo_project_repository]$ svn unlock
Analysis/ReadMe.txt
'ReadMe.txt' unlocked.
Page 28 of 29
Capgemini Public
Unix
4) Lock file code.txt , change the contents of the file and update the changes to
SVN server
Page 29 of 29
Capgemini Public