0% found this document useful (0 votes)
101 views

6 CPS393 LinuxReview

Uploaded by

Amirali Eslami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

6 CPS393 LinuxReview

Uploaded by

Amirali Eslami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 143

C/CPS 393

Introduction to UNIX, C and C++


Prof. Alex Ufkes

Topic 6: Review!
Notice!

Obligatory copyright notice in the age of digital


delivery and online classrooms:

The copyright to this original work is held by Alex Ufkes. Students


registered in course C/CPS 393 can use this material for the purposes
of this course but no other use is permitted, and there can be no sale
or transfer or use of the work for any other purpose without explicit
permission of Alex Ufkes.

© Alex Ufkes, 2022 2


Course Administration

Midterm #2, covering all Linux material,


takes place in your lab on Nov 2/3

© Alex Ufkes, 2022 3


Today

Linux
Review

© Alex Ufkes, 2022 4


Week 1:
Basic Commands

© Alex Ufkes, 2022 5


Directory Navigation

cd <path> Change directory using absolute or relative path


cd .. Move up to parent directory
ls List contents of current directory (dir in Windows)
ls <path> List contents of <path>

© Alex Ufkes, 2022 6


Viewing File Contents
cd <path>
cd ..
cat <file> Display file contents, no paging
ls tac <file> Like cat, but lines in reverse order
ls <path>
pwd

© Alex Ufkes, 2022 7


Viewing File Contents
cd <path>
cd ..
more Like cat, but paged (cat displays everything at once)
ls spacebar gives next screenful, enter gets next line, q quits
ls <path>
pwd
less Like more, has extended commands/features. How ironic.
cat <file>
tac <file>

© Alex Ufkes, 2022 8


File Creation & Deletion
cd <path>
cd ..
cp <source> <dest> copies <source> to <dest>
ls works for directories as well
ls <path>
pwd
cat <file>
tac <file>
more
less

© Alex Ufkes, 2022 9


File Creation & Deletion
cd <path>
cd ..
cp <source> <dest> copies <source> to <dest>
ls works for directories as well
ls <path>
pwd
rm <file> deletes (removes) a file
cat <file>
tac <file>
more
less

© Alex Ufkes, 2022 10


File Creation & Deletion
cd <path>
cd ..
mkdir <name> Create a new directory called <name>
ls rmdir <name> remove/delete directory. Must be empty!
ls <path>
pwd
cat <file>
tac <file>
more
less
cp
rm

© Alex Ufkes, 2022 11


File Creation & Deletion
cd <path>
cd ..
mv <src> <dest> Dest can be
ls in a different directory, thus moving the file.
ls <path>
pwd
cat <file>
tac <file>
more
less
cp
rm
mkdir
rmdir

© Alex Ufkes, 2022 12


File Creation & Deletion
cd <path>
cd ..
wc <file> Gives size of file (lines, words, chars)
ls touch <file> Creates an empty file called <file>
ls <path>
pwd
If file exists, update modification time
cat <file>
tac <file>
more
less
cp
rm
mkdir
rmdir
mv

© Alex Ufkes, 2022 13


File Editing

Using vim, nano, etc.

© Alex Ufkes, 2022 14


Week 1:
Command Options

© Alex Ufkes, 2022 15


Linux Commands: Options

Adding options to a command modifies the


behavior of that command from the default.
Rather than having five similar commands,
have one command with five options.
Options follow the command itself, prefixed
with a dash -

© Alex Ufkes, 2022 16


Recursive Removal

rm r <dir>
dir
and all files and sub-dirs
© Alex Ufkes, 2022 17
Long Directory Listing

ls t sorts newest to oldest


ls R recursively lists all subdirs and files
ls p print / following directory names
ls l long list, more properties

Can combine options! These are all the same,


and order us usually irrelevant:
ls l t ls t l
ls lt ls -tl
© Alex Ufkes, 2022 18
Keyword Searching man Pages

Performs a keyword search for <query>


man k zip

These commands
can be looked up in
man as well!

© Alex Ufkes, 2022 19


Week 1:
Permissions

© Alex Ufkes, 2022 20


Owners and Groups

Each file and directory has an owner and a group


associated with it. This can be seen using ls -l:

