Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2016-2017 – Curt Hill Bash Scripting Fundamentals Copyright © 2016-2017 – Curt Hill.

Similar presentations


Presentation on theme: "Copyright © 2016-2017 – Curt Hill Bash Scripting Fundamentals Copyright © 2016-2017 – Curt Hill."— Presentation transcript:

1 Copyright © 2016-2017 – Curt Hill
Bash Scripting Fundamentals Copyright © – Curt Hill

2 Copyright © 2016-2017 – Curt Hill
Introduction Previously, we have considered the PowerShell scripts and Batch scripts The original script is the shell script We will now consider Bash scripts This includes: Comments Variables and types Subsequent presentations will consider flow of control and functions Copyright © – Curt Hill

3 Copyright © 2016-2017 – Curt Hill
First Line Since There are multiple scripting languages Extensions are largely ignored How do we know the script language? The first line has a special format Starts with an octothorpe and exclamation point Next is the path to the script interpreter #!/bin/bash This is known as the shebang Copyright © – Curt Hill

4 Copyright © 2016-2017 – Curt Hill
Debugging Not needed This class is too good to make a mistake For the lesser mortals we can make a script trace what it is doing Append -x to the shebang and bash will trace it Copyright © – Curt Hill

5 Copyright © 2016-2017 – Curt Hill
Permissions When we create a script we generally use an editor Different than a compiler A compiler will set the execute permission bit but the editor will not Thus we need a chmod to make it executable Copyright © – Curt Hill

6 Copyright © 2016-2017 – Curt Hill
Comments The use of comments in a console are somewhat pointless We can use them there as we will see However we desperately need these in scripts Which we will get to The comment in Bash is an octothorp Like the // in C++ it finishes the line Copyright © – Curt Hill

7 Variables and Assignment
Bash variables have approximately the same format as in C No declaration is needed Assignment is just the = Like C but terminated by the line end rather than a semicolon No blanks in the assignment Copyright © – Curt Hill

8 Copyright © 2016-2017 – Curt Hill
Two Sides There are two usages of a variable To assign to a variable use the name To obtain the value prefix with a $ Consider the following simple script Copyright © – Curt Hill

9 Copyright © 2016-2017 – Curt Hill
Hello World $ cat hello.sh #!/bin/bash greet="Hello World" second=$greet echo $greet echo $second $ ./hello.sh Hello World $ Copyright © – Curt Hill

10 Copyright © 2016-2017 – Curt Hill
Commentary The quotes are needed because there is a blank in the phrase Had it been a simple Hello no quotes would be needed If the assignment had been second=greet then the second line would have been greet Rather than the contents of variable greet Copyright © – Curt Hill

11 Copyright © 2016-2017 – Curt Hill
Types There are no types in variables Variables may merely hold a string of characters Strings may be manipulated using a variety of operators We will look at this first This makes arithmetic rather interesting We must use the let Copyright © – Curt Hill

12 Copyright © 2016-2017 – Curt Hill
Strings Only strings with blanks need to be in quotes Strings may be concatenated by having them adjacent There are several other manipulation operators $ s1=Thisisalargestring $ s2="This one has blanks" $ s3=$s1$s2 $ echo $s3 ThisisalargestringThis one has blanks Copyright © – Curt Hill

13 Copyright © 2016-2017 – Curt Hill
String Length The length of a string may be obtained in a rather curious way: ${#str} where str is a variable For example: $ echo ${s3} ThisisalargestringThis one has blanks $ echo ${#s3} 37 Copyright © – Curt Hill

14 Copyright © 2016-2017 – Curt Hill
String Functions This unusual notation ${…} is the basis for a whole set of string manipulations What changes are the operators used inside the braces Length was preceding the string with the octothorpe Substring is following with :position:length The position is a C subscript – starts at zero Length is optional Copyright © – Curt Hill

15 Copyright © 2016-2017 – Curt Hill
Substring Example $ letter=abcdefghijklmnopqrstuvwxyz $ echo ${letter:5:2} fg $ echo ${letter:5} fghijklmnopqrstuvwxyz Copyright © – Curt Hill

16 Copyright © 2016-2017 – Curt Hill
Replacement Replace a portion of a string with something else Basic form: ${string/pattern/replace} Where pattern is the string to look for Replace substitutes if found Variations If first slash is doubled all are replaced If first slash is followed by # must match front If first slash is followed by % must match end Copyright © – Curt Hill

17 Copyright © 2016-2017 – Curt Hill
Replacement Example $ echo ${letter/ghi/..} abcdef..jklmnopqrstuvwxyz $ echo ${letter/#ab/....} ....cdefghijklmnopqrstuvwxyz $ echo ${letter/#def/....} abcdefghijklmnopqrstuvwxyz $ echo ${letter/%xyz/....} abcdefghijklmnopqrstuvw.... $ echo ${letter/%pqr/....} str= $ echo ${str//123/99} Copyright © – Curt Hill

18 Copyright © 2016-2017 – Curt Hill
Delete a Substring Similar form: ${str#pattern} This delete match from front The % removes match from back The pattern may also be a limited regular expression This is true in replacement also Copyright © – Curt Hill

19 Copyright © 2016-2017 – Curt Hill
Example $ echo $num 133,321.84 $ echo ${num#[0-9]} 33,321.84 $ echo ${num%[0-9]} 133,321.8 $ echo ${num%%\.[0-9]*} 133,321 $ echo ${num/,[0-9]*./xxx} 133xxx84 Copyright © – Curt Hill

20 Copyright © 2016-2017 – Curt Hill
The Let Similar to a normal assignment but the domain is restricted to numbers and arithmetic Format is: let expr let string The first is used if no blanks The second is a string containing an expression with blanks Copyright © – Curt Hill

21 Copyright © 2016-2017 – Curt Hill
Example $ let x=5 $ let y=2 $ echo $x+$y 5+2 $ let z = $x+$y -bash: let: =: syntax error: operand expected (error token is "=") $ let z=$x+$y $ echo $z 7 $ let "w = x*2 +y++" $ echo $w 12 $ echo "$w and $y" 12 and 3 Copyright © – Curt Hill

22 Copyright © 2016-2017 – Curt Hill
Commentary Notice that the stropping $ allows the value to display even in quoted strings The ++ and -- operators have their usual C/C++/Java meaning Copyright © – Curt Hill

23 Command-line parameters
The command line parameters of a script are predefined variables Start with $ and followed by a digit $0 is the script name $1 is the first word $2 is the second word A parameter not present is an empty string Copyright © – Curt Hill

24 Copyright © 2016-2017 – Curt Hill
Example $ ./test2.sh test.sh ./test2.sh test.sh No 2nd parameter $ cat test2.sh #!/bin/bash echo $0 if [ -n "$1" ] then echo $1 fi if [ -n "$2" ] echo $2 else echo "No 2nd parameter" exit 0 Copyright © – Curt Hill

25 Copyright © 2016-2017 – Curt Hill
Finally for Now Now we know the minimal amount of BASH scripting Next we consider some flow of control Copyright © – Curt Hill


Download ppt "Copyright © 2016-2017 – Curt Hill Bash Scripting Fundamentals Copyright © 2016-2017 – Curt Hill."

Similar presentations


Ads by Google