0% found this document useful (0 votes)
2 views45 pages

Unit - 3 With Linux -Unix

This document provides an overview of shell programming in Linux, covering key concepts such as shell keywords, variables, and user-defined variables. It explains the structure and components of Linux, including the kernel, system library, and utilities, as well as the history and features of Linux as an open-source operating system. Additionally, it outlines the installation and booting procedures for Linux, specifically Ubuntu, and discusses the GNU project and GPL licensing.

Uploaded by

disecek477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views45 pages

Unit - 3 With Linux -Unix

This document provides an overview of shell programming in Linux, covering key concepts such as shell keywords, variables, and user-defined variables. It explains the structure and components of Linux, including the kernel, system library, and utilities, as well as the history and features of Linux as an open-source operating system. Additionally, it outlines the installation and booting procedures for Linux, specifically Ubuntu, and discusses the GNU project and GPL licensing.

Uploaded by

disecek477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Unit - 3

Shell programming, started with linux


introduction
Shell provides an interface to user through which user interact
with OS kernal.
Unix shell is both command language and programming language
interface.

Shell keywords :

Keywords is a word which is having specific meaning for


particular language.
Unix shell scripts are not having only collections of commands,
but also features of programming language like looping,
condition etc.
So, the words like if, for, while, until, case etc are used and are
having its own specific meaning and are called shell keywords.
Variables
“Variable is a location in memory which is used to
store the data”.
To access this value of variable, the name of variable
must be preceded by “$” sign.
variables

Pre defined User defined

System
Shell variables
variables
Shell variables
Shell commands are used to configure shell. The shell value
provides string valued variables. These variable names begin with a
letter and consists of letters, digits and underscore.
For example: echo $user
Where ‘$’ is a shell parameter and user is a variable.
Other shell parameters are:
$* : it is complete set of positional parameters as a single
string.
$@ : same as $*, but enclose in double quotes.
$? : the exit status (return value) of last command executed as a
decimal string.
$# : the number of positional parameters in decimal.
$$ : process number in this shell
$! : process number of last process running in background
$- : current shell flags, such as –x & -v
System variables
These variables are used to configure the environment during boot
sequence or after user login. So, these variables are also known as
environment variables.
The shell has 2 prompts stored in ps1 & ps2.
Ps1:
This is primary prompt string like $ or %. The shell uses primary
prompt when command is expected by the shell. For c changing the
values of this prompt, we can use
Ps1 = “@”
It is set the new prompt is displayed, we can back to $ prompts as
Ps1 = “$”
Ps2 :
It is secondary prompt. It is used when command entered on first line
was not able to be completed. Default prompt is >.
Example : echo $ps2
Path:
It searches path for a list of directories that contains commands.
Each time a command is executed by shell, a list of directories is
searched for executable file.
If path is not set, then current directory is searched by default.
$path consists of directory names seperated by : (colon).
For example :
Path = :user/ajay/bin:/bin
This specifies that the current directory is to be searched in this
order.

HOME:
It shows the path name of home directory. When user logs in the
system, unix normally places the user in home directory, and this
path is stored in variable “home”.
For example : echo $home
Logname:
It contains only login username, which is stored in passwd file.
Example : echo $logname

Mail :
It displays the absolute pathname of user’s mail box & determines where
all incoming mail addressed to the user is to be stored.
If specified file has been modified since last seen, the shell prints the
message “you have mail”.
This variable is set in “.profile” file, in user’s login directory.
Example: echo $mail

Mailcheck:
It contains interval between checks for new email. The default time is
600 seconds (6 minutes). It means, at every 600 seconds, it checks mail
file to check if there is a new mail.
IFS:
This variable contains internal field separator.
These are the characters that separate the parameter and
commands.
These separators includes space, tab and new line character.
Default separator is space and is represented by octal value 040.
/t for tab and /n for new line.
Shell:
This contains the pathname of login shell. Various shells
available in unix like Korn shell, Bash shell or C shell. User can
selects any one from them.
To change the default shell, the last field or file /etc/passwd has
to be modified.
The system administrator sets the login shell while creating user
account.
Example : echo $shell
USER DEFINED VARIABLES
The variable which is created by user is known as user defined
variables.
Rules for creating variables:
(1) variable name should not be same as pre-defined variables.
(2) must start with alphabet or underscore. We can use alpha
numeric values or underscore.
(3) unix is case sensitive. Means, sum & SUM are different
variables.
Command line:
$filename.sh $first $second $third……

