0% found this document useful (0 votes)
5 views24 pages

Unix Shell Programming Lab Guide

The document is a lab manual for Unix and Shell Programming, detailing various experiments aimed at teaching commands, file operations, and shell scripting. It includes objectives, experiments, outputs, lab assignments, and viva questions for each experiment, covering topics like file attributes, pattern matching, and arithmetic operations in shell scripts. The manual is structured to provide hands-on experience with Unix/Linux commands and scripting techniques.

Uploaded by

Piyush Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

Unix Shell Programming Lab Guide

The document is a lab manual for Unix and Shell Programming, detailing various experiments aimed at teaching commands, file operations, and shell scripting. It includes objectives, experiments, outputs, lab assignments, and viva questions for each experiment, covering topics like file attributes, pattern matching, and arithmetic operations in shell scripts. The manual is structured to provide hands-on experience with Unix/Linux commands and scripting techniques.

Uploaded by

Piyush Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unix & Shell Programming Lab Manual

Before starting the shell scripting in Unix/Linux shell we must have the
knowledge of commands related to different section. All these commands will
be helpful in shell scripting for making the user friendly program. We will
explore the commands by syntax, description, and options used with
commands. This is the good way to remember the command.

EXPERIMENT NO. 1

Aim: - Explain the following Commands which are used to retrieve the general
information of Linux system with description and options used with
command.
1. tput clear
2. cal
3. who
4. whoami
5. date (%m,%h,more)
6. ls
7. ls >list (> is chevron character)
8. cat list
9. wc
10. echo
11. pwd
12. whatis
13. printf “My name is Linux”
14. printf “my current shell is %s” $SHELL , $HOME
15. uname
16. uname –r
17. uname –n
18. tty
19. passwd
20. bc
21. xcalc
22. exit
23. logout
24. ps
25. script

Objective:-
1. Knowledge of UNIX / Linux terminal.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 1
Unix & Shell Programming Lab Manual

2. Execution of the commands.


3. Writing the command with options.
4. Study of help using man command in linux.

Experiment: - Open the terminal and execute the commands one by one.

Output: - Note down the output of commands with options in left side of your
file.

Lab- Assignment:-

Viva- Questions:-
1. What is Linux.
2. Difference between UNIX and Linux.
3. Features of Linux.
4. Versions of Linux.
5. Default shell of Linux.
6. Why UNIX/Linux are called case sensitive.

Useful Links:
1. [Link]
2. [Link]
3. [Link]
4. [Link]
5. [Link]

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 2
Unix & Shell Programming Lab Manual

EXPERIMENT NO. 2

Aim: Explain the following commands related to file and directory with detail
description.
1. cd 12. cp –i
2. mkdir 13. cp –R
3. rmdir 14. rm
4. cd. 15. rm –i
5. cd.. 16. mv
6. ls 17. wc with option –l,-w,-c
7. ls –x 18. cmp
8. ls –Fx 19. more
9. ls –aFx 20. less
10. cat 21. comm.
11. cp 22. diff

Objective:-
1. Creation of files and directories in linux.
2. Study of the operation related to files and directories such as copy
rename, delete etc.
3. Finding the path of the file and directory.
4. Showing the contents of directory.

Experiment: - Execute the commands one by one.

Output: - Note down the output of commands with options in left side of your
file.

Lab- Assignment: - Create your own directory. Save with your name and also
create multiple files inside of this directory and do the operation such as
renaming, copying, removing the files.

Viva- Questions:-
1. What is file explain with type.
2. Naming rule of files in unix/linux.
3. Features of file system.
4. Attributes of file.
5. Why cat command is called versatile.
Department of Computer Science & Engineering
UNIX & SHELL Programming Prepared By R. N. Shukla
Page 3
Unix & Shell Programming Lab Manual

EXPERIMENT NO. 3

Aim: - Explain the commands related to file attributes and creation of links
with files. How to change the permission of files and directory with detail
description.

Objective: -
1. To know the various attributes of files and directories.
2. To know the importance of permissions associated with every file in
unix/linux system.
3. To know the techniques to create link with your file.
4. To know the default permission associated with your file.
5. To know the working of chmod and ln with options.

Experiment: -
1. Execute the commands related to exploring the file attributes.
2. Execute the commands to change the permission of your file.
3. Execute the commands to create link with files.

Output: - Note down the output of commands with options in left side of your
file.
Lab- Assignment: - Create your own directory. Save with your name and also
create multiple files inside of this directory and do the operation such as link
creation and permission change of files.

