Unit - 3 With Linux -Unix
Unit - 3 With Linux -Unix
Shell keywords :
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.
(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