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

Lab03 - Introduction To Shell Programming

This document provides details about Lab 03 for the CS-330 Operating Systems course. The lab introduces students to shell programming in Linux (Ubuntu). [The lab objectives are to familiarize students with the Ubuntu operating system and teach shell programming basics. Students will practice solving more complex problems in Linux using shell scripts.] Students will complete 4 tasks - writing scripts to get user input, print multiplication tables, create a basic calculator, and copy/analyze files. They will submit lab codes and screenshots in a document for grading.

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)
52 views

Lab03 - Introduction To Shell Programming

This document provides details about Lab 03 for the CS-330 Operating Systems course. The lab introduces students to shell programming in Linux (Ubuntu). [The lab objectives are to familiarize students with the Ubuntu operating system and teach shell programming basics. Students will practice solving more complex problems in Linux using shell scripts.] Students will complete 4 tasks - writing scripts to get user input, print multiplication tables, create a basic calculator, and copy/analyze files. They will submit lab codes and screenshots in a document for grading.

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/ 6

Department of Computer Science

CS-330: Operating Systems

LAB-03: Introduction to Linux and Shell Programming

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 03: Introduction to Linux and Shell Programming

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: m

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 # 0

Write a shell script to input name and his batch number from the student and display this
information on terminal.

Task # 1

Write a shell script to print the multiplication table for the given number. You can take that
number as input from the user.
Note:- the expression calculation (explained in demo) looks like this:-
x=`expr $x + 1`

Task # 2

Write a shell script to make a simple calculator which should be capable of performing basic
calculations. Also add a feature of repeated inputs from the user.

Task # 3

Write a shell script that copies multiple files into a directory.

Note :- Please explore the command cp.

Task # 4

Write a shell script to find no. of lines and words in a given file. You can take file name as
input from the user.
Note :- Please explore the command wc.

Deliverables
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 5


Cs330: Operating systems Page 6

You might also like