LINUX Operating
System
and
Shell Scripting
What is Kernel?
What is kernel is a heart of Linux operating system .
It managers resources of Linux operating system ,resources means
facilities available in Linux .
for example facilities to store data, print data on printer ,memory file
management etc. ,who will use this resources for how long and
when.
It runs your programs or set up to execute binary files and
intermediate between the computer hardware and various programs/
applications/shell.
Kernel Con.
USER REQUEST
SHELL APPLICATI
ON
LINUX KERNEL
COMPUTER HARDWARE
Some LINUX commands
Basic Linux Terminal Commands
S.No. Linux Commands Functions
1 Is Displays information about files in the current directory.
2 pwd Displays the current working directory.
3 mkdir Creates a directory.
4 cd To navigate between different folders.
5 rmdir Removes empty directories from the directory lists.
6 cp Moves files from one directory to another.
Some LINUX commands
Basic Linux Terminal Commands
S.No. Linux Commands Functions
7 mv Rename and Replace the files
8 rm Delete files
9 uname Command to get basic information about the OS
10 locate Find a file in the database.
11 touch Create empty files
12 ln Create shortcuts to other files
13 cat Display file contents on terminal
14 clear Clear terminal
15 ps Display the processes in teminal
Some LINUX commands
Basic Linux Terminal Commands
S.No. Linux Commands Functions
16 man Access manual for all Linux commands
17 grep Search for a specific string in an output
18 echo Display active processes on the terminal
19 wget download files from the internet
20 whoami Create or update passwords for existing users
21 sort sort the file content
22 cal View Calendar in terminal
What is a shell ?
Shell is a program that text commands type by the user and cause
the operating system to run those commands.
The shell is a program that acts as as the interface between you and
the Linux system allowing you to enter commands for the operating
system to execute shell.
Shell accept your instructions or command in English and translate
it into a computer native binary language.
Why to write Shell script?
There are two ways of writing shell programs::
You can type a sequence of commands and allow to shell to execute
them interactively.
You can store those command in a file that you can then invoke as a
program ( shell script).
A shell script is similar to batch file in MS DOS but much more
powerful .
Shell script can take input from user file and output them on
screen ,useful to create, commands save lots of time to automate
some task of day today life.
How to create Shell Script?
Create a script as stores the plane text file generally one command per line.
The vi editor (visual editor) is the default text editor in many Linux
distributions. The vi editor is an advanced text editor with many features
To open the vi editor in Linux
Click on Applications System Tools > Terminal Then Type vi with file name
(Optional) as shown in screenshot
The Vi editor works in two main modes, one for editing text and the other for
giving commands. To switch between the two modes we can use the I and Esc
keys. The program opens in the Command mode, which is used for cursor
movements, delete, cut copy, paste, and saving changes.
1. The Insert mode is what we will work in most of the time. We use it to make
changes in an open file. Enter the Insert mode by pressing the I key. Newer Vi
versions will display the word "INSERT on the bottom line while we are in Insert
mode
2. Press the Esc key to switch Vi back to Command mode. As soon as we hit the
Esc key the text "INSERT on the bottom line disappears.
3. We have to save changes to the open file from the Command mode. Press Shift-
ZZ is save
4. If we make a mistake when saving a file, such as pressing Ctrl-ZZ or closing Vi
before saving the file, you'll end up with a swap file. The swap file is not readable
but can be recovered by typing a command like this at the $ prompt and pressing
Enter.
Once script is created you need to set executable
permission on script.
Why without executable permission running a script is
almost impossible beside executable.
Permission script must have a great permission syntax to
set up executive permission.
$chmod 755 script_file_name
$chmod +x script_file_name
permission
r read=4
w write=2
x execute=1
owner group
others
Shell variables
It is also called as user define variables, created and maintain by
user.
This type of variables define in lower letters .
User define variables used following syntax
variable_name = value
Rules for naming variables
Variable name must begin with alphanumeric character and or
underscore character followed by one or more alphanumerical
character .
Don't use spaces on other side of the equal sign when assigning a
value to variable.
To print or access value of user define variables use following syntax:
$variable_name
The read statement used to get input data from user from keyboard
and store data to variable.
Syntax is as follow:
read variable1 ,variable2,………variable n
Control Statements
IF statement.
CASE statement.
IF statement syntax:
if ((condition)) then
statements
else
statements
fi
Control Statements
case statement syntax:
case EXPRESSION in
Pattern_Case_1) STATEMENTS
;;
Pattern_Case_2) STATEMENTS
;;
Pattern_Case_N) STATEMENTS
;;
*) STATEMENTS
;;
esac
Looping statements in Shell script
There are total 3 looping statements that can be used in shell programming
while statement
for statement
until statement
To alter the flow of loop statements, two commands are used they are,
break
continue
while statement syntax
while <condition>
do
<command 1>
<command 2>
<etc>
done
for statement syntax
for <var> in <value1 value2 ... valuen>
do
<command 1>
<command 2>
<etc>
done
until statement syntax
until <condition>
do
<command 1>
<command 2>
<etc>
done
Implementing for loop with break statement
#Start of for loop
for a in 1 2 3 4 5 6 7 8 9 10
do
# if a is equal to 5 break the loop
if [ $a == 5 ]
then
break
fi
# Print the value
echo "Iteration no $a"
done
Practical session schedule
Date Batches
16 DEC 2023 B1 (Rollno 1- 45)
19 DEC 2023 B2(Rollno 46-89)