Viva- Questions:-
1. What are the links and types of links with files.
2. Why we requires link.
3. Difference between hard and soft link.
4. Different methods of changing the permission.
5. What is recursive permission.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 4
Unix & Shell Programming Lab Manual

EXPERIMENT NO. 4

Aim: Write with description and options used in commands for pattern
matching.
1. sort 6. egrep
2. uniq 7. find
3. tr 8. touch
4. grep 9. nl
5. sed

Objective:-
1. To know the various wild card characters used in pattern matching.
2. To know the command and how to use quotation.
3. How to merge the commands.
4. How to take input from other commands.
5. How to use standard input and standard output syntax.

Experiment: - Execute the commands used as filters for finding particular


pattern from specified file.

Output: - Note down the output of commands with options in left side of your
file.

Lab- Assignment: - Create a file [Link] and save it on your working


directory.
File will contain the multiple fields and each field must be separated by any
separator. Field name will be Roll no, Name, Branch, Semester, Date of birth
and Marks Obtained. Enter at least 15 entries for various fields. Use this file
to perform the pattern matching commands and wild card characters.

Viva- Questions:-
1. Why we require pattern matching.
2. What are the different meta characters used in pattern matching.
3. Give the various options used with grep command.
4. What is basic regular expression.
5. Difference between BRE and ERE.
6. Working of different options with sort, sed, egrep, uniq, tr.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 5
Unix & Shell Programming Lab Manual
EXPERIMENT NO. 5

Aim: Study the working mechanism used in vi editor.

Objective:-
1. How to create files using vi editor.
2. How to work in various modes of vi editor.
3. How to perform navigation and substitution in any file.
4. What are the various editing and pattern searching techniques
used in vi editor.

Experiment:-

Output: - Note down the output of commands with options in left side of your
file.

Lab- Assignment: - Create a file [Link] using vi editor. Perform the


various editing, saving, substitution, copy, scrolling and navigation operation
and related short cut keys to easily perform the operation.

Viva- Questions: -
1. How many types of modes used in vi editor.
2. What is the use of vi editor.
3. How many types of different editors used in unix/ linux.
4. What are the different short cut keys used in different modes.
5. Explain the various short keys to perform the editing operation in
text file.
6. How the navigation and searching operations are performed in vi
editor.
7. Explain the disadvantages of vi.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 6
Unix & Shell Programming Lab Manual
EXPERIMENT NO. 6

Aim: - Write a shell script to accept three numbers and display the largest.

Objective: - After this students will be able to know:

1. How to make programs using shell scripting.


2. How to take inputs from user in shell.
3. Use of if-else statements in shell program.
4. Creation and display of variables.
5. Execution and retrieval of outputs of shell program.

Experiment:

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:

1. Try the same thing in getting out the smallest number.


2. Try the same concept to find the greatest among four numbers.

Viva Questions:-

1. What is BASH shell.


2. What is the use of echo, read and print command.
3. What is the variable in unix and types of variables.
4. What is rule for naming of variable.
5. What are the different operators used in the Unix shell scripting.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 7
Unix & Shell Programming Lab Manual
EXPERIMENT NO. 7

Aim: Write a shell script to display first ten positive numbers using until
loop.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-
1. Try to make the program for display of 1 to 20 multiplication table.
2. Try to make the program for display of numbers in reverse order.
3. Try to make the program using other loop such as while and for loop
available in unix.

Viva Questions:-
1. What are the different types of loop statement used in unix.
2. What is the difference between unix and c loop statement.
3. How to get the infinite loop condition.
4. How to use expr statement in unix loop.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 8
Unix & Shell Programming Lab Manual
EXPERIMENT NO.8

Aim: - Write a shell script which will accept different numbers and find
their sum.

Objective: - After this program students will be able to know:


1. How to perform arithmetic operations in shell programming.
2. How to use expression statement.
3. How to use loop with expression statement.
4. What are the different conditions to use expr statement.

Experiment:-
Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. How the loop can be used in shell program.
2. What are the different operators used in shell scripting.
3. How the arithmetic operations can be used in shell program.
4. Explain the syntax of while loop.
5. How the condition can be checked using operators.

EXPERIMENT NO. 9
Department of Computer Science & Engineering
UNIX & SHELL Programming Prepared By R. N. Shukla
Page 9
Unix & Shell Programming Lab Manual

Aim: - Write a shell script to generate the factorial of a given number


