=====
CS 279 - Week 4 Lecture 1 - 2022-09-12
=====
=====
TODAY WE WILL
=====
* a few announcements/reminders
* interactive input
* command-line variables
* *maybe* start some bash control structures
* prep for next class
=====
interactive input: read command
=====
* one pretty easy way to obtain input form the user
while the script is running is by using the read
command
read desired_variable
* it reads everything the user types until
a newline/return/enter is typed and
stores it into the given variable
* in trying out the little script
simple-read, it appears that read
ignore leading and trailing blanks!
=====
command-line arguments
=====
* note: bash expects command-line arguments
to be separated by blanks
(so you can include a blank in a command argument
by quoting that argument, or preceding each non-separating
blank with a \)
cp file1 "file 2"
cp file1 'file 2'
cp file1 file\ 2
* within a bash shell,
$1 will be the first command line argument,
$2 will be the second command line argument,
...
$0 will be running script's name...!
(may be expressed close to how you typed it in the command)
* $# will be set to the number of command line arguments
* it LOOKS like $* and $@ both contain the whole
set of command line arguments [I need to look
up IF there is a difference...]
* based on the comment in Section 3.2.5 in
https://www.gnu.org/software/bash/manual/bash.html
$@ is the SAFER choice, and we'll be sticking with
that.
=====
a few more goodies...
=====
* expr command evaluates an expression and sends the
result to standard output
> expr 3 + 5
8
* basename - grabs the base/basename from a pathname
(the "last" part, the loca filename)
dirname - grabs everything but the base/basename
(the directory of the basename, if you will)
====
ASIDE - exiting a shell script early
====
* you can use the exit command to exit a shell script
early
...give it an argument with the return code
...remember that 0 is considered success in bash...!
exit 1
=====
STARTING 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
* test commands...?
the possibly-list of test commands are executed,
if its return status is zero (success)
then the if's commands are executed
* WE WILL TALK MORE ABOUT THESE, but to get us
started:
SOME conditionals work inside of [ ],
which is considered the same as giving them as
arguments to the test command
[ ] are considered a command and so need to be
surrounded by at least a blank
for example:
-e <filename>
...will be treated as success/true if <filename>
exists.
= can be used to compare if two STRINGS have the
same contents
-eq can be used to compare if two NUMBERS have the
same value
* ! is prefix not
|| is Boolean or
&& is Boolean and
* for example (these are demo'd in bash script if-play)
if [ $# -eq 0 ]
then
echo -n "Enter a file name: "
read input
else
input=$1
fi
echo "\$input: [$input]"
if [ ! -e $input ]
then
echo "$0: $input does not exist"
echo "...exiting"
exit 1
fi
echo "$input's contents: "
ls -l $input