KEMBAR78
SHELL PROGRAMMING | PPTX
SHELL PROGRAMMING
By:-  JINAL THAKRAR
To know about shell scripting we must
have a basic idea on the followings
terms:-
• Kernel
• Shell
• Process
• Pipes
• Filters etc
What is kernel?
The kernel is a computer program that
manages input/output requests from software
It manages resource of Linux O/S.
Resources means facilities available in Linux.
For eg. Facility to store data, print data on printer,
memory, file management etc .
Tasks
I/O management
Process management
Device management
File management
Memory management
What is process?
Processes carry out tasks within the operating system.
a process can be thought of as a computer program in
action.
What are pipes?
A pipe is a form of redirection that is used in Linux to send the
output of one program to another program for further
processing.
What are filters?
When a program performs operations on input and writes
the result to the standard output, it is called a filter.
Most popularly used shells are :-
1) BASH ( Bourne-Again SHell )
Most common shell in Linux. It's Freeware shell.
2) CSH (C SHell)
The C shell's syntax and usage are very similar
to the C programming language.
3) And KSH (Korn SHell)
What is shell?
Shell accepts your instruction or commands in English and
translate it into computers native binary language.
Shell is an command language interpreter that executes
commands read from the standard input device (keyboard)
or from a file.
Linux common commands
Purpose Syntax Example
To see date date $ date
To see who's using system. who $ who
Print working directory pwd $ pwd
List name of files in current
directory
ls $ ls
To create text file cat > { file name } $ cat > myfile
To move or rename file /
directory
mv $ mv sales
sales.99
To remove file rm file1 $ rm myfile
Remove all files in given
directory/subdirectory.
rm -rf {dirname} $ rm -rf old files
Read your mail Mail $ mail
To See more about
currently login person
who am I $ who am I
To login out Logout $ logout
Send mail to other person mail {user-name} $ mail ashish
To print file pr {file-name} $pr myfile
To Use to compare files cmp {file1} {file2} $cmp myfile
myfile.old
Introduction of shell programming
Shell program is series of Linux commands.
Shell script can take input from user, file and output them on
screen.
Useful to create our own commands that can save our lots
of time and to automate some task of day today life.
Variables in linux
Sometimes to process our data/information, it must be kept in
computers RAM memory.
RAM memory is divided into small locations, and each
location has unique number called memory
location/address, which is used to hold our data.
In Linux, there are two types of variable:-
• System variables
• User defined variables(UDV)
System variables -Created and maintained by Linux itself.
User defined variables -Created and maintained by user.
Examples of system variables :
SHELL, PATH, OSTYPE
If you want to know the value of a variable:
$ echo $USERNAME
$ echo $HOME
How to define User defined variables
(UDV)
Syntax :- variable_name=value
NOTE: Here 'value' is assigned to given 'variablename'
and Value must be on right side = sign For
e.g.
$ no=10 # this is ok
$ 10=no # Error, NOT Ok, Value must be on right side
of = sign.
Rules for Naming variable name
1) Variable name must begin with Alphanumeric
character or underscore character (_), followed by
one or more Alphanumeric character.
2) Don't put spaces on either side of the equal sign when
assigning value to variable.
3) Variables are case-sensitive, just like filename in Linux.
4) You can define NULL variable.
5) Do not use ?,* etc, to name your variable names.
How to write shell script?
To write shell script to print “Knowledge is Power” you
can use cat command.
$ cat > first
#
# My first shell script
#
clear
echo "Knowledge is Power"
How to run shell script?
Because of security of files, in Linux, the creator of Shell Script
does not get execution permission by default. So if we wish to
run shell script we have to do two things as follows
(1) Use chmod command as follows to give execution
permission to our script.
(2) Run our script as
Syntax: ./your-shell-program-name
For e.g.
$ ./first
Commands Related with Shell
Programming
1) echo [options] [string, variables...]
Displays text or variables value on screen.
The -e option is used to enable echo's interpretation
of additional instances of the newline character as
well as the interpretation of other special
characters, such as a horizontal tab, which is
represented by t
b backspace
n new line
t horizontal tab
 backslash