Filename $1 $2 $3
(argument) (parameters)
Argument is a filename given by user in command line,
and is input to shell scripts.
Positional parameters are predefined memory variables
in shell scripts.
In above example, there are 3 positional parameters $1,
$2 and $3 are used to store user arguments.
The filename is stored in $0. when script is executed,
shell puts first argument in first positional parameter
@1, second in $2 and so on. Shell scripts can use these
variables.
Shell Script
Shell script is collection of commands. When some commands required frequently then we can make
collection of such commands in a single file, that will become our script file. We can create a file with
any name, but extension must be “.sh” for shell program (script) file.
Echo :
Echo command is used to display output on screen.
Example : echo “welcome to shell”
Echo $a
Read :
Read statement is shell’s internal tool for taking input from user. Read is used with variable. Read
command read from standard input device and store input in variable.
Example : read name
Echo hello $name
In above example, name is a variable. When we use read, $ is not required.
Example :
Echo read values for a, b and c
Read a b c
Echo $a $b $c
Decision statement
Unix shell provides decision making using if then else structure.
(1) simple if :
If [condition]
Then
Executable command
Fi
Here, condition is nothing but comparison between 2 values. Here, we can use
relational operators for comparing values.
(2) If … else
If [condition]
Then
executable commands
Else
executable commands
fi
(3) multilevel if ...elif … else (ladder if else)
If [condition 1]
Then
Command set 1
Elif [condition 2]
Then
Command set 2
Elif [condition 3]
Command set 3
Else
Command set 4
(4) nested if else
If [condition 1]
Then
◦ If [condition2]
◦ Then
◦ command set 1
◦ Else
◦ Command set 2
◦ Fi
Elif [condition 3]
◦ Then
◦ command set 3
◦ Else
◦ command set 4
◦ fi
fi
Test command
Test is a condition evaluation command. The test command evaluates
the expression and if the value is true, results 0 exit status. Otherwise,
non zero exit status returned.
It also returns non zero exit status if there are no arguments.
Example : program of negative & positive
Echo “Enter any number”
Read num
If test $num –lt 0
Then
◦ Echo “number is negative”
Else
◦ Echo “number is positive”
fi
Logical operators
Unix shell programming includes 3 logical operators.
(1) and ($$)
(2) or (||)
(3)not (!)
Not (!) operator is unary operator and it complements the
value of an expr. It changes the true value to false and false
value to true.
AND and OR are known as binary operators because it uses
2 expressions. In AND operator, results is true when both
expressions are true, Otherwise false.
OR operator returns true when any one expression is true.
Looping statements
Computer can repeat instruction again and again until
particular condition satisfies. A group of instructions is
executed repeatedly is called loop.
There are 3 types of loop:
(1) for loop
(2) while loop
(3) until loop
(1a) for loop:
Syntax :
For (( var=initial value; condition; incr/decr ))
Do
◦ code
Done
This loop is a self contained. It means, initial value,
termination condition and iterator (incr/decr) are given in
for structure itself.
Initializer is used to store initial value for looping variable.
The code is executed as condition tested given true. When
this condition becomes false, the control leaves block
statements.
(1b) for loop:
Syntax :
For variable in {list of values}
Do
◦ code
Done
This loop execute once for each item in the list until the
list is not finished.
(2) while loop :
Syntax :
While [ condition ]
Do
◦ Code
Done
This while loop works as an entry control loop.

(3) until loop :


Syntax :
Until [ condition ]
Do
◦ code
Done
The until loop works as an exit control loop.
HISTORY OF LINUX
Linux is a free open source unix os PCs which was
originally developed in 1991 by linus torvalds, when he
was a student of university of helsinki.
Open source of linux means, source code for linux
kernal is freely available. So that anyone can add
features and correct deficiencies.
Hence, collaborations of hundreds of developers
develops the different flavours of linux like redhat,
slackware, mandrake, coldera etc.
Components of linux
Linux has primarily 3 components
(1) kernal
(2) system library
(3) system utility
(1) kernal:
Kernal is core part of Linux and responsible for all
major activities of this OS. Kernal directly interacts
with hardware. It hides low level hardware details from
users.
(2) system library:
System library is a collection of special functions, using
which application programs or system programs can
access kernal’s features.

(3) system utility :


