KEMBAR78
Bash shell scripting | PPTX
BASH SHELL SCRIPTING Name: Vikas Tiwari
En-roll: 14486SC029
SHELLS
A shell is the user interface or set of programs user to interact
with Unix and process commands
Common shells are:
 Bourne
 C
 Korn
BOURNE SHELL (SH)
Written by Steven Bourne of Bell Labs
Both a command interpreter and a high- level
programming language
Typically the default Unix shell
Fast
 Approximately 20 times faster than C shell because it is
similar.
C SHELL (CSH)
Written by Bill Joy at University of California at Berkeley
Slower and more complex than Bourne shell, but has
facilities to make it more user friendly
Alias
History
Job control
Filename completion
KORN SHELL (KSH)
Written by David Korn of AT&T, released in 1986
Includes features of both the Bourne and C shell
Introduces several new user interface features including
command line editing
Adds features that improve its usefulness as a programming
language
 Report formatting capabilities
 Built-in arithmetic
 Data types
SOME BASH COMMANDS
cd (change directory): change the current working directory to
‘directory’
cd… using for one step back.
SOME BASH COMMANDS
ls (list) : showing listing of the current folders
SOME BASH COMMANDS
pwd (print working directory): print name of current working
directory.
cp (copy): Create copy of files and directories
SOME BASH COMMANDS
mv (move): move from one directory place to another either
rename of the current folder/file
SOME BASH COMMANDS
less: view the contents of a text file one screen at a time.
cat: used to in display files on print screen
grep: allow you to search one file or multiple file for line that
contain a pattern
SOME BASH COMMANDS
Echo: places a string on the computer terminal.
SOME BASH COMMANDS
touch: very quick way to create new, empty files.
mkdir: make a directory.
SOME BASH COMMANDS
chmod: allow to access the permissions to the file and
directory.
SOME BASH COMMANDS
rm (remove): use in remove file, directory.
SUPERUSER
A user with essentially all privileges
 Often referred to as root privileges
Allowed access to all files
Allowed to run all commands
System administrator has superuser privileges
Can be very dangerous, all the Unix built in protections are by-
passed
PASSWORD SECURITY
Unfortunately, password security is a necessity
Good passwords have several characteristics
 Minimum of six characters
 Mixture of alpha and numeric characters
 Mixture of upper and lower case
 No real words
 Susceptible to a dictionary attack
It is also good practices to avoid:
 Family names
 Birthdates
 Pet’s names
 Any other personal data
WHAT HAPPENS WHEN YOU LOGIN?
Unix runs the login program
 If it exists, .login script is executed
Then the shell specified in /etc/passwd is executed
and user is placed in his HOME directory
 If it exists, .cshrc (if csh is your default shell) is executed
So what’s the difference? Why to initialization files?
 .login is executed only at login
 .cshrc is executed every time a new shell is spanwned
EXIT OR LOGOUT
exit terminates your current shell
If it is also your login shell, exit will exit and logout
logout terminates a login shell
SCRIPTING BASICS
Shell scripts are text files that contains a series of
commands or statements to be executed.
Shell scripts are useful for:
 Automating commonly used commands.
 Performing system administration ad troubleshooting
 Creating simple applications
 Manipulations of text or files.
 Application prototyping
CREATING SHELL SCRIPTS
Step 1: Use a text editor such as vi to create a text file
containg commands.
 First line contains the magic “shbang” sequence :#!
 #!/bin/bash
 Comments your scripts
 Comments start with a#
Create shell scripts which is self documenting.
If you this pressing  key followed by the Enter key on
the most keyboards. This will enable you to enter one
command that spans multiple lines.
CREATING SHELL SCRIPTS CONT.
Step 2: Make the script executable
$ chmod a+x myscript.sh
To execute the new scripts:
Place the scripts file in a directory in the executable
path –OR-
Specify the absolute path or relative path to the script
on the command line.
CONTROL STRUCTURES
The three types in shell programming:
Sequential structures – the program flows one
line after another
Selection structures – code execution based on
a logical decision.
 if, if/else, if/elif/else and conditional operators.
