6 CPS393 LinuxReview
6 CPS393 LinuxReview
Topic 6: Review!
Notice!
Linux
Review
rm r <dir>
dir
and all files and sub-dirs
© Alex Ufkes, 2022 17
Long Directory Listing
These commands
can be looked up in
man as well!
Owner Group
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
--- (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
Interesting!
lsfile created before ls
command executes!
> will overwrite file if it
exists, create it if not.
>> appends if file exists
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 &>
/dev/stdin
stdin/stdout/stderr are: /dev/stdout
/dev/stderr
Globbing
? * [ ]
© Alex Ufkes, 2022 33
Match Any Single Character
ls lab?.txt
lab1.txt lab2.txt lab3.txt lab4.txt
ls lab?.???
lab1.txt lab2.txt lab3.txt lab4.cpp
lab4.txt
* 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
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
[ ] 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 if we want to echo the glob construct, and not expand it?
Use quotes:
Can also
use \
Oh no!
Why is permission denied?
I created the script file.
Much better!
Common comment
structure at the top of
a Bash program
grep string
-i Ignore case
-v Print lines not matching search string
-x search string must match entire line
.
*
[...] Like glob, any single character inside brackets
.
* \< Beginning of word
^ \> End of word
$
[...]
[^...]
\{m\}
\{m,\}
\{m,n\}
egrep or grep -E
| Logical OR:
grep - dog|cat|bird fname
Lines with at least 1 of dog, cat, or bird
egrep or grep -E
\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
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
paste f1 f2
The same command, but first 10 lines of big.file are also saved in first10
#!/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
#!/bin/bash
#source prof1
#prints names of a prof logged in
#name supplied as command line arg
who | grep $1 | cut -c1-8 | uniq
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
sed
sed
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
-d for delete:
tr -d "\n" <inf Remove all newlines
tr -d "a-z" <inf Remove all lowercase
-s for squeeze:
Take all repeated occurrences of char1, replace with a single occurrence of char2
As a one-liner
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.
echo () {
/bin/echo -e "$@" gives all args
}
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
{} optional, but
prevents ambiguity.
fn=Denise ; ln=Woit ;
echo $fn$ln # DeniseWoit
Try printing an "M" between first and last name, as in: DeniseMWoit
Indexed Arrays:
students[0]=Bob
students[1]=Ha can just use directly
students[2]=Sue
VS this:
myFile
myFile
Single Quotes:
Most restrictive, nothing within is evaluated.
(Single quoted string passed to grep still evaluates regex)
Ignores Glob metacharacters, special characters, etc.
[ -w hello.txt -a -d bashprogs ]
NAME="Alex" Problem:
test $NAME = "Alex" test $NAME = "Alex"
echo $? becomes: test = "Alex"
0 Results in an error.
Use backquotes, or $( )
c=`expr $a \* 5` # OR
c=`expr $a '*' 5` # OR
c=$(expr $a \* 5)
echo $c
Use typeset i
© Alex Ufkes, 2022 117
Arithmetic Evaluation (in Bash)
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
Examples:
if test -f fn1
then
cp fn1 fn5 #only done if fn1 is a file
fi
x=dog # a string
if [ $x = "dog" ] ; then
echo YES
fi
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
#!/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
#!/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
Example: List lines containing 'bash' from all files whose NAME
starts with 'f', where files are located anywhere in the filetree
under current directory
By type: By name:
Find in file starting Output piped into xargs,
current which calls grep with that
directory output as args to grep
-VS-
find . -type f -name "f*" | grep bash