A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27:...

25
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux Operating System © Copyright 2012, All Rights Reserved

Transcript of A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27:...

Page 1: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

A Practical Guide to Fedora and Red Hat Enterprise Linux

Unit 5: Scripting in LinuxChapter 27: Programming the Bourne Again Shell

By Fred R. McClurg

Linux Operating System

© Copyright 2012, All Rights Reserved

Page 2: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

Executing ScriptsMust be executable. Example:

chmod a+x scriptPathname of shell or interpreter on first line. Example:

#!/bin/bashSpecify relative or full pathnameOr if directory is on $PATH, specify script name. Example:

$PATH=$PATH:$HOME/bin

Page 3: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

False

True

if ... then: Flow Diagram

if

commands

Page 4: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

if ... then: Control Structure

Definition: Evaluates a logical condition and controls whether or not statements are executed

Syntax:if test ; then commandsfi

Page 5: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

if ... then: Display first arg

Purpose: Display first command line argument if specified

Example script “argDisplay”:if test "$1" != ""then echo "First arg: $1"fi

Page 6: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

if ... then: Input prompt

Purpose: Guess the “mystery” word when prompted

Example script “mysteryWord”:target="secret"echo -n "Guess word: "read guessif ["$guess" = "$target"] ; then

echo "That's right!"fi

Page 7: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

FalseTrue

if ... then ... else: Flow Diagram

if

commandscommandselse

branchif

branch

Page 8: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

if ... then ... else: Structure

Definition: Evaluates a condition. If condition is true, control follows one branch. If the condition is not true (else), control follows a different branch.

Syntax:if test ; then commandselse commandsfi

Page 9: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

FalseTrue

True

False

if ... elif ... else: Flow Diagram

commands

commands

else

elif

if

commandselif branch

if branch

else fallback

Page 10: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

if ... elif ... else: Example Purpose: File type via command-line

argument

Example “whatisit”:if [ -h "$1" ] ; then echo "Link: $1"elif [ -f "$1" ] ; then echo "File: $1"elif [ -d "$1" ] ; then echo "Directory: $1"else echo "Not link, file or dir"fi

Page 11: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

Debugging Shell Scripts

Description: Echo every script line before being executed

Example:#!/bin/bash -x

orbash -x script [args]

Page 12: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

False

True

for ... in: Flow Diagram

for in

commands

for

in

true

true

Page 13: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

for ... in: Looping Structures

Description: Structure that performs given number of iterations on a statement or statements

Syntax:for idx in argList ; do commandsdone

Page 14: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

for ... in: Looping Example

Purpose: Display all command-line arguments

for arg in $* ; do echo "Arg: $arg"done

Page 15: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

False

True

while: Flow Diagram

while

commands

wh

ile

true

true

Page 16: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

while: Looping Structures

Description: Structure that performs iterations while a condition is true (i.e. repeat while true)

Syntax:while test ; do commandsdone

Page 17: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

while: Looping Example

Purpose: Count to ten

num=1while [ $num -lt 11 ]; do echo $num ((num += 1))done

Page 18: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

break: Looping Interrupt

Description: Terminates the current iteration and breaks out of the loop

Example:for idx in {0..9} ; do if [ $idx = 4 ] ; then break fi echo $idxdone

Page 19: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

continue: Looping Interrupt

Description: Terminates the current loop and continues the next iteration

Example:for idx in {0..9} ; do if [ $idx = 4 ] ; then continue fi echo $idxdone

Page 20: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

False

True

case: Flow Diagram

case

patt1)

*)

commands

commands

case fallback

Page 21: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

case: StructureDescription: Multiple branching

mechanism similar to if ... elif ... else

Syntax:case test in pattern1) command1 ;; patternN) commandN ;; *) fallbackCmd ;;esac

Page 22: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

case: Exampleecho -n "Where do you want to go? "

read room

case "$room" in

"cave")

echo "It is dark!" ;;

"hill")

echo "Tough climb!" ;;

"cliff")

echo "I’m falling!" ;;

*)

echo "Can’t go there!" ;;

esac

Page 23: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

Special Parameters

Parameter Description

$$ Process Id (PID) of the shell

$# Command line arg count

$0 Name of the script$1-$9 Command line

arguments$* All the command line

args

Page 24: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

<<: The “here” document

Purpose: Allows the redirection of input from within the shell

Example:cat << EOF > fileonetwoEOF

Page 25: A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.

Ctrl+D: End of File Signal

Purpose: 1. Terminates a shell2. Provides end of file signal to

stdin

Example:cat > fileonetwoCtrl+D