Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 Operating System © Copyright 2012, All Rights Reserved

2 Executing Scripts Must be executable. Example: chmod a+x script Pathname of shell or interpreter on first line. Example: #!/bin/bash Specify relative or full pathname Or if directory is on $PATH, specify script name. Example: $PATH=$PATH:$HOME/bin

3 False True if... then: Flow Diagram ifif commands

4 if... then: Control Structure Definition: Evaluates a logical condition and controls whether or not statements are executed Syntax: if test ; then commands fi

5 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

6 if... then: Input prompt Purpose: Guess the “mystery” word when prompted Example script “mysteryWord”: target="secret" echo -n "Guess word: " read guess if ["$guess" = "$target"] ; then echo "That's right!" fi

7 False True if... then... else: Flow Diagram ifif commandscommandscommandscommands elsebranch ifbranch

8 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 commands else commands fi

9 False True False if... elif... else: Flow Diagram commandscommands commandscommands elseelse elifelif ifif commandscommands elif branch if branch else fallback

10 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

11 Debugging Shell Scripts Description: Echo every script line before being executed Example: #!/bin/bash -x or bash -x script [args]

12 False True for... in: Flow Diagram for in commands true

13 for... in: Looping Structures Description: Structure that performs given number of iterations on a statement or statements Syntax: for idx in argList ; do commands done

14 for... in: Looping Example Purpose: Display all command-line arguments for arg in $* ; do echo "Arg: $arg" done

15 for: Looping Structures Description: Structure that iterates on the command line arguments Syntax: for arg ; do commands done

16 for: Looping Structure Purpose: Display all command-line arguments for arg ; do echo "Arg: $arg" done

17 False True while: Flow Diagram whilewhile commands while true

18 while: Looping Structures Description: Structure that performs iterations while a condition is true (i.e. repeat while true) Syntax: while test ; do commands done

19 while: Looping Example Purpose: Count to ten num=1 while [ $num -lt 11 ]; do echo $num ((num += 1)) done

20 True False until: Flow Diagram untiluntil commands until false

21 until: Looping Structures Description: Structure that performs iterations while a condition is false (i.e. repeat until true) Syntax: until test ; do commands done

22 until: Looping Example Purpose: Guess a number secret=4 guess="" echo "Number between 1 & 10" until [ "$guess" = "$secret" ] ; do echo -n "Your guess: " read guess done echo "You guessed it!"

23 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 $idx done

24 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 $idx done

25 False True case: Flow Diagram casecase patt1)patt1) *)*) commandscommands commandscommands case fallback

26 case: Structure Description: Multiple branching mechanism similar to if... elif... else Syntax: case test in pattern1) command1 ;; patternN) commandN ;; *) fallbackCmd ;; esac

27 case: Example echo -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

28 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

29 <<: The “here” document Purpose: Allows the redirection of input from within the shell Example: cat file one two EOF

30 Ctrl+D: End of File Signal Purpose: 1.Terminates a shell 2.Provides end of file signal to stdin Example: cat > file one two Ctrl+D


Download ppt "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."

Similar presentations


Ads by Google