For eg. $ echo -e "An apple a day keeps away tt
doctorn"
2) More about Quotes
There are three types of quotes
" i.e. Double Quotes
' i.e. Single quotes
` i.e. Back quote
1."Double Quotes" - Anything enclose in double quotes
removed meaning of that characters (except  and $).
2. 'Single quotes' - Enclosed in single quotes remains
unchanged.
3. `Back quote` - To execute command
For eg.
$ echo "Today is date“
$ echo "Today is `date`".
3) Shell Arithmetic
Use to perform arithmetic operations
For e.g.
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ echo `expr 6 + 3`
Local and Global Shell variable.
 Normally all our variables are local.
 Local variable can be used in same shell, if
you load another copy of shell then new shell
ignored all old shell's variable.
 "You can copy old shell's variable to new shell
(i.e. first shells variable to seconds shell), such
variable is know as Global Shell variable."
SHELL PROGRAMMING
SHELL PROGRAMMING

SHELL PROGRAMMING

  • 1.
  • 2.
    To know aboutshell scripting we must have a basic idea on the followings terms:- • Kernel • Shell • Process • Pipes • Filters etc
  • 3.
    What is kernel? Thekernel is a computer program that manages input/output requests from software It manages resource of Linux O/S. Resources means facilities available in Linux. For eg. Facility to store data, print data on printer, memory, file management etc . Tasks I/O management Process management Device management File management Memory management
  • 4.
    What is process? Processescarry out tasks within the operating system. a process can be thought of as a computer program in action. What are pipes? A pipe is a form of redirection that is used in Linux to send the output of one program to another program for further processing. What are filters? When a program performs operations on input and writes the result to the standard output, it is called a filter.
  • 5.
    Most popularly usedshells are :- 1) BASH ( Bourne-Again SHell ) Most common shell in Linux. It's Freeware shell. 2) CSH (C SHell) The C shell's syntax and usage are very similar to the C programming language. 3) And KSH (Korn SHell)
  • 6.
    What is shell? Shellaccepts your instruction or commands in English and translate it into computers native binary language. Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file.
  • 7.
  • 8.
    Purpose Syntax Example Tosee date date $ date To see who's using system. who $ who Print working directory pwd $ pwd List name of files in current directory ls $ ls To create text file cat > { file name } $ cat > myfile To move or rename file / directory mv $ mv sales sales.99 To remove file rm file1 $ rm myfile Remove all files in given directory/subdirectory. rm -rf {dirname} $ rm -rf old files Read your mail Mail $ mail To See more about currently login person who am I $ who am I To login out Logout $ logout Send mail to other person mail {user-name} $ mail ashish To print file pr {file-name} $pr myfile To Use to compare files cmp {file1} {file2} $cmp myfile myfile.old
  • 9.
    Introduction of shellprogramming Shell program is series of Linux commands. Shell script can take input from user, file and output them on screen. Useful to create our own commands that can save our lots of time and to automate some task of day today life.
  • 10.
    Variables in linux Sometimesto process our data/information, it must be kept in computers RAM memory. RAM memory is divided into small locations, and each location has unique number called memory location/address, which is used to hold our data. In Linux, there are two types of variable:- • System variables • User defined variables(UDV) System variables -Created and maintained by Linux itself. User defined variables -Created and maintained by user. Examples of system variables : SHELL, PATH, OSTYPE If you want to know the value of a variable: $ echo $USERNAME $ echo $HOME
  • 11.
    How to defineUser defined variables (UDV) Syntax :- variable_name=value NOTE: Here 'value' is assigned to given 'variablename' and Value must be on right side = sign For e.g. $ no=10 # this is ok $ 10=no # Error, NOT Ok, Value must be on right side of = sign.
  • 12.
    Rules for Namingvariable name 1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character. 2) Don't put spaces on either side of the equal sign when assigning value to variable. 3) Variables are case-sensitive, just like filename in Linux. 4) You can define NULL variable. 5) Do not use ?,* etc, to name your variable names.
  • 13.
    How to writeshell script? To write shell script to print “Knowledge is Power” you can use cat command. $ cat > first # # My first shell script # clear echo "Knowledge is Power"
  • 14.
    How to runshell script? Because of security of files, in Linux, the creator of Shell Script does not get execution permission by default. So if we wish to run shell script we have to do two things as follows (1) Use chmod command as follows to give execution permission to our script. (2) Run our script as Syntax: ./your-shell-program-name For e.g. $ ./first
  • 15.
    Commands Related withShell Programming 1) echo [options] [string, variables...] Displays text or variables value on screen. The -e option is used to enable echo's interpretation of additional instances of the newline character as well as the interpretation of other special characters, such as a horizontal tab, which is represented by t b backspace n new line t horizontal tab backslash For eg. $ echo -e "An apple a day keeps away tt doctorn"
  • 16.
    2) More aboutQuotes There are three types of quotes " i.e. Double Quotes ' i.e. Single quotes ` i.e. Back quote 1."Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except and $). 2. 'Single quotes' - Enclosed in single quotes remains unchanged. 3. `Back quote` - To execute command For eg. $ echo "Today is date“ $ echo "Today is `date`".
  • 17.
    3) Shell Arithmetic Useto perform arithmetic operations For e.g. $ expr 1 + 3 $ expr 2 - 1 $ expr 10 / 2 $ echo `expr 6 + 3`
  • 18.
    Local and GlobalShell variable.  Normally all our variables are local.  Local variable can be used in same shell, if you load another copy of shell then new shell ignored all old shell's variable.  "You can copy old shell's variable to new shell (i.e. first shells variable to seconds shell), such variable is know as Global Shell variable."