System utility programs are respoinsible to specialized
individual tasks.
GNU (Govt. of Nationality)
GNU (GNUs Not Unix) is a project, initiated and managed
by free software foundation to provide free softwares to
users, programmers and developers.
Linux distributed freely under GNU by free software
foundation, which is non – profit organization based in
Boston, USA.
GNU project was developed by richard stallman an activist in
free software foundation in 1984. in 1992, complete GNU
project was combined with linux and produced GNU/Linux
OS.
Linux is copyrighted, not public domain. Different programs
are developed under GNU project like X-windows, TEX etc.
gcc compiler for c & c++ is a part of GNU.
GPL (General Public License)
GPL is a short term for general public license and is a
type of license published by GNU project.
It was written by richard stallman to give rights to run,
study & modify the programs and redistributes the
improvement of programs to the public.
GPL provides the freedom to share and change free
software to make sure that the software is free for all
users.
Open source software
Linux is an open source OS. Source code is included in all its
distributions and is freely available on internet. So, no
institution controls linux.
Open source is a certification mark given by Open Source
Initiative (OSI). Open source software is protected by public
license. This prevents commercial companies from taking
control of open source software by additional modifications
of their own, copyrighting these changes and selling the
software as their own product. The most popular public
license is GNU GPL provided by free software foundation.
FREEWARE :
Freeware is programming that is offered no cost and is a
common class of small applications available for
downloading and used in most OS.
Architecture of LINUX
Linux system architecture is consists of following layers:
(1) hardware layer:
Hardware consists of all peripheral devices like RAM, HDD,
CPU etc.
(2) Kernal :
Core part of OS, interacts directly with hardware and provides
low level services to upper layer components.
(3) shell :
It takes commands from user and executes kaernal’s functions.
(4) utility :
Utility programs giving user most of functionalities of and OS.
Features of Linux
1. Multiuser
2. Multi tasking
3. Portable
4. Hierarchical file system
5. Shell
6. Hardware support
7. Networking connectivity
8. Open source :
Linux code is freely available and multiple teams works in
coloboration.
9. GUI:
The most powerful frameworks for working with graphical
applications in linux is X-windows, which is GUI based OS.
Installation & configuration of linux
(ubuntu)
First of all, download ubuntu from link :
www.ubuntu.com/download/ubuntu/download. write down
this download in your CD.
Before installation, it is safe to get backup you data. Ensure
that you have a network cable connected. Restart your
computer and boot from cd drive. Now follow the following
steps:
Step 1:
When cd will load up, you will be shown a screen. From
this screen, click on “install ubuntu”.
Step 2:
You will see “preparing to install ubuntu” screen. From
here, select “third party software.”
Step 3:
The next step you will see is “installation type”. Now, next
steps will depend upon whether you have existing window
installation or not. We split this into 3 different sub steps.
Step 3A:
For those who installing virtual machine, you will have 2
choices.
(1) “Erase the entire disk and use all of it for installation.”
ubuntu will automatically partition your disk and proceed
with installation,
(2) select “something else” and manually create partitions.
(covered in 3C).
If you are choosing first option, select the radio button and
then click continue, proceed to step 4.
Step 3B :
Those who have current windows installation or are
going to dual boot with another existing OS, will se a
screen. Which contains 3 options.
(1) you can choose first option and install ubuntu along
with your existing OS.
(2) you can replace your windows with ubuntu. This
means, allowing installer to format your current
partitions and automatically create new partition for
linux.
(3) select “something else” and create your own
partition.
Select your option and proceed to step 4.
Step 3C :
If you will select “something else”, it will go through to
manually create partitions. After completing this step,
move to step 4.
Step 4 :
As installation starts to copy required files to hard disk,
you will see a screen to select your location. It should
automatically find where you are. Just double check it
is correct and select continue.
Step 5 :
The next screen to appear will be keyboard layout,
from where select correct keyboard.
Step 6:
The next screen will be “who are you” screen where
you have to fill out your information.
Step 7:
Installation information screen will now appears as
ubuntu continuous the installation.
Step 8:
Now, installation has finished, and select “restart now”
and when requested, remove your installation CD, then
press enter to reboot.
Linux booting procedure
Booting is a process that starts operating system when user turns
on a computer system.
A boot sequence is a set of operations that computer performs
when it starts and load an operating system.
Follow stages are involved in linux booting process.
(1) POST
(2) BIOS
(3) BOOT LOADER
● MBR
● GRUB
(4) KERNAL
(5) INIT
(6) RUN LEVEL SCRIPT
(1) POST:
POST stands for “power on self test” which includes power
on for computer system.

