awk
The final exam will be held on Tuesday, May 14th from 3:00 - 6:00 PM.
The exam will be given in this room.
If for some reason you are not able to take the Final at the time it will be offered, you MUST send an email to me before the exam so we can make alternative arrangements.
60% of the points on this exam will consist of questions from the Ungraded Class Quizzes.
You do not need to study a Class Quiz question if the topic is not mentioned in either the Midterm or Final review.
The other questions I will make up specifically for this exam.
For these questions you will have to know
The last class on Thursday, May 2nd, will be a review session.
You will only be responsible for the material in that review session, which you will find here, and the review for the Midterm, which you will find here.
Although the time alloted for the exam is 3 hours, I would expect that most of you would not need that much time.
The final is a closed book exam.
To prevent cheating, certain rules will be enforced during the exam.
Remember, the Midterm and Final determine 50% of your grade.
At the end of each semester we offer you the opportunity to say what you think about this course.
What have I done right?
What have I done wrong?
What can I do better?
You are not asked for you name.
So the submissions are anonymous.
I will not see your responses until after I have submitted grades for this course.
We collect this feedback through Course Evaluations.
I will use what you say to make this course better.
To complete the course evaluation, use the following link .
You have until Friday, May 24th, to submit the evaluation.
Are there any questions before I begin?
$ dir=hw
$ echo The directory is $dir11
The directory is
$ echo The directory is ${dir}11
The directory is hw11
echo
the $ at the
command line
$ echo $$ 6834 $ ps PID TTY TIME CMD 6834 pts/1 00:00:00 bash 7024 pts/1 00:00:00 ps
$ sleep 60 & [1] 7347 $ echo $! 7347
$ ./bother.sh > /dev/null & [1] 5274 $ jobs [1]+ Running ./bother.sh > /dev/null & $ echo $! 5274 $ kill $! [1]+ Terminated ./bother.sh > /dev/null $ jobs $
$ pwd /home/it244gh/it244/work $ echo $? 0 $ ls asdfasd ls: cannot access asdfasd: No such file or directory $ echo $? 2
exit
built-in
$ cat exit.sh #! /bin/bash # # demonstrates the use of the exit command with a status code exit 2 $ ./exit.sh $ echo $? 2
$ cat arg_count.sh #!/bin/bash # # Prints the number of arguments sent to this script echo This script received $# arguments $ ./arg_count.sh foo bar bletch This script received 3 arguments
$ cat command_name.sh #!/bin/bash # # prints the pathname by which called this script echo This script was called using the pathname $0 $ ./command_name.sh This script was called using the pathname ./command_name.sh $ /home/ghoffman/examples_it244/command_name.sh This script was called using the pathname /home/ghoffman/examples_it244/command_name.sh
basename
command ...echo Usage: $(basename $0) ...
$ cat print_positionals.sh #!/bin/bash # # Prints the value of the first four positional arguments echo 0: $0 echo 1: $1 echo 2: $2 echo 3: $3 $ ./print_positionals.sh foo bar bletch 0: ./print_positionals.sh 1: foo 2: bar 3: bletch
$ echo $foo $ echo --${foo}--- -----
read
to get input from a user ...
${VARIABLE_NAME:-DEFAULT_VALUE}
$ unset filename $ echo $filename $ ls -l ${filename}.txt ls: cannot access .txt: No such filename or directory $ ls -l ${filename:-foo}.txt -rw-rw-r-- 1 ghoffman crontab 0 2012-04-25 13:27 foo.txt
$ echo $filename $
${VARIABLE_NAME:=DEFAULT_VALUE}
$ unset filename
$ echo $filename
$ ls -l ${filename:=foo}.txt
-rw-rw-r-- 1 ghoffman grad 0 Nov 18 16:45 foo.txt
$ echo $filename
foo
${VARIABLE_NAME:?ERROR_MESSAGE}
$ cat var_error.sh
#! /bin/bash
# demonstrates the use of :?
unset arg # make sure there is no value in the variable arg
read -p "Value: " arg
echo About to echo the value of arg using :?
echo 'The value of arg is' ${arg:?'arg not set. exiting script'}
# otherwise
echo Exiting normally
$ ./var_error.sh
Value: foo
About to echo the value of arg using :?
The value of arg is foo
Exiting normally
$ ./var_error.sh
Value:
About to echo the value of arg using :?
./var_error.sh: line 9: arg: arg not set. exiting script
test
command
test
[[ EXPRESSION ]]
Operator | Condition Tested |
---|---|
< | The first string comes alphabetically before the second string |
> | The first string comes alphabetically after the second string |
$ [[ "aa" < "ab" ]]; echo $? 0 $ [[ "ab" < "ab" ]]; echo $? 1 $ [[ "ab" < "abc" ]]; echo $? 0 $ [[ "ab" > "aa" ]]; echo $? 0 $ [[ "abb" > "ab" ]]; echo $? 0
Logical Operator | [ ] | [ [ ] ] | Example |
---|---|---|---|
AND | -a | && | [[ $value -gt 1 && $value -lt 4 ]] |
OR | -o | || | [[ $value -lt 2 || $value -gt 4 ]] |
$ numb=9 $ [[ ($numb -gt 1) && ($numb -lt 10) ]]; echo $? 0 $ numb=11 $ [[ ($numb -gt 1) && ($numb -lt 10) ]]; echo $? 1
$ [[ artist = a* ]] ; echo $? 0 $ [[ aa = a? ]] ; echo $? 0 $ [[ aab = a?? ]] ; echo $? 0
$ [[ artist = a* ]] ; echo $? 0 $ [[ a* = artist ]] ; echo $? 1
p | q | p AND q |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
$ cd dir1 && rm *Unix will not run
rm
if cd fails
p | q | p OR q |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
grep ls $script || echo ERROR: Could not find script
grep
cannot find what it is looking forgawk
awk
at the command line
$ which awk
/usr/bin/awk
$ ls -l /usr/bin/awk
lrwxrwxrwx 1 root root 21 Jul 24 12:12 /usr/bin/awk -> /etc/alternatives/awk
$ ls -l /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Jul 24 12:26 /etc/alternatives/awk -> /usr/bin/gawk
awk
awk [OPTIONS] [PROGRAM] [FILE_LIST]
awk [OPTIONS] -f PROGRAM_FILE [FILE_LIST]
awk
will read the program from the file specified#! /usr/bin/awk
PATTERN '{ ACTION }'
awk
understands this line to mean the following$ cat cars.txt plym fury 1970 73 2500 chevy malibu 1999 60 3000 ford mustang 1965 45 10000 volvo s80 1998 102 9850 ford thundbd 2003 15 10500 chevy malibu 2000 50 3500 bmw 325i 1985 115 450 honda accord 2001 30 6000 ford taurus 2004 10 17000 toyota rav4 2002 180 750 chevy impala 1985 85 1550 ford explor 2003 25 9500
$ awk '{print}' cars.txt plymt fury 1970 73 2500 chevy malibu 1999 60 3000 ford mustang 1965 45 10000 volvo s80 1998 102 9850 ford thundbd 2003 15 10500 chevy malibu 2000 50 3500 bmw 325i 1985 115 450 honda accord 2001 30 6000 ford taurus 2004 10 17000 toyota rav4 2002 180 750 chevy impala 1985 85 1550 ford explor 2003 25 9500
$ awk '/ord/ {print}' cars.txt ford mustang 1965 45 10000 ford thundbd 2003 15 10500 honda accord 2001 30 6000 ford taurus 2004 10 17000 ford explor 2003 25 9500
$ awk '/^f/ {print}' cars.txt ford mustang 1965 45 10000 ford thundbd 2003 15 10500 ford taurus 2004 10 17000 ford explor 2003 25 9500
$ cat begin_end.awk BEGIN {print "About to start processing lines"} /ord/ {print} END {print "No more lines to process"} $ awk -f begin_end.awk cars.txt About to start processing lines ford mustang 1965 45 10000 ford thundbd 2003 15 10500 honda accord 2001 30 6000 ford taurus 2004 10 17000 ford explor 2003 25 9500 No more lines to process
$ awk 'length > 25 {print}' cars.txt ford mustang 1965 45 10000 ford thundbd 2003 15 10500
s$ awk '/bmw/, /ford/ {print}' cars.txt bmw 325i 1985 115 450 honda accord 2001 30 6000 ford taurus 2004 10 17000
$ awk '/bmw/, /xxx/ {print}' cars.txt bmw 325i 1985 115 450 honda accord 2001 30 6000 ford taurus 2004 10 17000 toyota rav4 2002 180 750 chevy impala 1985 85 1550 ford explor 2003 25 9500
$ awk 'length == 23, length == 24 { print }' cars.txt volvo s80 1998 102 9850 ford thundbd 2003 15 10500 chevy malibu 2000 50 3500 bmw 325i 1985 115 450 honda accord 2001 30 6000 ford taurus 2004 10 17000 toyota rav4 2002 180 750
$ awk '{print $2 }' cars.txt fury malibu mustang s80 thundbd malibu 325i accord taurus rav4 impala explor
df
is a little hard to read
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 12G 3.5G 7.7G 32% / none 4.0K 0 4.0K 0% /sys/fs/cgroup udev 2.0G 4.0K 2.0G 1% /dev tmpfs 396M 536K 395M 1% /run none 5.0M 0 5.0M 0% /run/lock none 2.0G 0 2.0G 0% /run/shm none 100M 0 100M 0% /run/user blade82:/disk/sd0h/home/ghoffman 7.9G 4.5G 3.4G 58% /home/ghoffman mx1:/disk/sd1e/spool/mail 4.0G 2.9G 1.1G 73% /spool/mail
$ cat check_space.sh #! /bin/bash # # returns disc usages statistics date=$(date +%Y-%m-%d) space_avail=$(df -h | grep /dev/sda1 | awk '{print $3}') percent=$(df -h | grep /dev/sda1 | awk '{print $4}') echo "$date $space_avail $percent " $ ./check_space.sh 2014-12-08 7.7G 32%