entered through keyboard.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output:- Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. How the loop can be used in shell program.
2. What are the different operators used in shell scripting.
3. How the arithmetic operations can be used in shell program.
4. Explain the syntax of while loop.
5. How the condition can be checked using operators.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 10
Unix & Shell Programming Lab Manual

EXPERIMENT NO. 10

Aim: Write a shell script to reverse the digits of a given number.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. How the loop can be used in shell program.
2. What are the different operators used in shell scripting.
3. How the arithmetic operations can be used in shell program.
4. Explain the syntax of while loop.
5. How the condition can be checked using operators.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 11
Unix & Shell Programming Lab Manual

EXPERIMENT NO. 11

Aim: - Write a shell script to generate the Fibonacci series.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. How the loop can be used in shell program.
2. What are the different operators used in shell scripting.
3. How the arithmetic operations can be used in shell program.
4. Explain the syntax of while loop.
5. How the condition can be checked using operators.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 12
Unix & Shell Programming Lab Manual

EXPERIMENT NO.12

Aim: - Write a menu driven program to display a menu of options and


depending upon the user’s choice execute the associated command.

Objective: - After the execution of the program student will be able


1. To know the use of case statement in unix shell programming.
2. To know how we can give multiple choice options to user.
3. To know how we will provide default option.
4. To know the execution of various commands with choices.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-
1. Make the program to perform various operations on entered number
and give choice to user to perform specific operation.

Viva Questions:-
1. Explain the syntax of case statement used in unix shell.
2. How the case can be used in pattern matching.
3. How the wild cards can be used with case statement.
4. How to denote default statement in case.
5. How to break the choices in case statement of unix.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 13
Unix & Shell Programming Lab Manual

EXPERIMENT NO.13

Aim: - Write a shell script to find the mode of a file in a directory.

Objective: - After this program student will be able to know:


1. How to make the program to check the modes of entered file.
2. What are the different commands to check the file modes.
3. How to change the modes of files.

Experiment:-
Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-
1. Make the similar program to change the modes of file.
2. Make the program to count the size of file with no. Of words, characters
and lines in to file.

Viva Questions:-
1. What are the different modes of files.
2. How the modes can be changed.
3. Explain different techniques for changing the modes of files.
4. What is by default mode of file.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 14
Unix & Shell Programming Lab Manual

EXPERIMENT NO.14

Aim: - Write a shell script to copy the source file to the target file.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or decrement
statement.
3. How to use expression for increment/ decrement.

Experiment:-
Follow the step by step task given below to perform the experiment:
1. Create a new file for writing the shell program.
2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. What are the different conditions to check the existence of file.
2. How to copy the file.
3. Explain the syntax of if else statement in shell programming.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 15
Unix & Shell Programming Lab Manual

EXPERIMENT NO.15

Aim: Write a shell script to accept the name, grade, and basic salary from
the user. Write the details into a file called employee, separating the
fields with a colon (,) continue the process till the user wants.

Objective: - After the execution of the program student will be able:


1. To know the use of case statement in unix shell programming.
2. To know how we can give multiple choice options to user.
3. To know how we will provide default option.
4. To know the execution of various commands with choices.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-
1. Make the program to perform various operations on entered number
and give choice to user to perform specific operation.

Viva Questions:-
1. Explain the syntax of case statement used in unix shell.
2. How the case can be used in pattern matching.
3. How the wild cards can be used with case statement.
4. How to denote default statement in case.
5. How to break the choices in case statement of unix.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 16
Unix & Shell Programming Lab Manual

EXPERIMENT NO.16

Aim: - A five digit number is input through the keyboard. Write a shell
script to calculate the sum of its digits.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. Explain the syntax of until loop used in UNIX shell?
2. How the loop can be used in shell program?
3. What are the different operators used in shell scripting?
4. How the condition can be checked using operators?

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 17
Unix & Shell Programming Lab Manual

EXPERIMENT NO.17

Aim: - Write a shell script to find the number of files in a directory.

Objective: - After this program you should be able to


1. Know the importance of meta characters.
2. Know the working of ls and wc commands.
3. Know the use of for loop statement in shell programming.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-
1. Try the same program first for default directory.
2. Try to find the no. of files in directory entered by users choice.

Viva Questions:-

1. How we can retrieve line, words and characters from a file.


2. How to display file and subdirectories inside pwd.
3. Explain the iterative statement used in shell program.
4. What are the different options used with ls and wc command.

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 18
Unix & Shell Programming Lab Manual

