Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Hidaya Institute of

Science & Technology

www.histpk.org
A Division of Hidaya Trust, Pakistan

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Kernel

Kernel is the core component of Linux OS.

It manages resource of Linux OS. Resources means facilities
available in Linux. For example, Facility to store data, print data on
printer, memory, file management etc.

Kernel decides who will use this resource, for how long and when.

The kernel acts as an intermediary between the computer hardware
and various programs/application/shell.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


It performance following task:

Process management.

I/O management.

Memory management.

Device management.

File management.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Kernel
• uname -a
• Print all information about system.
• uname -s
• Print kernel name.
• uname -v
• Print kernel release.
• uname -r
• Print kernel version.
• uname -n
• Print host name.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Shell
• Shell is the interpreter between User and Linux Kernel.
• Shell is a user program or it's environment provided for
user interaction.
• Shell is an command language interpreter that executes
commands read from the standard input device
(keyboard) or from a file.
• Shell is not a part of system kernel, but uses the system
kernel to execute programs.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Shell
• chsh --list
• To view shells of Linux on our system.
• cat /etc/shells
• To view shells of Linux on our system.
• echo $SHELL
• To view current shell of Linux on our system.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


How to use Shell
• To use a shell, You start to use your shell as soon
as you log into your system, you have to simply
type commands.
• Any shell reads command from user via Keyboard
or Mouse and tells Linux OS what users want to
do.
• If we are giving commands from keyboard it is
called command line interface.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Shell Scrip
• Normally shells are interactive. It means shell accept
command from you (via keyboard) and execute them. But
if you use command one by one (sequence of number of
commands) , you can store this sequence of command to
a text file and tell the shell to execute this text file, This is
know as shell script.
• Shell script is a series of command written in plain text
file.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Why to write shell script?
• Shell script can take input from user, file and
output them on screen.
• Useful to create our own commands.
• Save lots of time.
• To automate some task of day today life.
• System Administration part can be also automated.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Shebang
• In computing, a shebang is the character sequence
consisting of the characters number sign and
exclamation mark that is "#!".
#!
• The shebang #! syntax used in scripts to indicate an
interpreter for execution.
• The specified interpreter program/Shell is responsible
to run that script or a compatible shell.

#!/bin/bash

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


How to write Shell Script
• Use any editor like vi, nano or gedit to write shell
script.
#!/bin/bash
Clear
echo “Hidaya Institute of Science and Technology Jamshoro”

• After writing shell script set execute permission


for your script as follows.
• chmod a+x script-file
• chmod 755 script-file
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
How to execute Shell Script
• There are several ways to execute script file.

• ./script-file
• sh script-file
• bash script-file
• /bin/sh script-file
• /bin/bash script-file

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Shell Script
• We execute script file where it is located only.
• But if we move our script file to /bin, so we can
execute it at any location like normal shell command.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Quotes
• “” Double quotes
• Enclosed double quotes print as a string and also call the values of
variable by using $ dollar sign with variable and execute the shell
commands enclosed ` ` back quotes.

• ‘’ Single quotes
• Enclosed in single quotes remain unchanged.

• `` Back quotes
• Back quotes used to execute shell commands in double quotes.

• ; Semi colon
• Semi colon is used to command terminator.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Assigning value to variable
• Numeric values
• var=20
• Sting values #!/bin/bash
var1=10
• str=“Hidaya Trust” var2=20
str=“HIST Jamshoro”
echo “The value of string is: $str”
echo “The value of var1 is: $var1”
echo “The value of var2 is: $var2”
((sum=$var1+$var2)) # OR
# let sum=$var1+$var2
echo “The sum of $var1 and $var2 is $sum”

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Arithmetic Comparison Operators
• Equal -eq or ==
• Less than -lt or <
• Greater than -gt or >
• Less than or equal -le or <=
• Greater than or equal -ge or >=
• Not equal to -e or !=

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


String Comparison Operators

• Equal ==
• Less than <
• Greater than >
• Not equal to !=

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


File Test Operators
• -f
• If file exists.

• -d #!/bin/bash
• If directory exists. echo “Enter file Name”
• -r read file

if [ -f $file ]; then
If file is read only.
echo "$fiel is exist..."
• -s else
• If non zero file exists. echo "$file does not exist..."
• -w fi
• If file is writeable.

• -x
• If file executable.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Read Command
• Used to take input from user via key board.
• Syntax

#!/bin/bash
clear
echo -n “Enter your name: ”
read name
echo “Your Name is $name”

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Decision Making
In Computing users may have a number of
situations, where they may have to change the
order of statements based on certain
conditions. This is decision making.

In computer science, conditional statements


and conditional expressions are features of a
programming language which perform different
computations or actions depending on
conditions to true or false.
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
If Statement

The if statement is a powerful


decision making statement and is
used to control the flow of execution
of statements.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


If Statement
• Syntax

if [ expression ]
then
Statement(s) to be executed if expression is
true
else
Statement(s) to be executed if expression is
not true
fi

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


If Statement

#!/bin/bash
a=20
b=50
if [ $a = $b ]; then
echo “$a and $b are equal”
else
echo “$a and $b are not equal”
fi

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


Nested If Statments

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
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Nested If Conditions
#!#!/bin/bash #!/bin/bash
a=100 OR a=20
b=100 b=50
if [ $a -gt $b ]; then if [ $a -gt $b ]; then
echo "$a is greater than $b" echo “$a is greater than $b”
else elif [ $a -lt $b ]; then
if [ $a -lt $b ]; then echo “$a is less than $b”
echo "$a is less than $b" else
else echo “$a and $b are equal”
echo "$a and $b are equal" fi
fi
fi
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Logical Operators
• || OR operator #!/bin/bash
clear
num1=1000
num2=2000
num3=3000
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then
• && AND operator echo "$num1 is Greater than $num2 and $num3"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
echo "$num2 is Greater than $num1 and $num3"
else
echo "$num3 is Greater than $num1 and $num2"
fi

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


The End

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org

You might also like