CIRC Summer School 2016 Baowei Liu Bash Scripting CIRC Summer School 2016 Baowei Liu
Day 3 Examples ssh to bluehive cd your directory for this class ./shareFiles.sh Day3
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
Filename Expansion / Globbing Expanding filenames containing special characters Wild cards * ? Square brackets [set]: “-” Special characters: ! (other than) Escaping backslash: protect a subsequent special character
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 .*: any string. Equivalent to * in filename expansion * : zero or more times, a* will match a,aa,…but not ab ^: 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: All 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
Project Write a Bash script to black out sensitive information buried in the files under files/ ./shareFiles.sh finalProject Key tech: editor, chmod, command substitution, for loop, echo, sed, regular expression, i/o redirection