1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell
2 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions
3 © 2001 John Urrutia. All rights reserved. Control Structures Selections if … then … fi One-Way Selection if … then … else … fi Two-Way Selection if … then … elif … fi Multi -Way Selection The above builtins must be on separate lines or separated by ;
4 © 2001 John Urrutia. All rights reserved. Control Structures Syntax: if test expression or [ expression ] then command(s) fi … Must Evaluate True or False Boolean Expression Executes if expression is true test expression or [ expression ] if then Executes if expression is false fi
5 © 2001 John Urrutia. All rights reserved. Boolean Expressions Algebra created by George Boole Always evaluates to a binary state Generally: 1 is TRUE 0 is FALSE
6 © 2001 John Urrutia. All rights reserved. Boolean or Logical Operators And -a Or -o Not !
7 © 2001 John Urrutia. All rights reserved. and Boolean Truth Tables Expr 1Expr 2 -a True FalseTrue False TrueFalse
8 © 2001 John Urrutia. All rights reserved. or Boolean Truth Tables True FalseTrue FalseTrue False Expr 1Expr 2 -o
9 © 2001 John Urrutia. All rights reserved. not Boolean Truth Tables Expr ! FalseTrue FalseTrue
10 © 2001 John Urrutia. All rights reserved. test ing, test ing, test ing test expression or [ expression ] Evaluates the expression and returns a Boolean true or false. expression can be simple or compound Criteria: String Integer Filename File descriptor number
11 © 2001 John Urrutia. All rights reserved. Test Criteria String expressions – applies to string variables or literals Is null or Length is zero (-z) or Length is > 0 (-n) string1 = or != string2 If you are comparing literals quote them
12 © 2001 John Urrutia. All rights reserved. Testing Strings …]$ if test "not" != "equal";then echo Not Equal;fi Not Equal …]$ if [ "not" = "equal“ ];then echo Equal;fi …]$
13 © 2001 John Urrutia. All rights reserved. Test Criteria (cont.) Integer relationship -gt greater than -ge greater than or equal to -eq equal to -ne not equal to -le less than or equal to -lt less than
14 © 2001 John Urrutia. All rights reserved. Testing Integers …]$ if test 123 -ne 234;then echo Not Equal;fi Not Equal …]$ if [ 123 -eq 234 ];then echo Equal;fi …]$
15 © 2001 John Urrutia. All rights reserved. Test Criteria (cont.) Filename expression – file exists and has specific attributes drewsx -d irectory -r eadable -e xists -w ritable -s ize – file has data (length >0) -x executable
16 © 2001 John Urrutia. All rights reserved. Testing Files …]$ if [ ! -e “TestFile” ];then echo Not found;fi Not found …]$ …]$ if test -e “TestFile”;then echo Found it;fi …]$
17 © 2001 John Urrutia. All rights reserved. Bourne - if, then if Establishes a control structure Followed by a test command then Commands executed if test is true
18 © 2001 John Urrutia. All rights reserved. Bourne – else, fi else Commands executed if test is false fi Terminates this if statement
19 © 2001 John Urrutia. All rights reserved. if …then … else … fi Start Test 1 False True
20 © 2001 John Urrutia. All rights reserved. Bourne - elif elif “else if” structure Linear in nature Similar to the case or switch in selection but completely different in execution.
21 © 2001 John Urrutia. All rights reserved. if …then … elif … else … fi Start Test 2 False True Test 1 True if then elifelse
22 © 2001 John Urrutia. All rights reserved. if … then … [ else or elif ] … fi 2 structures if […] then cmds if […] then cmds else cmds else cmds fi if […] then cmds elif […] then cmds else cmds fi
23 © 2001 John Urrutia. All rights reserved. if [ $a –gt 2 ] then if [ $a –gt 3 ] then echo value is 4 else echo value is 3 fi else if [ $a –gt 1 ] then echo value is 2 else echo value is 1 fi if [ $a –gt 3 ] then echo value is 4 elif [ $a –gt 2 ] then echo value is 3 elif [ $a –gt 1 ] then echo value is 2 else echo value is 1 fi
24 © 2001 John Urrutia. All rights reserved. Control Structures Iterations for … in for
25 © 2001 John Urrutia. All rights reserved. for… and for …in Assign element Do commands More elements Done
26 © 2001 John Urrutia. All rights reserved. Here we go loop-de-loop for x in a b c do cmd cmd cmd done Substitutes the x variable with each in variable and executes the commands between do and done
27 © 2001 John Urrutia. All rights reserved. California the land of … echo “California the land of – “ for info in fruits nuts cheese wine do echo –n “ $info” done echo –e “\n And of course Hollywood”
28 © 2001 John Urrutia. All rights reserved. Here we go loop-de-loop for x do cmd cmd cmd done Substitutes the x with command line arguments and executes the commands between do and done
29 © 2001 John Urrutia. All rights reserved. California the land of … echo “California the land of – “ for arg in fruits nuts cheese wine do echo –n “ $arg” done echo “ And of course Hollywood” This modification causes the script to display all command line arguments again
30 © 2001 John Urrutia. All rights reserved. Control Structures Iterations while until
31 © 2001 John Urrutia. All rights reserved. While I’m here – Until it’s gone while […] (initial state true - go) until […] (initial state true – stop) do cmd cmd cmd done
32 © 2001 John Urrutia. All rights reserved. While you were out… cold While Test do commands Done Pre-Test Loop
33 © 2001 John Urrutia. All rights reserved. Until … The cows come home until Test do commands Done Pre-Test Loop
34 © 2001 John Urrutia. All rights reserved. Continue to Break continue and break Used in for while and until structures break – terminates the structure continue – terminates this iteration
35 © 2001 John Urrutia. All rights reserved. continue for idx in do if [ $idx –le 3 ] ; then echo “continue” continue fi echo $idx done Continues three times Echoes 4 – 10
36 © 2001 John Urrutia. All rights reserved. break for idx in do if [ $idx –gt 6 ] ; then echo “break” break fi echo $idx done Stops for loop after sixth iteration Echoes 1 – 6
37 © 2001 John Urrutia. All rights reserved. Bourne – case The case command tests for multiple values in a variable Allows the use of “wild cards” First match wins
38 © 2001 John Urrutia. All rights reserved. Get on this case case $var in test-var1 ) cmd1;cmd2 cmd3 ;; test-var2 ) cmds ;; ) cmds ;; esac
39 © 2001 John Urrutia. All rights reserved. case “$variable” in A|a ) echo Entered A or a ;; [0-9] ) echo Entered number ;; ?z* ) echo Entered z in 2 nd position ;; esac
40 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions
41 © 2001 John Urrutia. All rights reserved. Here boy << The Here Document Allows in-stream data to feed a script. Must start with << and a data delimiter character Data delimiter character on line by itself - terminates
42 © 2001 John Urrutia. All rights reserved. Here in the script #!/bin/bash grep –i “$1” <<+ AlexJune 22 BabsFebruary 3 LeroyJanuary 20 + echo “All Done now”
43 © 2001 John Urrutia. All rights reserved. Bundle script #!/bin/bash echo “# To unbundle sh this file for i do echo “echo $i 1>&2” echo “cat >$i <<‘End of $i’” cat $i echo “End of $i” done
44 © 2001 John Urrutia. All rights reserved. Bundle script -- output # To unbundle sh this file echo 1 st filename 1>&2 cat > 1 st filename <<‘End of 1 st filename ’ Contents of 1 st filename.. End of 1 st filename
45 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions
46 © 2001 John Urrutia. All rights reserved. Expanding Null or Unset Vars. ${ name } – expands to the value of the variable. If variable is null or not set the expansion produces a null string (\0) ${ name :- default } – expands to the value of the variable or the default value if null.
47 © 2001 John Urrutia. All rights reserved. Expanding Null or Unset Vars. ${ name := default } – expands to the value of the variable or the default value if null and sets the variable to the default value. : ${ name := default } – if null sets the variable to the default value. ${ name :? message } – if null or unset sends message to stdout.
48 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions
49 © 2001 John Urrutia. All rights reserved. The execute command The exec command Executes scripts or programs Runs under the same PID Provides access to the original environment variables Terminates current process.
50 © 2001 John Urrutia. All rights reserved. The execute command The exec command Can be used to redirect stdin, stdout and stderr from inside a script. exec < infile exec > outfile 2> errfile
51 © 2001 John Urrutia. All rights reserved. I feel like trap – let’s kill a PID trap ’ action ’ [ signal number ] 64 signals available 0 – 63 1 – Phone Hangup 2 – Pressing an interrupt key ( ^c) 3 – Pressing the quit key (Ctrl+Shft+| or \) 9 – Kill command (without mercy ^d) 15 – Terminate process (kill command) 20 – Job control STOP key (^z)
52 © 2001 John Urrutia. All rights reserved. I feel like trap – let’s kill a PID kill - [ signal number ] [ pid ] Sends a signal to one or more PIDs By default sends signal 15 (SIGTERM) Using signal 9 is un-trappable Get-out … NOW! kill –9 0
53 © 2001 John Urrutia. All rights reserved. Topics Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions
54 © 2001 John Urrutia. All rights reserved. Play that Function music Functions are like pre-processed scripts. Can be stored in.profile or in script files. Generally used by more than 1 script.
55 © 2001 John Urrutia. All rights reserved. Function declaration Function-name () { commands } Functions can be called with arguments and process them the same way a command line would.