Date post: | 27-Dec-2015 |
Category: |
Documents |
Upload: | bertina-leslie-underwood |
View: | 220 times |
Download: | 2 times |
Exam Details
• Time 90 minutes (1hour 30 minutes).• Six questions!• How long per question?• Five parts per question. • How long for each part?• You can take the unix sheet into the exam. • You can READ the exam paper at the start
(before the exam starts – so get there early).
• No questions on History • Basics of commands, redirection, permissions,
processes.• awk, egrep and regular expressions. • Lots of general questions.
pathnames• Special symbols can be used in pathnames.. the directory one level above this../.. the directory two levels above this. the current directory~ this user’s home directory~user a specific user’s home directory* wildcard matching any string? wildcard matching any single
character
Relative and Absolute Pathnames Revisited
(root)
staff usr bin stud etc
Research Teaching Private
pgugitmastersxxxgtrxxx
CUA
Coursework1.txt
CUA
xxx02u xxx04u
MVR
Lecture1.ppt
Lecture2.doc
.profile
/
../../.. /stud
You are here!
/stud/ug/xxx04u..
/stud/ug../..
../../../..
~ ~xxx04u
Relative Absolute
. /stud/ug/xxx04u/CUA
Permissions lists• Specify who can do what• There are three kinds of who:
u the user (owner)g members of the user’s groupo others - anyone else
• There are three kinds of what:r readw writex execute
Setting and changing permissions -chmod
• chmod mode pathname is used to alter permissions lists
• Mode specifies a sequence of changes, each of the form who operation permission– who is u, g or o– operation is + (grant) or - (revoke)– permission is r, w or x
• Examples:chmod o-r g-r plan.docchmod u+rwx Admin
The mode can also be a three digit octal number that is interpreted as a sequence of nine bits to set the whole permissions list at once:– chmod 644 progress.txt
644 is 110 100 100 which is interpreted as rw- r-- r--
– chmod 777 progress.txt777 is 111 111 111 which is interpreted rwx rwx rwx
– chmod 400 progress.txt400 is 100 000 000 which is interpreted as r-- --- ---
Setting and changing permissions – chmod (2)
Setting and changing permissions – chmod (3)
• Files are created with a default permission– usually -rw- r-- r--– depends upon the command used to
create the file– set using the umask command
Finding Files• find searches in a directory hierarchy• find <starting directory> -name <filename> -
$ find /usr/share/doc/ -name 'post*' -print/usr/share/doc/postgresql-7.4.13/usr/share/doc/postgresql-7.4.13/html/postmaster-shutdown.html/usr/share/doc/postgresql-7.4.13/html/postmaster-start.html$$ find . -name '*.txt' –print (recently can do without print)
Regular Expressions
• grep “That” poem will only find the string “That” in poem if it has an upper case ‘T’ followed by lower case ‘hat’
• Regular expressions are much more powerful notation for matching many different text fragments with a single expression– i.e. could wish to find “That”, “that”,
“tHaT”, etc.
Regular Expressions (2)
• Search expressions can be very complex and several characters have special meanings– to insist that That matches only at the start of
the line use grep “^That” poem– to insist that it matches only at the end use
grep “That$” poem– a dot matches any single character so that
grep “c.t” poem matches cat, cbt, cct, etc.
• Square brackets allow alternatives:– grep “[Tt]hat$” poem
• An asterisk allows zero or more repetitions of the preceding match– grep “^-*$” poem for lines with only -’s
or empty– grep “^--*$” poem for lines with only -’s
and at least one -– grep “Bengal.*Sumatra” poem for lines
with Bengal followed sometime later by Sumatra
• Many flags to: – display only number of matching lines,
ignore case, precede each line by its number on the file and so forth
Regular Expressions (3)
STDOUT
• Stands for standard output• This is where programs (usually) write any
output they generate. By default STDOUT appears in your terminal window
• If you want to save the output to a file instead, use > – The file will be created– If the file already exists then it will be
overwritten
• You can also use >> which appends the output onto a file’s contents
STDERR
• Stderr stands for standard error• This is where programs usually write
error messages. So even if you are redirecting the normal output to a file, you can still see error messages on the screen
• You can redirect STDERR using 2>
Aliases (bash)
• To define shorthand for complex commands
• alias name definition defines an aliasalias hist=historyalias ls='ls -F'
• alias alone shows you current aliases• unalias name removes an alias• unalias –a removes an alias
Killing Processes• Use top• Alternatively, use kill– kill <signal number> <PID>
• E.g. kill -15 25718– “-15” is the signal number – here, it means
“stop the process cleanly” (i.e. close any files it is using)• More about signals later…
– “-9” means “kill the process whatever”• Useful if all else fails!
• killall <signal number> <process name> will send the signal to every process with that name.
Job Control (2)
• jobs allows you to:– Bring a job to the foreground• fg %<job number>
– Run a job in the background• bg %<job number>
– Suspend a job• stop %<job number>
– Terminate a job• kill %<job number>
Control Key Sequences for Processes
• Some control sequences affect processes:– Ctrl-C - kill a process– Ctrl-D - exit a shell (send EOF)– Ctrl-S - suspend or pause the display of
output– Ctrl-Q - resume or continue output
from Ctrl-S
PATH
• To add the location of your scripts dir to your path
• echo $PATH• export PATH="$PATH:~/scripts“• echo $PATH
execution permissions
• chmod u+x myScript.sh• to give execution permissions to user. • sh -x myScript.sh; shows which command is
executed. • you can enclose parts of script with
Using awk
• I could then invoke Awk to list all the gold pieces as follows:
• awk '/gold/' coins.txt • This example demonstrates the simplest
general form of an Awk program: • awk <search pattern> {<program actions>}
NR (number of records)
• The next example prints out how many coins are in the collection:
• awk 'END {print NR,"coins"}' coins.txt • This yields: • 13 coins
general form of an Awk program
• the general form of an Awk program to: • awk 'BEGIN {<initializations>} • <search pattern 1> {<program actions>}
<search pattern 2> {<program actions>} ... END {<final actions>}'
• awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt
• This yields: (note ounces is user defined)• value = $2592.5• Instead of doing it all from the command line• We can do it all from a file, • With the following syntax• awk -f <awk program file name>
AWK PROGRAM EXAMPLE
• Instead of doing it all from the command line• We can do it all from a file, • With the following syntax• awk -f <awk program file name>• http://www.vectorsite.net/tsawk_1.html#m1
• For example: /^[^a-zA-Z0-9]/ -- matches what.
• A "|" allows regular expressions to be logically OR-ed.
• For example: /(^Germany)|(^Netherlands)/ -- matches what.
• For example: /wh./ -- matches what.
a (possibly signed) integer number.
• /^[+-]?[0-9]+$/ -- matches any line that consists only of a (possibly signed) integer number.
• /^ Find string at beginning of line. • /^[-+]? Specify possible "-" or "+" sign for
number.• /^[-+]?[0-9]+ Specify one or more digits "0"
through "9". • /^[-+]?[0-9]+$/ Specify that the line ends with
the number.