CIRC Summer School 2017 Baowei Liu 7.25.2017 Bash Scripting CIRC Summer School 2017 Baowei Liu 7.25.2017
Stdin, stdout and stderr File descriptor(FD): a handle to access a file Stdin: standard input stream (e.g. keyboard) –FD:0 Stdout: standard output stream (e.g. monitor) –FD:1 Stderr: standard error output stream (usually also on monitor) ) –FD:2
I/O Redirection: Redirecting to a file Redirect between files including the three file descriptors, stdin, stdout and stder Redirecting output: date > file Appending redirecting output: date >> file Redirecting input: Command < input
I/O Redirection: Pipes Pipes connect the standard output of one command to the standard input of another with the pipe symbol |. command1 | command2
Positional Parameters Arguments given to bash script when it is invoked $1, $2 … $N. ${N} if N>9 $0 is the base-name of the script $# is the total arguments given Example: shareFiles.sh positionalParameters.sh
grep grep: search for matches to a pattern in a file and print the matched line to stdout grep Pattern file Pattern could be simple strings or regular expressions
Regular Expression Regular Expression: globbing pattern used for text . : Equivalent to ? in filename expansion * : zero or more times, aa* will match a,aa,…but not ab .*: any string. Equivalent to * in filename expansion ^: starting with, ^ab $: ending with, ab$
Regular Expression []: “-” \< \>: exact word
sed and Regular Expressions Stream editor for parses and transform text sed ‘s/abc/xyz/’ File: first occurrences sed ‘5,10s/abc/xyz/’ File: range, specified lines Sed ‘0~2 s/abc/xyz/’ File: Every other line starting 0 or even lines
sed and Regular Expressions Word Characters: Alphanumeric characters plus “_” [A-Za-z0-9_] Replace all occurrences in a line
More Examples Wrappers
Project Write a Bash script to black out sensitive information buried in all but the .data files under files/ (i.e. ONLY the html,xml,txt files) Key tech: editor, chmod, command substitution, for loop, echo, case/if, string compare, substring remove, sed, regular expression, i/o redirection