Lab04 - Introduction To Shell Programming
Lab04 - Introduction To Shell Programming
Introduction
Objective
The Objective of this lab is to familiarize yourself with Ubuntu ad then proceed to shell
programming.
Description:
This lab has been designed to get even better insight of the shell programming. By the end of this
lab you‘ll be able to solve more complex problems in Linux using shell.
Tools/Software Requirement:
Linux (Ubuntu)
Introduction to Shell:-
shell is an command language interpreter that executes commands read from the standard
input device (keyboard) or from a file. It is not part of system kernel, but uses the system
kernel to execute programs, create files etc.
3): ./myscript.sh
Loops in shell:-
1):- For loop
Syntax:-
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done
Syntax:-
while [ condition ]
do
command1
command2
command3
..
....
Done
case $NUM in
1) echo "one" ;;
2) echo "two" ;;
3) echo "three" ;;
4) echo "four" ;;
5) echo "five" ;;
6) echo "six" ;;
7) echo "seven" ;;
8) echo "eight" ;;
9) echo "nine" ;;
10) echo "ten" ;;
*) echo "INVALID NUMBER!" ;;
esac
The 1,2,3 are the values that could possibly be contained in $NUM.The * character at the end
indicates how you’ll handle any other case just like “Default case” in switch case scenario of
if statements :-
if condition ; then
commands
fi
if else statements:-
if condition ; then
commands
else
commands
fi
Groups: - you may work in the groups of two. But please note that you have to submit one
lab report per group (with the names of both group members written on it).If you submit
multiple submissions per group it will be considered as plagiarism.
Task # 1
Reuse your code of simple calculator (from last lab) to add the following functionalities into it.
The initial version of calculator which you programed in last lab had the capability to perform
four fundamental arithmetic operations, now it should be capable of performing six operations.
Task # 2
Write a shell script which should replace all the instances of a particular string in a file by
another string. You may take the replacing string and string to be replaced as input from the user.
Task # 3
Using bash scripts and command line tools, implement the following functionality
your created script should be run like this: and it should process *.txt files in that directory.
Example:
contents of file 1.txt
<text> This is some Text, this text will be modified by the <br> Program. </br>
nust is a university in islamabad and there is a campus in it called seecs, which has around 2134
students
Deliverables
Note :- You may submit in groups of two students ,but just make sure to submit one file per group (don’t
forget to mention the names of group members in it).