90 likes | 209 Vues
This lecture notes outline for CS311 provides an in-depth overview of Bash scripting fundamentals, including shebang lines, variable usage, and flow control statements. Learn to apply grep for pattern matching and understand regular expressions, including character sequences and usage in scripts. Additionally, the notes cover essential utilities like sed for stream editing. These resources are designed to enhance your understanding of scripting and text manipulation within operating systems. Make sure to refer to your existing notes for a more personalized learning experience.
E N D
CS311 – Lecture 04 Outline • Bash scripting • Understand the script2 script • Understand the myWC script • grep - find pattern matches • REGular EXpressions • sed: stream editing Note: These lecture notes are not intended replace your notes. This is what I expect your notes to already look like, but with more explanation as needed. CS311 – Operating Systems 1 1
Bash scripting language #! /bin/sh shebang lines ' vs " vs \ quoting ` ` command substitution $# bash variable for how many command line args were given $1 bash variable that retrieves the 1st command line arg. $USER environmental variable, your username exit bash builtin for exiting bash CS311 – Operating Systems 1 2
Flow control statements Lecture 02 • if statements • use test utility to test for equality or < or > or if a file exists. • for loops CS311 – Operating Systems 1 3 3
grep - find pattern matches Grep filters lines of text based on patterns grep [options] pattern [file] This util allows a filename. But, of course, you can send it input through pipes or <. CS311 – Operating Systems 1 4
REGular EXpressions Regular expressions are character sequences that describe a family of matching strings every letter matches itself ^ - start of line, or negation of characters (if it is in []) $ - end of line . - any character [] – range, or set of characters * - any number of times + - at least once ? - exactly once CS311 – Operating Systems 1 5
REGular EXpressions echo "cowboys" | grep ".*" echo "cowboys" | grep ". . . . . . ." echo "cowboys" | grep "c.*s" echo "cowgirls" | grep "c.*s" echo "tomboys" | grep "c.*" echo "aaabb" | grep "^a*b*$" echo "bbbb" | grep "^a*b*$" echo "ccbbb" | grep "^a*b*$" echo "aabbb" | grep "^a+b*$" echo "bbb" | grep "^a+b*$" yes yes yes yes no yes yes no yes no CS311 – Operating Systems 1 6
REGular EXpressions echo "cowboy" | grep "^[a-z]*$" echo "cowboy123" | grep "^[a-z]*$" echo "cowboy123" | grep "^[a-z]*[0-9]*$" echo "cowboy" | grep "^[bcowy]*$" yes no yes yes Lecture 04 CS311 – Operating Systems 1 7 7
grep - find pattern matches • Useful options • -E • -c • -o • -n • -i CS311 – Operating Systems 1 8
sed -r "s/regex/text/g" filename substitute every occurrence of the regular expressions regex by the string text. CS311 – Operating Systems 1 9