Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIRC Summer School 2017 Baowei Liu

Similar presentations


Presentation on theme: "CIRC Summer School 2017 Baowei Liu"— Presentation transcript:

1 CIRC Summer School 2017 Baowei Liu 7.25.2017
Bash Scripting CIRC Summer School 2017 Baowei Liu

2 source, . VS execute 1. builtin command in Linux.
source filename [argument] 1. builtin command in Linux. 2. read and execute commands from filename in the current shell environment 3. usually used for setting environment variables . Filename [argument] synonymous to source in Bash Make it executable and execute script start a new process when running and close when completed

3 Conditional Expression and if
if conditional expression then …. else fi

4 Conditional Expression and Operators
[[ conditional expression ]] : new, supported by Bash [ … ] : traditional/standard, portable (( … )) : arithmetic operations. (( $a > $b )) ==, !=, <, <=, >, >= (white space are not critical) [[ ”$a” –gt “$b” ]] –gt –le, =, !=, >, <, (white space are critical [ “$a” \> “$b” ] –gt, –le \> \<

5 Integer Comparison Suggestion:
Always use (( )) : for integer comparison > < in [[ ]] is for string comparison!! Example: compareIntegers.sh

6 Strings Comparison Compare strings: [[ “$a” = “$b” ]]
White spaces around [[ ]], [] and operators are necessary!! Double-quoting variables! Examples: compareStrings.sh; compareStrings2.sh

7

8 Compound Operators &&, || if [[ … ]] && [[ … ]] then …. fi

9 Loop Constructs –while loop
Conditional Expression while [[ conditional expression ]] do …. done Arithmetic Expansion while (( arithmetic expression ))

10 Loop Constructs –until loop
Conditional Expression until [[ conditional expression ]] do …. done Arithmetic Expansion until (( arithmetic expression ))

11 Flow Control Constructs: Case
case expression in pattern1) statement;; pattern2) *) …. esac ) ;; and *

12 Strings and Manipulation
Create a string: string variable Display a string: $stringName Length of a string: ${#stringName} Substring: a Bash string just holds one element Compare strings Concatenate of string: = or += Substring extraction Substring replacement

13 Substring Extraction ${string:position:length} ${string:position}
Example:substring_extraction.sh

14 Substring Replacement
${string/substring/replacement} ${string//substring/replacement}: replace all matches ${string/#substring/replacement}: replace one match from the front ${string/%substring/replacement}: replace one match from the end String match/replacement can be parameterized

15 Substring Removal ${string#substring}: Remove the shortest match from the front ${string##substring}: Remove the longest match from the front ${string%substring}: remove the shortest match from the back ${string%%substring}: remove the longest match from the back Example: rm_substr.sh

16 Common Use Get extension: ${filename##*.}
Get filename without extension ${filename%.*} Get extension: ${filename##*.} Get directory name: ${pathname%/*} Get filename: ${pathname##*/} Example: FileName/fileName.sh

17 Filename Expansion / Globbing
Expanding filenames containing special characters Wild cards * ? Square brackets [set]: “-” Special characters: ^ (exclude/ other than) Escaping backslash: protect a subsequent special character

18 Project Write a Bash script using if/case to count how many files there are in files/ for each of these types: txt, html, xml, data Key tech: editor, chmod, command substitution, for loop, arithmetic expansion, echo, case or if, string compare, substring removal


Download ppt "CIRC Summer School 2017 Baowei Liu"

Similar presentations


Ads by Google