EXPERIMENT NO.18

Aim: Write a shell script to check if a particular user has logged in or


not. If not, continue the loop till he/she logins. Once the required user
logins, display a message.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

1. Explain use of until loop in shell program?


2. Explain the iterative statement used in shell program?
3. What are the different options used for which user has logged in
command?
4. What is the importance of Meta characters?

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 19
Unix & Shell Programming Lab Manual

EXPERIMENT NO.19

Aim: - Write a shell script to check whether a file is existing or not.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output:-

Lab- Assignment:-

Viva Questions:-
1. Explain the use of if-else loop in shell program?
2. Explain the command used to check whether given file exists or not?
3. What are the different conditions to check the existence of file?
4. What is the difference between UNIX and c loop statement?
5. How to get the infinite loop condition?

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 20
Unix & Shell Programming Lab Manual
EXPERIMENT NO.20

Aim: - Write a shell script to calculate the total salary payable to all the
employees from the employee file. The salary should be taken from the
8th field of the employee file.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. Explain the syntax of various loops in shell program?
2. How to get the infinite loop condition?
3. What are the different operators used in shell scripting?
4. How the condition can be checked using operators?
5. How the arithmetic operations can be used in shell program?

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 21
Unix & Shell Programming Lab Manual
EXPERIMENT NO.21

Aim:- Write a shell script that accepts two integer as its arguments
and compute the value of first number raised to the power of second
number.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. Explain the syntax of various loops in shell program?
2. How to get the infinite loop condition?
3. What are the different operators used in shell scripting?
4. How the condition can be checked using operators?
5. How the arithmetic operations can be used in shell program?

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 22
Unix & Shell Programming Lab Manual

EXPERIMENT NO.22

Aim:- Write a shell script that accepts an integer as its arguments and
find whether the number is prime or not.

Objective: - After this program student will be able to know


1. The use of until loop in shell program.
2. Where to put the initialization, condition and increment or
decrement statement.
3. How to use expression for increment/ decrement.

Experiment:-

Follow the step by step task given below to perform the experiment:

1. Create a new file for writing the shell program.


2. Write the step by step command to complete the task in shell
programming.
3. Save and Exit from the vi editor.
4. Change the permission of file into execute mode.
5. Execute the file and note the output.

Output: - Note down the output of program in left side of your file.

Lab- Assignment:-

Viva Questions:-
1. Explain the syntax of various loops in shell program?
2. How to get the infinite loop condition?
3. What are the different operators used in shell scripting?
4. How the condition can be checked using operators?
5. How the arithmetic operations can be used in shell program?

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla
Page 23
Unix & Shell Programming Lab Manual

Department of Computer Science & Engineering


UNIX & SHELL Programming Prepared By R. N. Shukla

Page 24

EXPERIMENT NO. 23

Aim: Write a shell script to monitor CPU and memory usage and log the data to a file.

Objective:
1. Learn to use `top`, `vmstat`, `free`, and `uptime` commands.
2. Automate system resource logging.
3. Understand real-time resource monitoring.

Experiment Steps:
- Create a shell script using `vi` or any editor.
- Use commands like `top -b -n1 | head -10` and `free -m`.
- Redirect the output to a file with timestamp.

Lab Assignment:
- Schedule the script using `cron` to run every minute.

Viva Questions:
1. How do you check memory usage in Linux?
2. What is `cron` and how is it used?

------------------------------------------------------------

EXPERIMENT NO. 24

Aim: Write a shell script to backup a given directory into a compressed `.[Link]` file.

Objective:
1. Understand file archiving using `tar`.
2. Perform scheduled and conditional backups.

Experiment Steps:
- Use `tar -czvf [Link] /path/to/directory`
- Include date in the filename using `date +%Y-%m-%d`

Lab Assignment:
- Schedule automated daily backup using `cron`.

Viva Questions:
1. What is the use of `tar` command?
2. How to include dynamic values like date in filenames?

------------------------------------------------------------

EXPERIMENT NO. 25

Aim: Write a shell script to send an email alert when disk usage crosses a threshold.

Objective:
1. Use `df -h` and `awk` to check disk usage.
2. Integrate shell scripting with email utilities (`mail`, `mailx`).

Experiment Steps:
- Set threshold, e.g., 80%
- Parse disk usage and trigger `mail` command

Lab Assignment:
- Modify the script to alert for multiple mount points.

You might also like