2bfinalOS 2nd year
2bfinalOS 2nd year
AIM :
INTRODUCTION :
A shell is a special user program that provides an interface for the user to
use operating system services. Shell accepts human-readable commands
from users and converts them into something which the kernel can
understand. It is a command language interpreter that executes commands
read from input devices such as keyboards or from files. The shell gets
started when the user logs in or starts the terminal.
The shbang line The "shbang" line is the very first line of the script
and lets the kernel know what shell will be interpreting the lines in
the script.
The shbang line consists of a #! followed by the full pathname to
the shell, and can be followed by options to control the behavior of
the shell.
EXAMPLE
#!/bin/sh
Comments
Comments are descriptive material preceded by a # sign. They are
in effect until the end of a line and can be started anywhere on the
line.
EXAMPLE
# this text is not
# interpreted by the shell
03. SHELL VARIABLES :
Shell variables change during the execution of the program .The C
Shell offers a command "Set" to assign a value to a variable.
For example:
% set myname= Fred
% set myname = "Fred Bloggs"
% set age=20
A $ sign operator is used to recall the variable values.
For example:
% echo $myname will display Fred Bloggs on the screen
A @ sign can be used to assign the integer constant values.
For example:
%@myage=20
%@age1=10
%@age2=20
%@age=$age1+$age2
%echo $age
Local variables - Local variables are in scope for the current shell.
When a script ends, they are no longer available; i.e., they go out
of scope. Local variables are set and assigned values.
EXAMPLE
variable_name=value
name="John Doe"
x=5
Rules : -
1.A variable name is any combination of alphabets, digits and an
underscore (‘-‘);
2.No commas or blanks are allowed within a variable name.
3.The first character of a variable name must either be an alphabet
or an
underscore.
4.Variables names should be of any reasonable length.
5.Variables names are case sensitive . That is , Name, NAME,
name,NAme, are all different variables.
Equality:
= string
!= string
-eq number
-ne number
Logical:
-a and
-o or
! not
Logical:
AND &&
OR ||
Relational:
-gt greater than
-ge greater than, equal to
-lt less than
-le less than, equal to
Arithmetic :
+, -, \*, /, %
05.READ Statement :
To get the input from the user.
Syntax :
read x y
(no need of commas between variables)
EXAMPLE
echo "What is your name?"
read name
read name1,name2…
6. CONDITIONAL STATEMENTS :
The if construct is followed by a command. If an expression is to be
tested, it is enclosed in square brackets. The then keyword is
placed after the closing parenthesis. An if must end with a fi.
Syntax :
1.if
This is used to check a condition and if it satisfies the condition if
then does the next action , if not it goes to the else part.
2.if...else
Syntax :
If cp $ source $ target
Then
Echo File copied successfully
Else
Echo Failed to copy the file.
3.nested if
here sequence of condition are checked and the corresponding
performed accordingly.
Syntax :
if condition
then
command
if condition
then
command
else
command
fi
fi
EXAMPLE
The if construct is:
if command
then
block of statements
fi
--------------------------------------
if [ expression ]
then
block of statements
fi
—--------------------------
if [ expression ]
then
block of statements
elif [ expression ]
then
block of statements
elif [ expression ]
then
block of statements
else
block of statements
fi
—--------------------------
07. LOOPS
There are three types of loops: while, until and for. The while loop
is followed by a command or an expression enclosed in square
brackets, a do keyword, a block of statements, and terminated with
the done keyword. As long as the expression is true, the body of
statements between do and done will be executed.
The until loop is just like the while loop, except the body of the loop
will be executed as long as the expression is false.
The for loop used to iterate through a list of words, processing a
word and then shifting it off, to process the next word. When all
words have been shifted from the list, it ends. The for loop is
followed by a variable name, the in keyword, and a list of words
then a block of statements, and terminates with the done keyword.
The loop control commands break and continue.
EXAMPLE
while command
do
block of statements
done
------------
while [ expression ]
do
block of statements
done
until command for variables in word1 word2 word3 ...
do do
block of statements block of statements
done done
------------
until [ expression ]
do
block of statements
done
------------
until control command
do
commands
done
09. ARRAYS
(positional parameters) The Bourne shell does support an array,
but a word list can be created by using positional parameters. A list
of words follows the built-in set command, and the words are
accessed by position. Up to nine positions are allowed.
The built-in shift command shifts off the first word on the left-hand
side of the list.
The individual words are accessed by position values starting at 1.
EXAMPLE
set word1 word2 word3
echo $1 $2 $3 Displays word1, word2, and word3
set apples peaches plums
shift Shifts off apples
echo $1 Displays first element of the list
echo $2 Displays second element of the list
echo $* Displays all elements of the list
EXAMPLE
-d File is a directory
-f File exists and is not a directory
–r Current user can read the file
–s File is of nonzero size
–w Current user can write to the file
–x Current user can execute the file
#!/bin/sh
1 if [ –f file ]
then
echo file exists
Fi
2 if [ –d file ]
then
echo file is a directory
fi
3 if [ -s file ]
then
echo file is not of zero length
fi
4 if [ -r file -a -w file ]
then
echo file is readable and writable
fi
Output:
Enter three numbers:
12 45 30
45 is greatest
Output:
Enter two numbers:
10 2
Addition: 12
Subtraction: 8
Multiplication: 20
Division: 5
Output:
Enter a number:
5
Sum is 15
Result: