CS 279 - Week 8 Lecture 2 - 10-11-12
* aside: how you can get the exit status from
a command
* the variable $? reads the exit status of
the last command executed
* after a function returns, $? gives the exit
status of the last command executed in the
function
* following execution of a pipe, it gives
exit status of the last command executed
* after a script terminates, a $? from the
command-line gives the exit status of
the script, which is the exit status of
the last command executed in that script
(oughta be 0 for success!)
* bash shell programming feature of the day:
interactive input
* one easy way to get input from a user
while a script is running is with
the read command
* simplest form:
read desiredVariable
...and the script pauses until the
user enters something,
and what they entered (that string)
becomes the value of desiredVariable
* simple example in simple-read.sh
and also several other examples of read in action
in either.sh, loop-read.sh, and y-or-n.sh
some more FILES tidbits (and operations on files)
* first: a FEW words on the special files' category
of device files...
* a device file is associated with each device
and provides a useful ABSTRACTION for users ---
users "write" to a device by writing to its
device file,
users "read" from a device by reading from its
device file,
(and device driver software actually handles the
really-more-complex actions of this reading and
writing)
* conventionally, device files live in the /dev directory
* /dev/tty is a special device file typically associated
with your terminal;
... there is also a specific designation, which you can find
with the tty command;
* /dev/null
the null device
a "bit bucket"
...anything sent to it, is THROWN AWAY
...when you try to read from it, you get an end-of-file
* some interesting file commands...
which
-----
* followed by one or more commands,
outputs the full pathname for that command that
it finds by traversing your PATH
cmp and diff
------------
cmp - does a quick comparison of 2 files
if the same? exit status of 0
if different? exit status of 1 (and you
might get a message)
if one or both can't be accessed? you get
exit status of 2
* can use its -s option to run "silently"
(no outout to standard out, JUST the
exit status)
diff - analyzes the differences between two files,
and gives you a list of transformations for
transforming file1 to file2
n1,n2 d n3 delete lines n1 through n2 of file1
n1 a n3,n4 append lines n3 through n4 of file2
after line n1 of file1
n1,n2 c n3,n4 replace (change) lines n1 through n2
of file1 with n3 through n4 of file2
also shows ALL lines involved in these
transformations,
with < indicating lines deleted from file1
with > indicating lines taken from the original file2
* you can diff directories, too! (recursively with
a -r option)