Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

Lab04 - Introduction To Shell Programming

This 3-page document describes a lab assignment on Linux and shell programming. The objective is to familiarize students with Ubuntu and shell programming. Students will work in groups of two. Tasks include modifying a calculator program to add factorial and even/odd checking functions, using sed to replace strings in files, and using bash scripts and command lines to process text files by making text lowercase and removing XML/HTML tags. Students are instructed to submit one lab report per group containing the code and screenshots. The lab is graded out of 10 marks.

Uploaded by

Waseem Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Lab04 - Introduction To Shell Programming

This 3-page document describes a lab assignment on Linux and shell programming. The objective is to familiarize students with Ubuntu and shell programming. Students will work in groups of two. Tasks include modifying a calculator program to add factorial and even/odd checking functions, using sed to replace strings in files, and using bash scripts and command lines to process text files by making text lowercase and removing XML/HTML tags. Students are instructed to submit one lab report per group containing the code and screenshots. The lab is graded out of 10 marks.

Uploaded by

Waseem Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Computer Science

CS-330: Operating Systems

LAB-04: Introduction to Linux and Shell Programming (part2)

CLO1: (Explain & summarize OS Services and Abstractions)

Time:9.00 AM to 12.00 PM and 2.30 PM – 4:50 PM

Cs330: Operating systems Page 1


Lab 04: Introduction to Linux and Shell Programming(part2)

Introduction

Practice the basics of Shell programming in Linux (Ubuntu).

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)

Working with Linux system:

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.

How to run a shell script in Linux?

Type these commands on terminal:-

1): gedit myscript.sh

2): chmod u=rwx myscript.sh

3): ./myscript.sh

How to display on console and take input from user:


echo "Enter Name: "
read a;

Cs330: Operating systems Page 2


echo "Enter Batch Number: "
read b

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

How to use in code?


for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

2):- While loop

Syntax:-
while [ condition ]

do
command1
command2
command3
..
....
Done

How to use in code?


while [$x -le 5]
do
echo "Welcome $x times :"
x=`expr $x + 1`
echo "value of x is $x :"
done

Case…esac statement in shell:


These are the statements in shell which replicates the switch case statements in c/c++
language.Syntex is as follows:-

Cs330: Operating systems Page 3


#!/bin/sh

echo "Enter a number between 1 and 10. "


read NUM

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

How to use in code?


var=1
if [ $var -le 0 ];
then
echo "$var is positive"
elif [ $var -lt 0 ]
then
echo "$var is negitive"
elif [ $var -eq 0 ]

Cs330: Operating systems Page 4


then
echo "$var is zero"
else
echo "oops go to hell"
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.

1):- To find factorial of a natural number.


Your code should take a natural number as input from the user and then print the value of its
factorial.
2): - To check either a number is even or odd.
Your code should take a number and check either its odd or even and should print the
information accordingly.

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.

Note :- Please explore the command Sed

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.

This processing includes following steps:

1. make all alphabets small case

2. remove all xml/html tags

Cs330: Operating systems Page 5


HINT:-You can use sed command.

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

NUST has a total of 9,123 students,

this is some more TEXT, </text>

After this script is run,


The contents of file 1-modified.txt

this is some text, this text will be modified by the program.

nust is a university in islamabad and there is a campus in it called seecs, which has around 2134
students

nust has a total of 9123 students,

this is some more text,

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).

Upload the document containing codes as well as screenshots on LMS .

This lab is graded. Min marks: 0. Max marks: 10.

Cs330: Operating systems Page 6


Cs330: Operating systems Page 7

You might also like