CS 279 - Week 14 Lecture 1 - 2022-11-28
TODAY WE WILL
* announcements/reminders
* can you "return" from a Bash function?
* Bash shell case statement
* some "about your system" UNIX/Linux commands
* [if time] some "just fun utilities" UNIX/Linux commands
* prep for next class
* IF you have a chance:
please TRY to log into vlinux.humboldt.edu
and e-mail me letting me know if you could or could not
do so;
=====
CAN you return something from a Bash function?
=====
* answer: kind of...!
* four ways to attempt such a kluge:
1. Change the state of a "global" variable
[I wouldn't call that returning...! more like a
persistent change due to the function...]
2. Use the exit command with a return value to end
the function (and that exit status will be
available then via $?)
* BUT!! if a function calls exit, that
also ends/exits the calling shell script...!
* demo: funct-exit.sh and call-funct-that-exits.sh
3. Use the return command to end the function
(but not the calling shell script or other calling function!)
and its given value becomes the *exit status* for
that function call
* to access this "returned" value, then,
need to use $?
* demo: funct-return.sh and call-funct-that-returns.sh
4. echo desired output to standard output. which can be
accessed by the caller if called, say, with backquotes
for example,
value=`my_funct a b c`
* ironically, "looks" closest to a tradional-ish function
return...?
* demo: see echo3-demo.sh
=====
Bash shell case statement!
=====
* Bash has a case statement!
* basic syntax:
case <expr> in
<patt1>) <command>
<command>
;;
<patt2>) <command>
<command>
;;
...
*) <command>
<command>
;;
esac
* semantics: it compares the <expr> to each pattern in turn,
and as soon as it matches one, it does that pattern's actions,
and then exits the case statement
(patterns are file globbing patterns...)
* NOTE: the *) case is optional -- if NO cases' patterns
match the expression, none of the case branches are taken
********
* AFTER CLASS addition:
********
* (thanks to a class member! 8-) )
* yes, you CAN combine cases --
put | between the patterns;
for example,
Moo | MOO | mOO)
echo "this worked! Handles Moo or MOO or mOO"
;;
* and example demoing this IS now included in case-play.sh
=====
some commands "about the system"
=====
-----
uname
-----
* especially with argument -a
* can give you information about the OS being run,
and with -a, also info about the hardware being run;
-----
uptime
-----
* gives an idea of how long the system has been running
since its last reboot/restart,
how many "users", and the load average of the system
over the last 1, 5, and 15 minutes
* rough definition:
load average is a way to gauge how many processes are,
on average, concurrently demanding CPU attention
(from:
https://superuser.com/questions/23498/what-does-load-average-mean-on-unix-linux )
little less rough, but still a little rough:
The average sum of the number of processes waiting in
the run-queue plus the number currently executing
over 1, 5, and 15 minute time periods
(from:
http://www.lifeaftercoffee.com/2006/03/13/unix-load-averages-explained/
)
-----
who
-----
* what users are currently logged into the system
-----
top
-----
* displays and updates sorted information about processes
top
...for desired_username's processes, use -u option:
top -u desired_username
* q to quit top; -- ^c also works to stop it