CNIT 34010 final exam
cat file | grep <search term>
"globally regular expression print" filters stuff by your input
get a variable
$var
get a value from an array
${array[n]}
preferred method of string processing
${}
defining a variable
- cannot start w a number can have numbers, alpha, underscores
paste -d"-" animals age
-d delimits, following char is how they are put together outputs dog-6 cat-4
success return code
0
$@
all of the arguments
-a -o
and, or
failed return code
anything not 0
$n
argument as a positive integer
$ (( 5 + 4 ))
arithmetic substitution/outputs 9 lol
!
bang, negates
case "$CMD" in sup*) ;; *) echo cya ;; esac
case statement
tr <input chars> <output chars> < file
changes input to the output
alpha, lower, upper, digit, alnum, cntrl, punct, blank, space
character sets (cntrl: ascii 0-37, del) (blank: tabs and spaces) (space: vertical and horizontal space)
`<command>` or ${<command>}
command substitution 2nd method is best practice
tr '[:lower:]' '[:upper:]' < file
converts to upper case, using character sets
sort file | uniq -c
counts unique words
:=
default value ASSIGNMENT if unset, it becomes this.
:-
default value substitution if unset, original value DOES NOT turn into this
tr -d (ex: tr -d " " < file)
delete characters(in this case: spaces)
-d filename
directory
if [[ <statements> ]]
double bracket test more versatile and familiar with syntax
-eq
equal
\
escape character
!
negate
what does -n do
no execute, reads lines and checks for syntax (debugging)
VAR=${#array[@]}
VAR = length of array
cut -c <chars> <file>
includes the output u tell it to based on the chars/length
[ -n STRING ]
length of string is nonzero
-lt
less than
-le
less than r equal to
sort file | uniq
lists only unique entries(ONLY CONSECUTIVE)
FILES=($(ls))
ls array
FILES=$(ls)
makes ls a string var
[char set]
matches the characters stated one time ex: [a-zA-z] ls Chapter[0-9].doc
expr index <string> <regex>
returns the position of the first matching char in the of the regex in the string
sort -r filename
reverse order
tail -n # filename
same as head but opposite
set +x
shell tracing
if [ <statement> ]
single bracket test not as good as [[ ]]
sort filename
sorts alphabetically
if then elif then else fi
standard if syntax
if then fi
standard if syntax
[ -z STRING ]
string is zero
[ STRING1 = STRING2]
strings are equal (note: dont use -eq or == )
:+
substitute when set ex: echo ${DEBUG:+"Program running in debug mode"}
$0
the name of the script being executed
" and '
they hold shit like spaces
tr
transliterate
A?B[0-9]*)
u can use wildcards/regexes in case statements
delete a variable
unset VAR cannot unset readonly variables
debugging: -v
verbose (prints lines)
*
wildcard (matches 0 or more times)
${string##regex}
LONGEST match to the regex, from the FRONT of the string
expr match <string> '<regex1>\(<regex2>\)<regex3>'
Returns from regex2 from string where: regex1 comes immediately before regex2 regex3 comes immediately after regex1 and/or regex3 can be omitted
debugging: set +n
TURNS DEBUGGING OFFF +n = OFF
debugging: set -n
TURNS DEBUGGING ON -n = ON
-e file
exists
$?
exit status of previous function(0, 1, 2..)
cut -f
field number as separated by the d character cut -c only works on fixed length fields
-f filename
file
[ file1 -nt file2 ]
file1 is newer than file2
[ file1 -ot file2 ]
file1 is older than file2
-s filename
filename exists and size is greater than 0
globbing
filename substitution(* ? [charset])
?
fills in the spot for one character
BASH_REMATCH[0]
first matched result in BASH_REMATCH array
${array[*]}
gets all elements, elements are separated by spaces
${array[@]}
gets all elements, includes elements with spaces
${#string}
gets length of string
who | cut -c1-6
gives you first 6 characters
-gt/-ge
greater than/greater than or equal to
${string/%regex1/regex2}
if regex1 matches BACK of string, replaces it with regex2
-ne
not equal
:?
null value thingy checker, if a value gets unset this msg prints to stderr ${name:?hi wtf}
$#
number of arguments
sort -n filename
numerical order
getopts
parses the command line to find options
String processing - expr and ${}
perform operations on string variables
our regexes are what type??
posix compliant
\$FRED
prints out literally "$FRED"
head -n # filename (ex: head -n 5 mydoc)
prints the first 5 lines of the file
paste file1 file2
puts 2 files together by line
make a variable read only
readonly VAR
[[ $string =~ $REGEX ]]
regex equality/comparison operator
scalar
regular var
sort -u filename
removes duplicates(non consecutive, all output)
shift
removes the next argument from $* and $@
${string/#regex1/regex2}
replaces first match(regex1) to the front of the string with regex2
tr -s (ex: tr -s a i < file)
replaces repeated characters (aa to a)
expr match <string> <regex>
returns LENGTH of matching regex at the beginning of <string>
${string%%regex}
returns LONGEST match from the BACK of the string
expr substr <string> <position> <length>
returns Length characters from string starting at Position
${string%regex}
returns SHORTEST match from the BACK of the string
${string/regex1/regex2}
returns String with the 1st match to regex, replaced by regex2.
${<string>:<position>}
returns remaining substring at position ("helloworld":5)->world
${string#regex}
returns shortest match to regex, starting from the FRONT of the string
${string/regex1//regex2}
returns string with ALL matches to regex1 replaced by regex2