Linux Assignments - Practise
UNIX
ASSIGNMENTS: Day 1
Concept: Basic commands in UNIX, Filters, Pipes
Objective: Participant will be able to:
·
Execute Basic Unix commands
·
Implement the concepts of Pipes and
Filters
·
Work with vi editor
Problems:
Section 1:
1.
List all the files and sub
directories of the directory /bin.
2.
List all the files including hidden
files in your current directory.
3.
List all the files starting with
letter ‘r’ in your current directory.
4.
List all the files having three
characters in their names, from your current directory.
5.
List all the files with extension
‘.doc’ in your current directory.
6.
List all the files having the first
letter of their name within the range ‘l’ to ‘s’, from your current directory.
7.
Create a file ‘text1’ and read its
input from the keyboard.
8.
Copy the contents of file ‘text1’ to
another file ‘text2’.
9.
Append the contents of file ‘text2’
to file ‘text1’.
10.
Count the number of files in the
current directory.
11.
Display the output of commands ‘ls
-l’ to a file and on the output screen.
12.
From file ‘text1’ print all lines
starting from 10th line.
13.
Find the number of users currently
logged on to the system.
14.
Delete all files with their names
starting with ‘tmp’.
Section 2:
1.
Display your current working
directory.
2.
Create following directory structure
under your Home directory
(Note: Your home directory is where
you login into.)
3.
List detailed information about all
the files and directories of ‘Hardware’ directory while your current directory
is still the home directory.
4.
Change your current directory to
‘SYS’ and list the names of all files and sub directories in the directory sub
tree that starts from your home directory.
5.
Copy the file ‘SCANNER’ to directory
‘SYSTEM’ while your current directory is ‘APPL’.
6.
Rename the file ‘SCANNER’ to ‘SCAN’.
7.
Read some text from keyboard and
append it to the file ‘SCAN’.
8.
Remove the directory sub tree
starting from ‘SYSTEM’ in one go.
Section 3:
1.
Count the total number of words in
file ‘text1’.
2.
List the contents of ‘ls’ command
page wise.
3.
Create a file ‘FILE2’ with some text in
it. Increase the no. of hard links to the ‘FILE2’ to 3 and check the inode
number and link count for those names.
4.
Using one single command, display the
output of ‘who’ and ‘pwd’ commands.
5.
Display the system dare in following
format:
Today is Friday, 17 May 96
6.
Display the following text message on
the monitor screen.
Deposited $100 to your account
7.
Display the following message on the
monitor.
The long listing of my home dir …………. is ………………
(Hint: Use ls -l and pwd commands)
8.
Use find command to locate the following within your home directory
tree:
a)
Files with extension ‘.c’ or ‘.pl’
b)
Directories having permission ‘755’
c)
Files having permission 655
d)
Files having inode number 12122
e)
Files which have not been accessed
for more than a year and save the list in Old_File
f)
Files whose size is greater than 1024
bytes
Section 4:
1.
Using vi editor:
a)
Create a file “Data1.txt’
b)
Save the file and exit from the vi
editor.
c)
Open the vi editor without specifying
a file name
d)
Write some text and save it to a file
‘MyData2.txt’
e)
Repeat point (c) but after writing
some text don’t save and just exit ‘v’
2.
Create a file using vi editor and
enter the following text in it:
Unix Unix Unix Unix Unix
Unix is multi user operating system, Unix is multi tasking operating
system
Everything on Unix is a file.
Unix File structure is hierarchical like an upside down tree.
Regular files cannot contain another file, or directory
Directory File Contains directory(s) and/or file(s) within it
Device files are used to represent physical devices.
Symbolic link is an indirect pointer to a file
a)
Save the file without exiting vi.
b)
Display the line from within vi
c)
Move first three lines of the file to
the end of the file.
d)
Copy 5th line and paste
above the first line.
e)
Search the word Unix in forward
direction.
f)
Search the word Unix in backward
direction.
g)
Replace all the occurrences of the
word Unix
with UnixOS
UNIX
ASSIGNMENTS: Day 2
Concept: Shell Scripting
Objective: Participant will be able to understand and implement:
·
Regular expressions and grep
·
Shell fundamentals
·
Using basic UNIX commands and filters
as building blocks
·
The commands as conditions for
decision making in shell scripts
·
Shell scripting commands
Problems:
1.
`List only the directories in your
current director
2.
Display the name and count of the
directories in the current directory
3.
Find out whether the users with a
pattern ‘xx’ in their names have logged in
4.
Find out whether a particular user
‘xxx’ has logged
5.
Assign a value ‘Black’ to ‘var1’ and
then display the following message on the terminal using this variable,
Black belt is associated with
karate
6.
Create the file employee.txt having
colon (:) separated fields.
The fields of the record are:
enumber, ename, eposition, esal, edoj, edept.
And now answer the following:
a.
List all the employees along with a
row number
b.
Sort the file as per the names
c.
List top three salaried employees
d.
Remove duplicate records from the
file
e.
List dept. no along with no. of
employees working in each dept
f.
Sort the file in descending order of
salary
7.
Accept a file name and a number (x).
Display x lines from the top of the file. Check if the file exists and is
readable. The value of x should not exceed the total number of lines in the files.
Display suitable messages in case an error is encountered.
8.
Write a menu-based script which
displays the following options:
1.
Make a file.
2.
Display contents
3.
Copy the file
4.
Rename the file
5.
Delete the file
6.
Exit
Enter your option:
If the user selects option 1,
accept a file name from the user. If the file exists, then display an error
message pass the control to the menu. If the file does not exist, then allow
the user to enter some data. Pressing “^D” would save the contents and display
the menu.
If the user selects option 2,
then accept a file name from the user. If the file exists, then display the
contents of the file. If the file does not exist, then display suitable error
message. After this process, display the menu to accept another option.
Selecting option 3 allow the
user to accept the source file and target file. If the source file exists and
is readable, then accept the target file name. If the source file foes not
exist, then display suitable error message. If the target file does not exist,
then copy the contents of the source file to the target file. If the target
file exits, then display suitable message and go back to the menu.
Option 4 is similar to option
3 but rename the file instead of copying.
Selecting option 5 allows the
user to enter a file name. If the file exists, then check to see if it is
writable. If so, then delete the file with confirmation from the user. If the
file does not exist, then display suitable error message.
Option 6 should
allow the user to come out of the menu.
9.
Write a menu-based script which
displays the following options:
Accept roll number, name, marks in sub1, sub2 and sub3. Calculate total,
percentage, grade and class and enter the record in a file “student”. The marks
are out of 100 in each subject. Allow the user to enter any number of records.
Grade is found as follows:
Percentage Grade
<35 Fail
>=35 & <50 Third
>=50 & < 60 Second
>=60 & < 75 First
>=75 Distinction
If the student scores <35 in any one of the subjects, then class = “fail”.
Otherwise class=”Pass”.
10.
Accept roll number from the user at
the command line. Search it in the “student” file. If it is present, then
display name, percentage, grade and class of the student. If the roll number is
not present, then display a message “not found”. Allow the user to enter any
number of queries.
11.
Accept the roll number from the user.
Search it in the file. If it is present, then delete the respective record from
the “student” file. Get the confirmation from the user before deleting the record
from the file. If the roll number is not present, then display suitable error
message.
12.
Write a menu-based shell script which
will perform arithmetic operation on two numbers which are inputted by user.
Menu should display the following operation
Menu
---------------
1: Addition
2: Subtraction
3: Multiplication
4: Division
5: Exit
13.
Write a shell script that will remove
a file taken as command line argument after taking the proper backup of file in
/home/user1/backup directory
14.
Write a shell script that will accept
a string from the user. Copy all contents of the file to other file without
that string. Also display number of characters, lines, and words.
15.
Write a shell script which will
generate the O/P as follows
*
***************
*
16.
Write a shell script which will
accept three numbers from the keyboard and finds the largest of them.
17.
Write a shell script which will
accept three numbers from the keyboard and finds the smallest of them.
18.
Write a shell script which will
calculate the factorial of an integer entered from the keyboard.
19.
Count total number of files in the
current directory containing the text “UNIX” in them.
(Note: Prepare a few files with some
text as well as the text “UNIX” in them to test it)
20.
Write a shell script to copy one file
to another line by line.
(Hint: May use head/tail filters)
21.
Write a shell script that displays
the name of all ordinary files in the current directory having execute
permission for the owner.
22.
Write a shell script which allows a menu-based
selection of whether a user wants to:
a.
List the contents of his HOME directory
b.
List the contents of another user’s
HOME directory
c.
Display only the login name along
with the terminal addresses of all the users currently logged on to the system
d.
Exit the script
UNIX
ASSIGNMENTS: Day 3
1.
Write a shell script which will
generate the O/P as follows:
*
**
***
****
2.
Accept the first name, middle name,
and last name of a person in variable fname, mname and lname respectively.
Greet the person (take his full name) using an appropriate message.
3.
Display the names of files in the
current directory along with the names of files with maximum & minimum
size. The file size is considered in bytes.
4.
Write a script which when executed
checks out whether it is a working day or not?
(Note: Working days Mon-Fri)
5.
Write a script that accepts a member
into TTT health club, if the weight of the person is within the range of
30-250kgs.
6.
Write a script that greets the user
with an appropriate message depending on the system time.
7.
A data file has some student records
including rollno, names and subject marks. The fields are separated with a “:”.
Write a shell script that accepts rollno from the user, searches it in the file
and if the roll number is present allows the user to modify one record at a
time.
8.
Modify program 7 to accept the roll
number from the command line.
9.
Modify program 7 to accept the roll
number and display the record and ask for delete confirmation. Once confirmed,
delete the record and update the data file.
10.
Write a script that takes a command
line argument and reports on its file type (regular file, directory file, etc.)
For more than one argument generate error message.
11.
Add some student records in the
“student” file manually. The fields to be considered are “RollNo”, “Name”,
“Marks_Hindi”, “Marks_Maths”, “Marks_Physics”. Write a script which does the
following,
a.
If the roll number already exists,
then store the record and the following message “roll number exists” in a log
file “log1”.
b.
If the marks in the subjects is not
in the range of 1-99 then store such a record followed by a message “marks out
of range” in “log1”
c.
If the data is valid, then calculate
total, percentage, grade and display on the terminal.
12.
Create a data file “master” and enter
some records manually. The record comprises of batch code, faculty name and
number of students. Keep number of students as 0 initially.
Write a script that accepts the batch
code and searches it in the “master” file.
If it present, then allow the user to
enter any number of records in a file with the name same as the batch code
itself.
These records should have the fields
RollNo, Name and marks in Hindi, Maths and Physics.
Keep updating the number of student’s
field in “master” file.
13.
Create a library of shell functions
to do the following:
Function to concatenate 2 strings.
Pass 2 strings as parameters to function.
Function to find out the length of a
given string. Pass string as a parameter to function.
Function to compare two strings. Pass
2 strings as parameters to function.
Function to check if string is
palindrome or not. Pass string as a parameter to function.
Function to print given string in
reverse order. Pass string as a parameter to function.
14. Assignments on “sed” command
EmpData (Sample Database – save in a
file)
1122|j.b. saxena |g.m |account
|12/12/52|6000
2233|n.k. gupta |d.g.m |sales |31/12/40|9000
4545|anil agarwal |director |account
|06/07/47|7500
5656|lalit choudhury |executive |marketing
|07/09/50|5000
1265|chanchal singhvi |g.m. |admin
|12/09/63|6000
0110|shyam sakxena |chairman |marketing
|12/12/43|8000
5566|jai sharma |director |account
|23/12/89|7000
7733|Jayant |d.g.m |sales
|29/02/70|6000
a.
From the above database substitute
the delimiter of first 3 lines with “:”
b.
From the above database substitute the
delimiter with “:”
c.
Insert the string “MySpc Employees”
in the first line.
d.
Store the lines pertaining to the
directors, d.g.m and g.m. into three separate files.
e.
Using address store first 4 lines
into a file Empupdate.
f.
Find the pattern “account” in the
database and replace that with “accounts”.
g.
Select those lines which do not have
a pattern “g.m”.
h.
Insert a blank line after every line
in the database.
UNIX
ASSIGNMENTS: Day 4+5
Concept: Awk scripting and case study
Objective: Participant will be able to understand and implement:
·
Awk scripting
·
Write shell scripts and embed Awk
scripts with it
Problems:
1.
Consider the results are stored in
following format:
EmpID Name Subject Marks (/50)
E001 Nilesh Unix 30
E002 Nilesh DSA 30
Like these you have 10 records (5 of
DSA and 5 of Unix)
Calculate the avg score secured in
Unix and DSA and the first 2 toppers in Unix and DSA each.
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
users logged in.
3.
Consider a text file containing the
number of 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:ObtMarks:MarksOutof35
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 1st, 2nd, 3rd
with their total score.
4.
Create a emp_mast and dept_mast files
containing following details,
empno:name:job:deptno
deptno:deptname
empno:basic:hra:conveyance:medical:prof.tax:PF:TDS
Generate the payslip as shown below
by passing the empno as parameter to the Awk script.
Empno 1001
Deptno 10
Job MGR
Currency 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
-------------------------------------------------------------------------------------------------------------------------
Note: Check empno
before passing to Awk script.
5.
Generate a report as given below by
passing the deptno to Awk script
Deptno : 10 Dname
: Admin
--------------------------------------------------------
EmpNo Name Job
--------------------------------------------------------
1001 Ketan MGR
1002 Sachin CLERK
--------------------------------------------------------
Note: Check deptno before passing to Awk34 script
6.
Consider the Arizona roster 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.
(a)
Reports the number of players on the
roster
(b)
Displays the roster in order by
jersey number
(c)
Displays the roster in alphabetical
order by surname
(d)
Displays the heaviest five players
(e)
Displays all players who attended
Wisconsin
The Roster is given below:
JersyNo|Name,surname,
|linebackers|Weight | Date | Experience| Country
20|Anderson, Damien |RB
5’10” | 212 | 07/17/1279 | 3 |
Northwestern
30|Ayanbadejo, Oba
|FB 6’02” |235 |03/05/1975 |5| San Diego
92|Berry, Bert | DE 6’03” | 250 |08/15/1975 |7|NotreDame
81|Boldin, Anquan |WR 6’01” |215 |10/03/1980 |2|FloridaState
91|Bryant, Wendell |DT 6’04” |303 |09/12/1980 |3|Wisconsin
35|Carter, Dyshod |DB 5’10” |197 |06/18/1978 |2|Kansas State
52|Fisher, Levar |OLB 6’01” |235 |07/02/1979 |3|North Carolina
11|Fitzgerald, Larry |WR 6’04” |229 |08/31/1983 |10|Pittsburgh
63|Garcia, Frank |G 6’02” |302 |01/28/1972 |10|Washington
64|Grace, Steven |C 6’03” |295 |02/13/1979 |3|Arizona
84|Hamilton, Lawrence |WR 6’03”
|205 |08/31/1980 |2|StephenF.Austin
Where can we find answers to these assignment questions?
ReplyDeleteWhere can we find answers to these assignment questions?
ReplyDelete