OS Lab Journal 3-007
OS Lab Journal 3-007
Objective(s) :
To understand LINUX shell programming variables, wildcards, expressions and control
structures.
Lab Tasks :
Solution:
Linux Variables
Lab Manual Operating System | CSL-320
Output
Task 2: Write a program to calculate the addition, subtraction, multiplication and division of
numbers.
Solution:
Calculator Program
Lab Manual Operating System | CSL-320
Output
Task 3.1: Write a program that compares two numbers if a is greater than b it displays “a is
Solution:
If else program
Lab Manual Operating System | CSL-320
Task 3.2: Write a program that compares two numbers check whether the numbers are equal, a
Solution:
If else program
Lab Manual Operating System | CSL-320
Task 4 : Write a program using “case” that inputs a fruit from the user and displays “Apple pie”
on the input of apple, “I like banana” on the input of banana and “New Zealand famous for kiwi”
Solution:
Case program
Output
Lab Manual Operating System | CSL-320
Total 20 Signature
Note : Attempt all tasks and get them checked by your Lab. Instructor
Lab Manual Operating System | CSL-320
Objective(s):
To understand LINUX shell programming variables, wildcards, expressions and control
structures.
Tool(s) used:
Ubuntu, VIM Editor
Introduction
Shell programming is a group of commands grouped together under single filename. After logging
onto the system a prompt for input appears which is generated by a Command String Interpreter
program called the shell. The shell interprets the input, takes appropriate action, and finally prompts
for more input. The shell can be used either interactively - enter commands at the command prompt,
or as an interpreter to execute a shell script. Shell scripts are dynamically interpreted, NOT
compiled.
Common Shells
C-Shell - csh
The default on teaching systems Good for interactive systems Inferior programmable
features.
Bourne Shell
bash or sh - also restricted shell – bsh (The Bourne Again Shell) It was written by Steve
Bourne. Over the years the original Bourne Shell has been expanded, but it remains the basic
shell provided in many commercial versions of Linux.
Korn Shell
It was written by David Korn This shell extended many features of Bourne Again Shell
and added many new features.
The TC Shell performs the same functions as Bourne Again Shell. It is an interactive
command line interpreter as well as for high level programming languages.
Shell Keywords
echo, read, if fi, else, case, esac, for , while , do , done, until , set, unset, readonly, shift,
export, break, continue, exit, return, trap , wait, eval ,exec, ulimit , umask.
The “shbang” line is the very first line of the script and lets the kernel know what shall
will be interpreting the lines in the script. The shbang line consists of #! Followed by the
full pathname to the shell, and can be followed by options to control the behavior of
shell.
Example
#!/bin/bash
Comments
Comments are descriptive material preceded by a # sign. They are ineffect until the end
of a line and can be started anywhere on the line.
Example
#!bin/bash
#YOUR FIRST HELLO WORLD PROGRAM
echo ‘Hello World!’
command
$ chmod u + x hello.sh
$ ./hello.sh
Shell Variables
Rules
Shell Variables
OUTPUT
ECHO Statement
Similar to the output statement. To print output to the screen, the echo command is used.
Syntax: Echo “String” (or) echo $ b (for variable).
Example: echo "What is your name?"
READ Statement
Reading user input: The read command takes a line of input from the user and assigns
it to a variable(s) on the right-hand side. The read command can accept multiple
variable names. Each variable will be assigned a word.
ii) #!/bin/bash
#Input from user
echo "Enter your name"
read NAME
echo "Enter your age"
read AGE
echo "Enter your enrollment"
read ENROLLMENT
echo "Hello $NAME, Your age is : $AGE Your enrollment is :
$ENROLLMENT"
OUTPUT
iii) #!/bin/bash
#readonly Variables
echo "Readonly Variables"
Name=”David”
readonly Name
Name=’John’
OUTPUT
iv) #!/bin/bash
echo "Unset Variables : "
Name=”John”
unset Name
echo $Name
OUTPUT
Wildcards
There are some characters that are evaluated by the shell in a special way. They are
called shell meta characters or “Wildcards. These characters are neither number nor
letters.
Example
*,?,[ ],$
The following table shows a number of special variables that can be used in shell scripts.
Variable Description
$0 The filename of the current script
$n These variables correspond to the arguments with which a script is
invoked.
$# The number of arguments supplied to a script.
$* All arguments are double quoted. If a script receives two
arguments $* is equivalent to $1,$2
$@ All arguments are individually double quoted, equivalent to $1,$2
$$ Shows the number of current shell.
$! The process number of last background command.
v) #!/bin/bash
#Unix Special Commands
echo "File Name = $0"
echo "First Parameter = $1"
echo "Second Parameter = $2"
echo "Quoted Values = $@"
echo "Quoted Values = $*"
echo "Total number of parameters = $#"
OUTPUT
vi) #!/bin/bash
#Special Commands
echo "File Name = $0"
echo "First Parameter = $1"
echo "Second Parameter = $2"
echo "Quoted Values = $@"
echo "Quoted Values = $*"
echo "Total number of parameters = $#"
echo $?
OUTPUT
EXPRESSION Command
Arithmetic Operations
To perform all arithmetic operations. The Bourne shell does not support arithmetic.
LINUX/Linux commands must be used to perform calculations.
Syntax:
var=$(expr $value1 + $value2)
Task 2 Write a program for calculating addition, subtraction, multiplication and division of two
numbers.
Program
OUTPUT
Operators
The Bourne shell uses the built-in test command operators to test numbers and strings.
Example
Equality:
= string
!= string
-eq number
-ne number
Logical:
-a and
-o or
! not
Relational:
-gt greater than
-ge greater than, equal to
-lt less than
-le less than, equal to
Arithmetic:
+, -, \*, /, %
Control Structures
Unix Shell supports conditional statements which are used to perform different actions
based on different conditions. Two decision making statements are mentioned below:
if…else statements
case…esac statements
if..fi Statements
Syntax:
if [ expression ]
then
if..else...fi Statements
The if…else…fi statements is the next form of control statements that allow shell to
execute statements in more controlled way and making decision in two ways.
Syntax
if [ expression ]
then
Statement(s) to be executed if true
else
Statement(s) to be executed if true
fi
Task 3.1 Write a program that compares two numbers if ‘a’ is greater than ‘b’ it displays “a is
greater than b”, otherwise it displays that ‘a is not equal to b’.
Code
OUTPUT
if…elif…elseif…fi Statement
Syntax
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi
Task 3.2 Write a program that compares two numbers check whether the numbers are equal, a is
greater than b or a is less than b.
Code
OUTPUT
case…easc Statement
You can handle multiple if…elif statements to perform a multiway branch. However, this
is not the best solution, especially when all the branches depend the value of single
variable.
Syntax
case “word” in
“pattern1”)
Statement(s) to be executed if pattern1 matches;;
“pattern2”)
Statement(s) to be executed if pattern1 matches;;
“pattern3”)
Statement(s) to be executed if pattern1 matches;;
esac
Task 4 Write a program using “case” that inputs a fruit from the user and displays “Apple pie”
on the input of apple, “I like banana” on the input of banana and “New Zealand famous
for kiwi” on the input of kiwi.
OUTPUT