Owner Group

Here, I, aufkes, am the owner of both


files, and the group is named aufkes.
© Alex Ufkes, 2022 21
Permissions

What about
this stuff?
file
means diMtᵈ% [ Capital
member
group

/
Group permission S if owner of
you lowercase
just
as are C a

you
→ . are
¢

u g o Each of:
owner (u) group members (g) other users (o)
I can read/write hello.txt have a set of :
read (r) write (w) execute (x)
permissions.
© Alex Ufkes, 2022 22
rwx

file: Can be looked at, copied


r dir: Can look at contents (with ls), but cannot view files within.
Would need r perm on file, and x perm on directory.

file: Can modify or delete the file


w dir: Can add or delete entries to/from directory.
A file in the directory can be modified without w on dir.

file: Can run it, if it is runnable (executable)


X dir: Can access entry in dir, if its name is known. Cannot list
contents. Weaker than r permission.

© Alex Ufkes, 2022 23


Changing Permissions

ls l, how can we change them?


Only the owner can change permissions, and
can do so using the chmod command

chmod (ugoa)(+-=)(rwx) name(s)

(ugoa) user, group, other, all


(+-=) + adds perm, - removes perm, = sets to given perm
(rwx) read, write, execute

© Alex Ufkes, 2022 24


chmod: Alternative Permission Format

Notice that rwx triple can be represented in binary:

--- (000) 0
--x (001) 1 Use a 3-digit octal literal to represent
-w- (010) 2 ugo permissions in one shot:
-wx (011) 3
chmod 160 myFile
r-- (100) 4
--x for user, rw- for group, --- for others
r-x (101) 5
rw- (110) 6 Not as intuitive, but potentially much
rwx (111) 7 faster to type

© Alex Ufkes, 2022 25


Week 1:
IO Redirection

© Alex Ufkes, 2022 26


Output Redirection

Redirect stdout to a file instead of monitor using > or >>

Interesting!
lsfile created before ls
command executes!
> will overwrite file if it
exists, create it if not.
>> appends if file exists

© Alex Ufkes, 2022 27


Input Redirection

Can redirect stdin also! And both at the same time!

wc input redirected from lsfile


(like calling wc with file arg,
subtle difference here)
Output redirected to wcfile

© Alex Ufkes, 2022 28


stderr Redirection

Use 2>

Why 2>?
Each stream given a number by shell.
Stdin = 0, stdout = 1, stderr = 2
ls >outfile is same as ls 1>outfile
cat <infile is same as cat 0<infile
Redirect stdout and stderr at
the same time using &>

© Alex Ufkes, 2022 29


Devices

Disks and printers and modems and so on

In Linux, we access devices as though they were (special) files


Typically found in directory /dev

/dev/stdin
stdin/stdout/stderr are: /dev/stdout
/dev/stderr

Displaying file contents on stderr could be done:


cp hello.txt /dev/stderr

© Alex Ufkes, 2022 30


/dev/null

Data goes in, cannot come back out.


Sending data to /dev/null throws it
away forever
/dev/null Common to send errors here
We know the errors are happening

Out of sight out of mind!

© Alex Ufkes, 2022 31


Week 2:
Glob Constructs

© Alex Ufkes, 2022 32


Glob Constructs

Globbing

Bash version of Regular Expression matching


Match over files, directory contents

When the shell encounters a glob construct:


1. Expand the construct
2. Execute the resulting command

? * [ ]
© Alex Ufkes, 2022 33
Match Any Single Character

This can be used to match any single character

? Multiple ? can match multiple characters

For example: The working directory contains the following:

lab1.txt lb2.txt new.txt pie.c


prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out

ls lab?.txt
lab1.txt lab2.txt lab3.txt lab4.txt

© Alex Ufkes, 2022 34


Match Any Single Character

? Substitute for multiple characters? Use multiple ?

lab1.txt lb2.txt new.txt pie.c


prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out

ls lab?.???
lab1.txt lab2.txt lab3.txt lab4.cpp
lab4.txt

© Alex Ufkes, 2022 35


Match Any Character Sequence

The * symbol matches any sequence of characters

* Includes the null string!

lab1.txt lb2.txt new.txt pie.c


prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out
ls lab?.*
lab1.txt lab2.txt lab3.txt lab4.cpp
lab4.txt

© Alex Ufkes, 2022 36


Match Any Character Sequence

lab1.txt lb2.txt new.txt pie.c

* prog1.c
top.c
ls *.c*
lab2.txt
lab4.txt
# What prints?
lab3.txt
lab4.cpp
prog2.c
a.out

© Alex Ufkes, 2022 Very common for filtering based on file extension 37
Match Specific Single Character

In square brackets we can specify a sequence of characters.

[ ] If one matches, the substitution will be successful.


lab1.txt lb2.txt new.txt pie.c
prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out

ls lab[123].txt
lab1.txt lab2.txt lab3.txt

ls lab[1-3].txt # Variation
lab1.txt lab2.txt lab3.txt
© Alex Ufkes, 2022 38
Match Specific Single Character

lab1.txt lb2.txt new.txt pie.c

[ ] prog1.c
top.c
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
prog2.c
a.out

ls lab[1-3].txt # Variation
lab1.txt lab2.txt lab3.txt
Can use a dash to specify a range of values.
[1-3], [A-Z], [a-z], etc.
Can also do [A-Za-z]
[A-z] is based on ASCII ordering.
Careful!
© Alex Ufkes, 2022 39
Exclude Specific Characters

More examples:
[ ] [!ABC]
[!0-9]
Matches any character except A,B,C
Matches anything except digits

What happens if nothing matches? Cannot access:

© Alex Ufkes, 2022 40


Ignoring Glob Constructs

What if we want to echo the glob construct, and not expand it?

Use quotes:

Can also
use \

© Alex Ufkes, 2022 41


Glob: Patterns & Regular Expressions

*(exp) 0 or more occurrences of exp

+(exp) 1 or more occurrences of exp

?(exp) 0 or 1 occurrences of exp

!(exp) Anything that does not match exp

© Alex Ufkes, 2022 42


What Prints?

A Ax Axxx Axxxx Ay X x X.bak xx xxxx

ls A*(x) A Ax Axxx Axxxx


ls A*(xx) A Axxxx
ls A+(x) Ax Axxx Axxxx
ls A?(x) A Ax
ls X?(.bak) X X.bak
ls @(*xx|*ak) Axxx Axxxx X.bak xx xxxx
ls !(@(*xx|*ak)) A Ax Ay X x

© Alex Ufkes, 2022 43


Week 2:
Basic Scripting

© Alex Ufkes, 2022 44


Hello, Bash!

Oh no!
Why is permission denied?
I created the script file.

© Alex Ufkes, 2022 45


Hello, Bash!

Much better!

© Alex Ufkes, 2022 46


Shell Script Arguments

Common comment
structure at the top of
a Bash program

Bash program will


terminate when it ends,
but can do so explicitly
as well with exit

© Alex Ufkes, 2022 47


Week 2:
grep

© Alex Ufkes, 2022 48


grep

Global Regular Expression Print


(Globally search for a regular expression and print it)

grep string filename(s)


String The string to search for
Filename(s) One or more files to search in
Result? Displays the lines of the file containing the string

grep string

Without a filename, grep will read from stdin until EOF.

© Alex Ufkes, 2022 49


Example: grep

© Alex Ufkes, 2022 50


grep: Options

-i Ignore case
-v Print lines not matching search string
-x search string must match entire line

© Alex Ufkes, 2022 51


grep: Metacharacters

. Substitutes for any character, except \n


Like ? In glob
0 or more repetitions of previous character
* Very much NOT like glob!

grep A*xx gfile.txt Axxx, Axxxx, xx, xxxx


grep .xx gfile.txt Axxx, Axxxx, xxxx
grep ..... gfile.txt Axxxx, X.bak
grep Ay* gfile.txt A, Ax, Axxx, Axxxx, Ay

© Alex Ufkes, 2022 52


grep: Metacharacters

. Beginning of line: pattern must


*
^ be at the start of the line
End of line: Pattern must be at
$ the end of a line

© Alex Ufkes, 2022 53


grep: Metacharacters

.
*
[...] Like glob, any single character inside brackets

^ [^...] Like glob, any single character not inside brackets


Note that ^ works in glob, can use ! or ^ in glob
$ ! May or may not work in grep regex

© Alex Ufkes, 2022 54


grep: Metacharacters

. \{m\} Exactly m repetitions of previous character


* \{m,\} At least m repetitions of previous character
\{m,n\} Between m and n repetitions of previous character
^
$ Consider additional flanking characters/expressions:
[...]
[^...] \{2\ AxxA
\{2,4\ AxxA, AxxxxA
\{1,\ Axa, AxxA, AxxxxA

© Alex Ufkes, 2022 55


grep: Metacharacters

.
* \< Beginning of word
^ \> End of word
$
[...]
[^...]
\{m\}
\{m,\}
\{m,n\}

© Alex Ufkes, 2022 56


Extended Regular Expressions

egrep or grep -E

| Logical OR:
grep - dog|cat|bird fname
Lines with at least 1 of dog, cat, or bird

+ 1 or more repetitions of previous character


grep - -Za- fname
Lines with at least one alpha char
(Recall * is zero or more)

© Alex Ufkes, 2022 57


Extended Regular Expressions

egrep or grep -E

() Performs substring grouping:


grep - fname
Two or three consecutive substrings ab
Lines containing AababB or AabababB

\n Backreferencing
\1 replaced with first substring, \2
replaced with 2nd substring, etc.
grep - aei]+)B\ myfile
Here, \1 gets replaced with ([aei]+)
© Alex Ufkes, 2022 58
Week 2:
find

© Alex Ufkes, 2022 59


Find: By Type

grep searches files, find searches the filesystem

find <path> -options <additional args>

find . -type f List all files rooted in current directory


find . -type d List all folders rooted in current directory

Recursively finds in subdirectories as well

© Alex Ufkes, 2022 60


Find: By Name

find /usr/[!c]* -name tali 2>/dev/null

Glob construct! -name Find by name, tali


Expands first. /usr/[!c]* Search /usr/*, but exclude
folders starting with c
2>/dev/null redirect stderr to /dev/null

Why redirect stderr?

Permission denied results in message to stderr, can clog terminal.

© Alex Ufkes, 2022 61


Find: By Name

Can also exclude using prune option:


cd /usr/courses/cps393/dwoit/courseNotes/Programs/c

find . -name c2 -prune -o -name "m*.c" -print

Does not recurse -print says print from


down dir ./c2 directories that recursed.
Otherwise, ./c2 itself is
still printed

© Alex Ufkes, 2022 62


© Alex Ufkes, 2022 63
Week 2:
head & tail

© Alex Ufkes, 2022 64


Head and Tail

Not too exciting:

head sends to stdout the first 10 lines of stdin


tail sends to stdout the last 10 lines of stdin

Redirect to use with files (can also pass file as arg):

head <myfile
# Displays first 10 lines of myfile on screen
tail <myfile >end.of.myfile
# Copies of last 10 lines of myfile into file called end.of.myfile

© Alex Ufkes, 2022 65


Head and Tail

Specify number of lines with options:


head -20 f1 # First 20 lines of file f1
tail -3 # Last 3 lines of stdin
option -n-X for head and -n+X for tail:

tail -n+3 f1 # all lines of f1 starting at line 3


head -n-2 f1 # all but the last 2 lines of f1

© Alex Ufkes, 2022 66


Week 3:
cut & paste

© Alex Ufkes, 2022 67


cut

Cut by column (character) using c option:


cut -c8 myfile # outputs 8th column of each line of myfile
cut -c5-7,25- # outputs columns 5,6,7,25... of each line of stdin

Cut by field using f option:


cut -f2 myfile # outputs second field of each line of myfile
(assumes fields separated by a tab)

Change field delimiter using d option:


cut -d' ' -f3 # outputs field 3, fields delimited by one space

© Alex Ufkes, 2022 68


paste

paste f1 f2

Line_x of stdout contains Line_x of f1, followed by tab, followed by Line_x of f2

If you want to save the results, simply redirect to a file.

© Alex Ufkes, 2022 69


© Alex Ufkes, 2022 70
Week 3:
Pipes

© Alex Ufkes, 2022 71


| Pipes |

A pipe connects stdout of one command to stdin of another.


Easy to understand once we know about streams and redirection.

ls | more # Lists all files in current dir in alpha


order, one screen at a time
ls -t | head # Lists the 10 most recently modified
files in this dir

Pipes make the shell very flexible!


No need to create new commands that emulate combined behavior.
No need to create (and later delete) temporary files.

© Alex Ufkes, 2022 72


Pipes & Tees

The tee command saves the data passing through a pipe.


Sends output to a file as well as the next command.

head <big.file | grep "paper"


VS.
head <big.file | tee first10 | grep "paper"

The same command, but first 10 lines of big.file are also saved in first10

© Alex Ufkes, 2022 73


Back to Scripts

#!/bin/bash
#source profs
#prints names of profs logged in
who: Gives who | grep dwoit | cut -c1-8 | uniq
list of users who | grep eharley | cut -c1-8 | uniq
logged in who | grep aabhari | cut -c1-8 | uniq
who | grep aufkes | cut -c1-8 | uniq
exit 0

uniq: remove duplicate lines


grep: Search for cut: Extract first from input (same user can be
a userid string 8 columns (max logged in more than once)
length of CS IDs)
© Alex Ufkes, 2022 74
Back to Scripts

Or, in a single-line command?

who | egrep "dwoit|eharley|aabhari|aufkes" | cut -c1-8 | sort -u

sort u: sort lines of input, removing duplication

© Alex Ufkes, 2022 75


With Argument?

#!/bin/bash
#source prof1
#prints names of a prof logged in
#name supplied as command line arg
who | grep $1 | cut -c1-8 | uniq

© Alex Ufkes, 2022 76


Week 3:
Transformers and Translators

© Alex Ufkes, 2022 77


Transformers & Translators: sed

We can also transform/modify a stream

Stream editor:
sed Can perform simple search and replace using the e option
-e indicates the string that follows is an edit command

sed s/Hello/HELLO

s indicates substitution
Hello String to search for
HELLO String to replace it with
© Alex Ufkes, 2022 78
Use i option to modify input file

© Alex Ufkes, 2022 79


Transformers & Translators: sed

sed

This command replaces the first occurrence on each line.


To replace ALL occurrences, do the following:

sed

© Alex Ufkes, 2022 80


Transformers & Translators: sed

sed -e "s/xx*/1234/" myfile


The same regular expressions we used
Changes first occurrence of x or xx or
with grep and be used with sed:
xxx etc on a line to 1234 in myfile

© Alex Ufkes, 2022 81


More Edit Commands: sed

sed e "1,5 s/HI/hi there/" f1.txt


Changes 1st occurrence of "HI" to "hi there" on lines 1-5

sed -e "/UNIX/ s/shell/Shell/" f1.txt


Changes first occurrence of "shell" to "Shell" only on
lines containing the string "UNIX"

sed -e "/UNIX/ d" deletes all lines containing UNIX


sed -e "1,5 d" delete lines 1-5

© Alex Ufkes, 2022 82


Translate: tr

Translates stdin, character by character, writes to stdout.


- -
Uses a 1-to-1 correspondence between them when replacing

tr ":" ";" <inf Changes all colons


into semicolons
Read from file inf,
From-string To-string write to stdout

© Alex Ufkes, 2022 83


Translate: tr

Translates stdin, character by character, writes to stdout.

Changes space to A
tr " x" "AX" <inf Changes x to X

Note:
If to-string is shorter than from-string, last character in
to-string is repeated implicitly until lengths are equal.
Excess characters in to-string are ignored

© Alex Ufkes, 2022 84


Translate: tr

Translates stdin, character by character, writes to stdout.

tr " x" "AX" <inf Space to A, x to X


tr "A-Z" "a-z" <inf Uppercase to lowercase
tr "a-zA-Z" "A-Za-z" Invert case
tr " " "\n" <inf Spaces to newlines

Can also use octal codes, precede with \

Change blanks to newlines


tr " x" "\012y" <inf \012 is octal for newline
Change x to y

© Alex Ufkes, 2022 85


Translate: Options

-d for delete:
tr -d "\n" <inf Remove all newlines
tr -d "a-z" <inf Remove all lowercase

© Alex Ufkes, 2022 86


Translate: Options

-s for squeeze:
Take all repeated occurrences of char1, replace with a single occurrence of char2

tr -s " " " " <inf


All strings of 1 or Replaced by
single space

© Alex Ufkes, 2022 87


Translate: Options

Can squeeze multiple characters:

tr -s "Ae" " Y" <inf


All strings of one or more "A"s are replaced by one space
All strings of one or more "e"s are replaced by one "Y"

© Alex Ufkes, 2022 88


Week 4:
Interactive & Scripted Functions

© Alex Ufkes, 2022 89


hello ( ) { They can be defined interactively,
then called interactively:
}

As a one-liner

© Alex Ufkes, 2022 90


Functions in Shell Programs

echo e option:
#!/bin/bash Enable interpretation of \
#Source: showLastLines
lastLine () {
echo -e "Last line of $1 is:\t\c "
tail -1 $1
}
lastLine $1 \c escape sequence in string
Causes echo to suppress further output.

Not universal. Does nothing in Python for example.

© Alex Ufkes, 2022 91


Functions as Aliases

What if I want echo to always have the e option turned on?

In .bash_profile (or type manually interactively)

echo () {
/bin/echo -e "$@" gives all args
}

Now, calling echo runs this function,


which calls built-in echo with e option

© Alex Ufkes, 2022 92


Deleting a Function

unset -f fname
Behavior of unset:
unset fn fn
If variable fn does not exist, unsets function fn.
Use f option to specify function instead of variable

© Alex Ufkes, 2022 93


Current Shell VS Sub-shell

Functions defined interactively (or in


.bash_profile) execute in the current shell.
Shell programs run in a sub-shell.

© Alex Ufkes, 2022 94


Variables

Like functions, they can be set interactively or declared in a Bash program

shell programs run in their own sub-shell


Individual shells have their own environments

© Alex Ufkes, 2022 95


Variables & Function Scope

Functions run in current shell


They can modify/access variables
defined in the current shell

© Alex Ufkes, 2022 96


Shell Variables

Seen previously when we talked about functions:

No declaration required, just assign value to name

myname=Denise # No spaces around = or interpreted as shell cmd


myname='Dr. Woit' # Quote if value contains spaces or special chars

{} optional, but
prevents ambiguity.

© Alex Ufkes, 2022 97


Shell Variables: Ambiguity?

fn=Denise ; ln=Woit ;
echo $fn$ln # DeniseWoit

Try printing an "M" between first and last name, as in: DeniseMWoit

echo $fnM$ln # Wrong. Why?


echo ${fn}M$ln # Correct
echo ${fn}M${ln} # Correct

© Alex Ufkes, 2022 98


Shell Variables: Indexed Arrays

Indexed Arrays:
students[0]=Bob
students[1]=Ha can just use directly
students[2]=Sue

Once again, { } removes


ambiguity
$students without index
gives first element

© Alex Ufkes, 2022 99


Shell Variables: Indexed Arrays

Arrays can be declared in one shot, seen below.


Use * as index to access entire array
Use length operator # to get array size
Used on discrete variables, # gives number of chars

© Alex Ufkes, 2022 100


Shell Variables: Associative Arrays

Must declare using


declare -A
Index can be any string

© Alex Ufkes, 2022 101


Week 4:
Quoting

© Alex Ufkes, 2022 102


Quoting

Required when argument contains whitespace.


Why? Shell will read this whitespace as a separator
between arguments.

grep Alex Ufkes myFile

VS this:

myFile

myFile

© Alex Ufkes, 2022 103


Quoting: Single VS Double Quotes

Single Quotes:
Most restrictive, nothing within is evaluated.
(Single quoted string passed to grep still evaluates regex)
Ignores Glob metacharacters, special characters, etc.

Using double quotes:


$ is evaluated

© Alex Ufkes, 2022 104


Backquote

The symbol above the tilde: `

Also evaluated in double quotes, but not single quotes.


But, more importantly, what does the backquote even do?
Performs command substitution.
A string in backquotes will be executed as a command.
Whatever that command printed to stdout will be
substituted into the larger expression.

© Alex Ufkes, 2022 105


Backquote

© Alex Ufkes, 2022 106


$(command)

$(command) is an alternative for `command`

Pipe date output into cut


Take first 10 characters
Passed as argument to echo

© Alex Ufkes, 2022 107


Week 4:
Logic & Conditions

© Alex Ufkes, 2022 108


Testing Files

test -x fname # Does fname have execute perms for user?


echo $? # 0 printed if yes; 1 if no
Options include: -r, -w, -f, -d, etc.
-r for readable, -w for writable, -f for regular file, -d for directory

© Alex Ufkes, 2022 109


Testing Files

Can logically connect multiple expressions using o (OR) and a (AND)

test -x fname -a -d bin

True if fname is executable AND bin is a directory

hello.txt is writeable AND


bashprogs is a directory

© Alex Ufkes, 2022 110


test: Alternative Syntax

Use square brackets [ ]

[ -w hello.txt -a -d bashprogs ]

© Alex Ufkes, 2022 111


Testing Value Equality

NAME="Alex" Problem:
test $NAME = "Alex" test $NAME = "Alex"
echo $? becomes: test = "Alex"
0 Results in an error.

Solution? Use quotes:

test "$NAME" = "Alex"


which becomes:
test "" = "Alex"

© Alex Ufkes, 2022 112


Integer Comparisons

test "$NAME" = "Alex"

= works for strings, for integers we use other options:

-eq Equal test 5 eq 7 # $? = 1


-ne Not equal test 5 ne 7 # $? = 0
-lt Less than test 5 lt 7 # $? = 0
-gt Greater than test 5 gt 7 # $? = 1
-le Less than or equal
-ge Greater than or equal

© Alex Ufkes, 2022 113


expr VS test

The expr command evaluates expressions, and prints TRUE or


FALSE (1 or 0) to stdout (not in $?), or numerical value

Supports more symbolic operators, as opposed to using options:

* / % + - = != < > <= >=


Produces Produces
integer boolean

© Alex Ufkes, 2022 114


© Alex Ufkes, 2022 115
Assign Result to Variable

Use backquotes, or $( )
c=`expr $a \* 5` # OR
c=`expr $a '*' 5` # OR
c=$(expr $a \* 5)
echo $c

© Alex Ufkes, 2022 116


Arithmetic Evaluation (in Bash)

We can use the expr command, but Bash supports it natively:

Oops, x+1 is read as a string!


Must declare as integer explicitly

Use typeset i
© Alex Ufkes, 2022 117
Arithmetic Evaluation (in Bash)

Can also use $(( ))

© Alex Ufkes, 2022 118


Week 4:
Control Structures

© Alex Ufkes, 2022 119


Control Flow

Sub-shell VS Current shell

Careful!
If you call a shell program within
{ }, it still has its own subshell.
Shell programs cannot change
calling environment
© Alex Ufkes, 2022 120
Crossing Streams

Both () and {} group stdout/stderr of multiple cmds into one stream:

(cat f1; cat f2) | grep Hello

Combined into single output stream


Combined stream piped into grep.
Without ( ), cat f1 prints in terminal,
cat f2 piped into grep

© Alex Ufkes, 2022 121


Conditional Execution

&& Tests for true and executes


cmd1 && cmd2 # execute cmd2 if cmd1 returns true (0)
|| Tests for false and executes
cmd1 || cmd2 # execute cmd2 if cmd1 returns false (1)

Combine with ( ) cmd1 || ( cmd2; cmd3 )

© Alex Ufkes, 2022 122


if / then / elif / else / fi

Examples:
if test -f fn1
then
cp fn1 fn5 #only done if fn1 is a file
fi

© Alex Ufkes, 2022 123


if [ -f fname -a -x fname ] ; then
echo fname is an executable file
else
echo fname is not executable and/or not a file
fi

x=dog # a string
if [ $x = "dog" ] ; then
echo YES
fi

One-liner, ; used as separator


© Alex Ufkes, 2022 124
#!/bin/bash
#Source: datecase
mnth=`date +%m` #formats it as mm (month)
case ${mnth} in
09)
echo "September"
;;
Case driven by
10) variable mnth
echo "October"
;;
11) Values to match
echo "November" followed by )
;;
12)
echo "December" Cases terminated by ;;
;;
*)
echo "some other month"
;;
esac
exit 0
Asterisk defines catch-all case
© Alex Ufkes, 2022 125
Combining Cases

Use |

case ${userdate} in
01|Jan|January) Assume userdate is a
echo "first month" variable defined earlier.
;; The first case will match if
02|Feb|February) o userdate=01
o userdate=Jan
;; o userdate=January
# etcetera
esac

© Alex Ufkes, 2022 126


For Loop

Semantically similar to high level languages

for variable in value1 value2 ... valuen


do
... # loop body code here
done
# loops n times
# 1st: $variable is value1
# 2nd: $variable is value2 etc.

© Alex Ufkes, 2022 127


© Alex Ufkes, 2022 rm i: Interactive, prompts for delete 128
#!/bin/bash Glob construct, any file
#source renamePgm
for i in *sample*
do
cp $i `echo $i | sed -e 's/sample/Ch1.sample/'` 2>/dev/null
if [ $? = "0" ]
then echo arg, pipe into sed
echo copied $i sed substitutes sample for
else Ch1.sample
echo could not copy $i Outputs to stdout, becomes
fi If command succeeded, exit 2nd arg to cp command
done status is zero.
Exit status stored in variable $?

© Alex Ufkes, 2022 129


While Loop

Like traditional HLLs, while loops are driven by conditions:

#!/bin/bash
#Source: list10
x=1 Initialize x to 1
while [ $x -le 10 ]
do Test x <= 10
echo $x
x=$((x+1)) Print and
done update x
exit 0

© Alex Ufkes, 2022 130


While Loop

Spy on your friends:

#!/bin/bash
#Source: profgone
# does nothing until prof logs off
# should run this in the background using &
while [ "`who | grep dwoit`" ]
do
sleep 60 # 1 minute
done
echo "she's finally gone \a\a"
exit 0
© Alex Ufkes, 2022 131
Week 5:
Odds & Ends

© Alex Ufkes, 2022 132


Reading

We can read from files, we can read from the keyboard

read line < fn

Variable to File to read from


read to Using redirection

By default, one line is read at a time from stdin


Returns nonzero value (in $?) if at EOF

© Alex Ufkes, 2022 133


Reading

Useful example? #!/bin/bash


Ask for forgotten if [ $# -eq 0 ]
arguments:
then
echo "Enter filename"
read fn
else
fn=$1
fi
cp $fn ${fn}.bak
exit 0
© Alex Ufkes, 2022 134
Clean way to read contents of a file!

© Alex Ufkes, 2022 135


Odds & Ends: xargs

Example: List lines containing 'bash' from all files whose NAME
starts with 'f', where files are located anywhere in the filetree
under current directory

find . -type f -name "f*" | xargs grep bash

By type: By name:
Find in file starting Output piped into xargs,
current which calls grep with that
directory output as args to grep

© Alex Ufkes, 2022 136


find . -type f -name "f*" | xargs grep bash

-VS-
find . -type f -name "f*" | grep bash

The one without xargs is going to grep on the output


stream of find, NOT the files themselves!
It will find file NAMES that contain bash, not file CONTENTS

© Alex Ufkes, 2022 137


More Built-ins: eval

Consider the following variable:


v1="cat hello.txt | grep Alex"
Suppose we want to run this
as a command? Try: $v1

cat hello.txt succeeds,


but fails at the |
Because the | is not
recognized as a pipe
© Alex Ufkes, 2022 138
More Built-ins: eval

Consider the following variable:


v1="cat hello.txt | grep Alex"
Use eval instead:

© Alex Ufkes, 2022 139


More Built-ins: eval

Another: v1="cat hello.txt | grep 3"


v2="grep 9 | more "

We want to execute: cat hello.txt | grep 3 | grep 9 | more

$v1 | $v2 | inside v1 and v2 not


recognized as pipes

eval "$v1 | $v2" Eval makes two passes:


1) Make variable substitutions:
eval "cat fn
2) Result evaluated, meta chars etc. interpreted

© Alex Ufkes, 2022 140


More Built-ins: eval

eval "$v1 | $v2"

© Alex Ufkes, 2022 141


Linux & Bash

© Alex Ufkes, 2022 142


© Alex Ufkes, 2022 143

You might also like