Upload
alexandra-oconnor
View
232
Download
0
Embed Size (px)
Citation preview
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
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
False
True
if ... then: Flow Diagram
if
commands
if ... then: Control Structure
Definition: Evaluates a logical condition and controls whether or not statements are executed
Syntax:if test ; then commandsfi
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
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
FalseTrue
if ... then ... else: Flow Diagram
if
commandscommandselse
branchif
branch
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
FalseTrue
True
False
if ... elif ... else: Flow Diagram
commands
commands
else
elif
if
commandselif branch
if branch
else fallback
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
Debugging Shell Scripts
Description: Echo every script line before being executed
Example:#!/bin/bash -x
orbash -x script [args]
False
True
for ... in: Flow Diagram
for in
commands
for
in
true
true
for ... in: Looping Structures
Description: Structure that performs given number of iterations on a statement or statements
Syntax:for idx in argList ; do commandsdone
for ... in: Looping Example
Purpose: Display all command-line arguments
for arg in $* ; do echo "Arg: $arg"done
False
True
while: Flow Diagram
while
commands
wh
ile
true
true
while: Looping Structures
Description: Structure that performs iterations while a condition is true (i.e. repeat while true)
Syntax:while test ; do commandsdone
while: Looping Example
Purpose: Count to ten
num=1while [ $num -lt 11 ]; do echo $num ((num += 1))done
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
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
False
True
case: Flow Diagram
case
patt1)
*)
commands
commands
case fallback
case: StructureDescription: Multiple branching
mechanism similar to if ... elif ... else
Syntax:case test in pattern1) command1 ;; patternN) commandN ;; *) fallbackCmd ;;esac
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
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
<<: The “here” document
Purpose: Allows the redirection of input from within the shell
Example:cat << EOF > fileonetwoEOF
Ctrl+D: End of File Signal
Purpose: 1. Terminates a shell2. Provides end of file signal to
stdin
Example:cat > fileonetwoCtrl+D