Week 2
globbing, grep, and intro to bash scripting
Glob Constructs, Meta-Chars, & Special-Chars
globbing
: used for matching or expanding specific types of patterns
mostly used to match filenames or searching for content in a file.
uses wildcard characters (
?
,*
,[]
,^
,!
,$
,{}
,|
,) to create the pattern:?
- matches single character (1-1)ex. match any 4-char .txt file :
ls ????.txt
ex. match any file w/ 4-char name & 2-char ext :
ls ????.??
ex.
ls week?.txt
can match week1.txt, ... weekN.txt
*
- matches N-characters (1-N)ex. match any file w/ .tsx ext :
ls *.tsx
ex. match any file that starts w/ "a" :
ls a*.*
[]
- matches any character from rangerange of:
all uppercase alphabets :
[:upper:] or [A-Z]
all lowercase alphabets :
[:lower:] or [a-z]
all numeric alphabets :
[:digit:] or [0-9]
all alphabets :
[:alpha:] or [a-zA-z]
all the above:
[:alnum:] or [a-zA-Z0-9]
ex. match any file that starts w p/q/r/s :
ls [p-s]*
ex. match any file that starts w 1/2/3/4/5 :
ls [1-5]*
ex.
ls prog[1-58].c
=> possible: prog1.c, prog34.cex.
ls *[-]*
=> possible: prisma-api.md, redux-tk.md
!
- exclude[!ABC]
: matches any single charexcept A, B or C
[!a-z0-4]
: matches any single charexcept a,...z, 0, ...4
Character Classes
[:alnum:] [:alpha:] [:blank:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:]
ex. match any file that starts w/ a A-Z :
ls [[:upper:]]*
ex. combine multiple classes:
ls lab[[:digit:][:lower:]].*
(0-9 & a-z)
Specials Characters:
~
: your home directory name (/home/dwoit in my case)~usr
: the home dir of user with userid "usr"~-
: your previous working dir (note in bash cd - same as cd ~-)~+
: your current working dir\
: ignore glob construct following (like "" or '')ex.
echo /[abc]*
outputs:[abc]*
\
: at end of line, means command continues on next line
Patterns & RegEx
*(exp)
: 0 >= occurrences of exp+(exp)
: 1 >= occurrences of exp?(exp)
: 0 / 1 occurrences of exp!(exp)
: anything that does not match exp@(exp1|exp2|...)
: anything that matches exp1 or exp2 or ...
Bash Scripting
Running .sh
Scripts
Alternatively, in your myscript.sh
file, do the following
and run it with...
Arguments
Arguments can be passed into a script as you would w/ any bash command.
$#
: number of args (3)$n
: param name (n
is the number)eg.
$1 = "foo"
$*
: One string with all parameter names"foo bar baz"
$@
: Comma seperated strings for each parameter name"foo", "bar", "baz"
shift
: Shifts Positional Parameters by 1
Grep
Global Regular Expression Print
syntax :
grep string filename(s)
string
: string to search for
filename(s)
: files to search string in
w/o file, grep will read from stdin until EOF
Displays the lines of the file containing the string
Options
-i
: ignore case-v
: print liens not matching search string-x
: search string must match entire line
Metacharacters
.
: like?
in glob (subs in any char)*
: 0 or more reps of previous char (not like*
in glob)^
: patten must be at the start of the line (ie."^Raiders"
)$
: pattern must be at the end of a line (ie."Chargers$"
)\{m\}
: exactly m reps of previous char (ie."x\{3\}"
)\{m,\}
: atleast m reps of previous char\{m,n\}
: between m to n reps of previous char (inclusive)\<
: line has word that starts w/ character (ie."\<x"
)\>
: line has word that ends w/ character (ie."x\>"
)
Last updated