CS 279 - Week 4 Lecture 2 - 2022-09-14
TODAY WE WILL
* announcements
* a few odds-and-ends
* intro to bash's two kinds of for loops
* more on the bash if statement
(really, some more of its possible conditions!)
* prep for next class
Next Reading
* Section III, Chapter 24 - has good info on bash
control structures
====
* Course text, Section 12.1 - white space removal
* in handling/interpreting/parsing commands,
the bash shell uses blanks/tabs to separate
arguments
* ...it happily ignores/throws out any excess
blanks or tabls
====
* NOTE:
you CAN execute a shell without making it executable
if you use the sh command:
> sh my-script-name
====
difference between $@ and $*?
====
* both do give a way to grab all the command line
arguments from a call --
they are subtly different,
but The Linux Document Project's "Bash Guide for Beginners"
Section 3.2.5, notes $* can have security issues,
so we'll stick with $@
====
bash's for loops
====
* one iterates over a list -- let's call that
a "list-style" for loop
* one is closer to the "classic" C/C++ for loop --
let's call that a "classic" for loop
* "list-style" for is discussed in course text Section 24.4:
for desired_var_name in list_expr
do
command1
command2
...
done
* YES, it turns out the do MUST be on a separate
line than the for part!
* loop iterates for each value in list_expr,
setting desired_var_name to each list value
in turn
* "classic" style - not in course text, but:
for ((init_part; bool_part; update_part))
do
command1
command2
...
done
* YES, it turns out the do MUST be on a separate
line than the for part!
* semantics are probably what you think --
do update_part when you first reach the loop,
if bool_part evaluates to a truthlike value,
enter the loop;
at the end of the loop, do the update part,
and if bool_part evaluates to a truthlike value,
enter the loop again
...
=====
demoing some list-style for loops
=====
* how can I get a list for this for loop?
* the output of a command written in backquotes
* $@, containing the command-line arguments,
is considered a list
* I'm pretty sure there are more...
=====
continuing our intro to if
=====
* basic syntax: is NOT that from the C/C++/Java/C#/JavaScript
family!!!
if <test-commands>
then
commands-if-test-command-true
fi
if <test-commands>
then
commands-if-test-command-true
else
commands-if-test-command-false
fi
if <test-commands1>
then
commands-if-test-commands1-true
elif <test-commands2>
commands-if-test-commands1-false-and-test-commands2-true
elif <test-cmds3>
commands-for-reach-here-and-test-cmd3-true
...
else # this is still optional
commands-if-all-the-prev-test-cmds-were-false
fi