(2) BIOS :
After power on, bios is the first thing which loads. when you
press power button of machine, CPU looks out into ROM for
further instruction.
ROM contains JUMP function in the form of instruction
which tells the CPU to bring up the BIOS.
BIOS determines all lists of bootable devices available in
system. Select your bootable device which can be hard disk,
CD/DVD ROM, floppy drive, USB flash dive.
OS tries to boot from hard disk where MBR contains primary
boot loader.
(3) BOOT LOADER:
In this phase, we includes loading of boot loader into
memory to bring up the kernal. This boot loader contains 2
things.
(a) MBR
(b) GRUB
(a) MBR (Master Boot Record) :
It is the first sector of hard disk with a size of 512 bytes.
Now, MBR directly can not load kernal because it is
unaware of file system concept and requires file system
drivers for each supported file system.
To overcome this situation, GRUB is used with details of
file system and its drivers.
(b) GRUB (Grand Unified Boot Loader) :
GRUB loads kernal in 3 stages:
Stage 1 :
The primary boot loader takes up less than 512 bytes of disk
space in MBR, which is too small to contain instructions required
to load OS. So, loading is done at either 1.5 stage or stage 2
boot loader.
Stage 1.5 :
Normally, stage 1.5 is used instead of stage 2. this can be happen
when 1024 cylinder hard disk is used.
GRUB stage 1.5 is located in first 30KB of hard disk
immediately after MBR and before first partition. This partition is
used to store file system drivers. Now, load stage 2.
Stage 2:
This is responsible for loading kernal from any module.
(4) kernal:
Kernal is a heart of OS, which is responsible for handling all
system processes. Kernal is loaded in following stages.
1. As soon as kernal is loaded, configures hardware and
memory allocation to system.
2. Next, it uncompresses the “initrd” image and mounts it to
load all necessary drivers.
3. Loading and unloading of kernal modules using ismod &
rmmod programs in initrd image.
4. Loads out for hard disk type.
5. Unmounts initrd image and frees up all memory, occupied
by disk image.
6. Then, kernal mounts the root partition as specified in
grub.config file.
7. Next, it runs “init” process.
(5) init process:
Executes the system to boot into selected run level
script.
(6) RUN LEVEL SCRIPT:
Based on selected run level, init process executes start
up scripts located in sub directories. This will executes
every time when you change run levels.
BOOT LOADERS
(1) LILO (Linux Loader):
LILO was first linux boot loader, which will never need to download
because all linux distributions come with it.
It is not depended on specific file system.
Can boot from hard disk & floppy.
Must change LILO when kernal file or config file is changed.
Most populer for linux.
It resides on your hard disk, and at booting time, it represents “boot
prompt” where you can choose an OS to boot.
LILO is fast, flexible and independent.
LILO can itself install in two places on your hard drive: boot sector of
partition or MBR.
If there is no other os present, LILO will have to be in master boot record.
MBR is the place on your hard drive that your computer’s BIOS looks for
bootstrap program that will load your OS, which can be LILO or DOS
boot loader.
(2) GRUB (Grand Unified Boot loader) :
GRUB is a boot loader, capable of loading a variety of
free os like linux, dos, or windows.
GRUB is dynamically configurable. Means, user can
make changes during boot time, which includes altering
existing boot entries, adding new custom entries or
modifiying “initrd”.
GRUB can be run and installed from any devices like
hard disk, CDROM, USB drive etc. and can load
operating system.
When computer boots, the BIOS transfer control to first
boot device, which is basically hard disk. The first
sector of hard disk is called MBR.
Differences: LILO v/s GRUB
no LILO GRUB

1 LILO supports only up to 16 GRUB supports unlimited number


different boot selections. of boot entries.

2 It can not boot from network. It can boot from network.

3 It must be updated every time you In GRUB, it does not required to


change configuration file. update again.

4 It does not have an interactive It provides command interface.


command interface.
CUI & GUI
CUI & GUI are interfaces, which are used to serve the purpose of
running programs in computer.

❖ CUI (Character User Interface) :


CUI means you have to use keyboard to type command to interact
with computer. You can only type text to give commands to
computer as in MS – DOS or command prompt. There are no
images or graphics on screen.
In beginning of computers, they were operated in this interface,
which provides only black screen, with white text. Mouse were not
used as pointer device.
Today , majority GUI are used, most modern computers have
modified versions of CUI called (Command line interface).
❖ GUI (Graphical User Interface ) :
Most modern computers uses GUI. This is an interface
that makes the use of graphics, images, icons etc.
This interface make it possible for mouse to be use with
computer and interaction, become very ease by just a
click of mouse, instead of typing every time to give
commands to computer.
Assignment - 3
Q.no Questions marks
1 What is shell? Explain shell keywords 3
2 What is variable? Explain shell variables & system variables. 5
3 Explain user defined variables. 3
4 What is shell script? 1
5 Explain decision statements in detail. 5
6 Explain test command. 3
7 Explain looping statements. 5
8 Write a short note on history of linux. 3
9 Explain components of linux. 3
10 Write a short note on (1) GNU & (2) GPL 3
11 Explain features of linux. 5
12 Explain installation steps of Linux 5
13 Explain boot loader process in booting procedure 5
14 Explain types of Boot loader : LILO & GRUB 5
15 Explain difference between LILO & GRUB 3

You might also like