Repetition structures (loos) –code execution is
repeated based on a logical decision
 for, while and until
CONDITIONAL EXECUTION
Commands may be executed conditionally, based on
the exit states of the previous command.
 && logical AND
 || logical OR
Examples:
 $ grep vikas passwd || echo ‘No vikas!’
 $cp –a /tmp/*.o . && echo ‘Done!’
This structures can be used in the command line as
well.
SELECTION STRUCTURES:
IF/THEN/ELIF
if selection structures execute the body of the
structure only if the condition tested is true.
if condition
then
 statement1
 statement2
 ………………..
fi
SELECTION STRUCTURES:
IF/THEN/ELIF CONT.
Sometimes, you may wish to specify an alternate
action when the condition fails. Here's how it's done.
 if condition
 then
 statement1
 statement2
 ………………..
 else
 statement3
 fi
SELECTION STRUCTURES:
IF/THEN/ELIF CONT.
alternatively, it is possible to test for another condition if the first "if" fails.
 if condition1
 then
 statement1
 statement2
 …………………
 elif condition2
 then
 statement3
 statement4
 …………………
 elif condition3
 then
 statement5
 statement6
 …………………
 fi
REPETITION STRUCTURES: THE
FOR-LOOP
The for repetition structure provides a method for iterating, or
looping, through a list of values and executing commands on
each of these values.
A loop requires
a) Start point
Process/Logic/statements to be repeated
b) Some hint or condition which changed the initial condition which
started the loop
c) Termination condition
 for variable in list-of-values
 do
 commands …
 done
SELECTION STRUCTURES: THE
WHILE-LOOP
The while loop structure provides a useful method for
performing a set of commands while a condition remains true.
The syntax is:
 while condition
 do
 commands
 Done
While loops are known as sentinel structures.
An until loop works in exactly the same way, except that it
continues to execute as long as the command following the
until statement executes successfully; that is it will stop the
loop when the command succeeds.
Thank You!

Bash shell scripting

  • 1.
    BASH SHELL SCRIPTINGName: Vikas Tiwari En-roll: 14486SC029
  • 2.
    SHELLS A shell isthe user interface or set of programs user to interact with Unix and process commands Common shells are:  Bourne  C  Korn
  • 3.
    BOURNE SHELL (SH) Writtenby Steven Bourne of Bell Labs Both a command interpreter and a high- level programming language Typically the default Unix shell Fast  Approximately 20 times faster than C shell because it is similar.
  • 4.
    C SHELL (CSH) Writtenby Bill Joy at University of California at Berkeley Slower and more complex than Bourne shell, but has facilities to make it more user friendly Alias History Job control Filename completion
  • 5.
    KORN SHELL (KSH) Writtenby David Korn of AT&T, released in 1986 Includes features of both the Bourne and C shell Introduces several new user interface features including command line editing Adds features that improve its usefulness as a programming language  Report formatting capabilities  Built-in arithmetic  Data types
  • 6.
    SOME BASH COMMANDS cd(change directory): change the current working directory to ‘directory’ cd… using for one step back.
  • 7.
    SOME BASH COMMANDS ls(list) : showing listing of the current folders
  • 8.
    SOME BASH COMMANDS pwd(print working directory): print name of current working directory. cp (copy): Create copy of files and directories
  • 9.
    SOME BASH COMMANDS mv(move): move from one directory place to another either rename of the current folder/file
  • 10.
    SOME BASH COMMANDS less:view the contents of a text file one screen at a time. cat: used to in display files on print screen grep: allow you to search one file or multiple file for line that contain a pattern
  • 11.
    SOME BASH COMMANDS Echo:places a string on the computer terminal.
  • 12.
    SOME BASH COMMANDS touch:very quick way to create new, empty files. mkdir: make a directory.
  • 13.
    SOME BASH COMMANDS chmod:allow to access the permissions to the file and directory.
  • 14.
    SOME BASH COMMANDS rm(remove): use in remove file, directory.
  • 15.
    SUPERUSER A user withessentially all privileges  Often referred to as root privileges Allowed access to all files Allowed to run all commands System administrator has superuser privileges Can be very dangerous, all the Unix built in protections are by- passed
  • 16.
    PASSWORD SECURITY Unfortunately, passwordsecurity is a necessity Good passwords have several characteristics  Minimum of six characters  Mixture of alpha and numeric characters  Mixture of upper and lower case  No real words  Susceptible to a dictionary attack It is also good practices to avoid:  Family names  Birthdates  Pet’s names  Any other personal data
  • 17.
    WHAT HAPPENS WHENYOU LOGIN? Unix runs the login program  If it exists, .login script is executed Then the shell specified in /etc/passwd is executed and user is placed in his HOME directory  If it exists, .cshrc (if csh is your default shell) is executed So what’s the difference? Why to initialization files?  .login is executed only at login  .cshrc is executed every time a new shell is spanwned
  • 18.
    EXIT OR LOGOUT exitterminates your current shell If it is also your login shell, exit will exit and logout logout terminates a login shell
  • 19.
    SCRIPTING BASICS Shell scriptsare text files that contains a series of commands or statements to be executed. Shell scripts are useful for:  Automating commonly used commands.  Performing system administration ad troubleshooting  Creating simple applications  Manipulations of text or files.  Application prototyping
  • 20.
    CREATING SHELL SCRIPTS Step1: Use a text editor such as vi to create a text file containg commands.  First line contains the magic “shbang” sequence :#!  #!/bin/bash  Comments your scripts  Comments start with a# Create shell scripts which is self documenting. If you this pressing key followed by the Enter key on the most keyboards. This will enable you to enter one command that spans multiple lines.
  • 21.
    CREATING SHELL SCRIPTSCONT. Step 2: Make the script executable $ chmod a+x myscript.sh To execute the new scripts: Place the scripts file in a directory in the executable path –OR- Specify the absolute path or relative path to the script on the command line.
  • 22.
    CONTROL STRUCTURES The threetypes in shell programming: Sequential structures – the program flows one line after another Selection structures – code execution based on a logical decision.  if, if/else, if/elif/else and conditional operators. Repetition structures (loos) –code execution is repeated based on a logical decision  for, while and until
  • 23.
    CONDITIONAL EXECUTION Commands maybe executed conditionally, based on the exit states of the previous command.  && logical AND  || logical OR Examples:  $ grep vikas passwd || echo ‘No vikas!’  $cp –a /tmp/*.o . && echo ‘Done!’ This structures can be used in the command line as well.
  • 24.
    SELECTION STRUCTURES: IF/THEN/ELIF if selectionstructures execute the body of the structure only if the condition tested is true. if condition then  statement1  statement2  ……………….. fi
  • 25.
    SELECTION STRUCTURES: IF/THEN/ELIF CONT. Sometimes,you may wish to specify an alternate action when the condition fails. Here's how it's done.  if condition  then  statement1  statement2  ………………..  else  statement3  fi
  • 26.
    SELECTION STRUCTURES: IF/THEN/ELIF CONT. alternatively,it is possible to test for another condition if the first "if" fails.  if condition1  then  statement1  statement2  …………………  elif condition2  then  statement3  statement4  …………………  elif condition3  then  statement5  statement6  …………………  fi
  • 27.
    REPETITION STRUCTURES: THE FOR-LOOP Thefor repetition structure provides a method for iterating, or looping, through a list of values and executing commands on each of these values. A loop requires a) Start point Process/Logic/statements to be repeated b) Some hint or condition which changed the initial condition which started the loop c) Termination condition  for variable in list-of-values  do  commands …  done
  • 28.
    SELECTION STRUCTURES: THE WHILE-LOOP Thewhile loop structure provides a useful method for performing a set of commands while a condition remains true. The syntax is:  while condition  do  commands  Done While loops are known as sentinel structures. An until loop works in exactly the same way, except that it continues to execute as long as the command following the until statement executes successfully; that is it will stop the loop when the command succeeds.
  • 29.