UNIX For BI-LabBook
UNIX For BI-LabBook
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.
Capgemini Internal
Document Data UNIX
Document Revision History
Page 2 of 17
Capgemini Internal
UNIX
Table of Contents
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>
Capgemini Public
UNIX
Page 6 of 17
Capgemini Public
UNIX
Page 7 of 17
Capgemini Public
UNIX
Page 8 of 17
Capgemini Public
UNIX
Page 9 of 17
Capgemini Public
UNIX
Goals
• Learn to grant and to remove permissions and to view the file system
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
1: Redirect the content of the help document ls, into a file called as lsdoc.
Page 10of 17
Capgemini Public
UNIX
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.
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
Page 11 of 17
Capgemini Public
UNIX
14. Use nl command to create a code table for designations to include designation
code (Start with dept. code 100, and subsequently 105, 110 …).
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.
Page 12 of 17
Capgemini Public
UNIX
8. Display only the filename(s) in which details of employee by the name ‘Seema
Sharma’ can be found.
9. Locate the lines containing saxena and saksena in the Mumbai office.
10. Find the number of managers who earn between 50000 and 99999 in the Pune
office.
11. List names of employees whose id is in the range 2000 – 2999: in Pune Office; in
all offices.
12. Locate people having same month of birth as current month in Pune office.
13. List details of all employees other than those of HR and Admin in file F1.
14. Locate for all Dwivedi, Trivedi, Chaturvedi in Pune file.
15. Obtain a list of people in HR, Admin and Recr. depts. sorted in reverse order of the
dept.
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 17
Capgemini Public
UNIX
Lab 5. Vi Editor
2: Create the data files, used in the previous lab sessions using vi editor.
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.
Page 14 of 17
Capgemini Public
UNIX
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”
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.
Page 15 of 17
Capgemini Public
UNIX
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.
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.
Page 16 of 17
Capgemini Public
UNIX
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 17 of 17
Capgemini Public