CIRC Summer School 2017 Baowei Liu 7.25.2017 Bash Scripting CIRC Summer School 2017 Baowei Liu 7.25.2017
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
Conditional Expression and if if conditional expression then …. else fi
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 \> \<
Integer Comparison Suggestion: Always use (( )) : for integer comparison > < in [[ ]] is for string comparison!! Example: compareIntegers.sh
Strings Comparison Compare strings: [[ “$a” = “$b” ]] White spaces around [[ ]], [] and operators are necessary!! Double-quoting variables! Examples: compareStrings.sh; compareStrings2.sh
Compound Operators &&, || if [[ … ]] && [[ … ]] then …. fi
Loop Constructs –while loop Conditional Expression while [[ conditional expression ]] do …. done Arithmetic Expansion while (( arithmetic expression )) …
Loop Constructs –until loop Conditional Expression until [[ conditional expression ]] do …. done Arithmetic Expansion until (( arithmetic expression )) …
Flow Control Constructs: Case case expression in pattern1) statement;; pattern2) *) …. esac ) ;; and *
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
Substring Extraction ${string:position:length} ${string:position} Example:substring_extraction.sh
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
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
Common Use Get extension: ${filename##*.} Get filename without extension ${filename%.*} Get extension: ${filename##*.} Get directory name: ${pathname%/*} Get filename: ${pathname##*/} Example: FileName/fileName.sh
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
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