Download presentation
Presentation is loading. Please wait.
Published byBruce Green Modified over 6 years ago
1
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Noon section has exam on 11 AM May 3 1:30 section has exam on 11 AM May 5 Exam review held on April 26
2
Variables in bash (review)
VARIABLE=STRING, with no spaces around the =, assigns the STRING into VARIABLE. In order to make it accessible to children processes, you must export the variable from the shell process that defined it. Use unset to remove a variable binding. C library functions getenv & putenv manipulate these. -bash-3.00$ v=1 -bash-3.00$ echo $v 1 -bash-3.00$ bash -c 'echo $v' -bash-3.00$ export v
3
Shell read and built-in variables.
Use read VARIABLENAME to read the terminal into VARIABLENAME. Use echo STRING or echo –e STRING to echo to stdout; -e interprets escapes such as \n \t. Special variables: $? is exitstatus of exit exitstatus; $0 is argv[0] (program name), $1 is argv[1] (first argument), etc.; shift shifts $2 down to $1, etc.; $# is number of positional arguments; $* or give all command line arguments; set - - STRINGS sets command line arguments to whatever is in STRINGS.
4
Boolean expressions Any successful (0) exit status is true, non-0 is false. Shell’s test primitive tests Shell variables and files. Exit status is 0 for success. [ expression ] is shorthand for test expression. -bash-3.00$ ls *.out nohup.out -bash-3.00$ test -f nohup.out ; echo $? -bash-3.00$ test -f junk.out ; echo $? 1
5
Shell test or [ ] expressions
Here is the subset to know for the exam: Numerical comparisons: -eq, -ne, -gt, -ge, -lt, -le String comparisons: s1 = s2, s1 != s2. Use “$v” or “${v}” if v may be empty; otherwise, a binary comparison operator won’t see empty string. File tests: -f, -r, -w, -x, -d. Know how to add to a variable using expr or (( )). There are no regular expressions in the exam.
6
Boolean connectives && is logical AND, || is logical OR
Both are short-circuited. && quits on the first failure, || on the first success. -bash-3.00$ ls *.out nohup.out -bash-3.00$ [ -f nohup.out ] && echo hello hello -bash-3.00$ [ -f junk.out ] && echo hello -bash-3.00$ [ -f nohup.out ] || echo hello -bash-3.00$ [ -f junk.out ] || echo hello
7
if control construct selects an execution path
Just like you are used to in C++, but Use elif instead of else if, use fi to end an if construct. -bash-3.00$ count=`ls *out |wc -l` -bash-3.00$ echo $count 1 -bash-3.00$ if [ $count -eq 0 ] > then > echo none > elif [ $count -eq 1 ] > echo one > else > echo other > fi one
8
while iterates while a text is true
while uses do and done keywords, also break and continue -bash-3.00$ set -- `date` # set command line arguments -bash-3.00$ echo "COUNT $# DATA: COUNT 6 DATA: Sun Mar 27 19:45:36 EDT 2011 -bash-3.00$ i=1 -bash-3.00$ while [ $# -gt 0 ] > do > echo "var $i: $1" > i=`expr $i + 1` > shift # shift position parameters to the left by one position, discard former $1, decrement $# > done var 1: Sun var 2: Mar var 3: 27 var 4: 19:49:10 var 5: EDT var 6: 2011
9
for loops over a list of values
-bash-3.00$ ls -d s* splitstudents startup -bash-3.00$ for f in `ls -d s*` > do > echo "file: $f" > done file: splitstudents file: startup -bash-3.00$ find ~/unix -name "s*" -print /export/home/faculty/parson/unix/seeargs /export/home/faculty/parson/unix/seeargs/seeargs.c /export/home/faculty/parson/unix/seeargs.zip -bash-3.00$ for path in `find ~/unix -name "s*" -print` > echo "file `basename $path` in directory `dirname $path`" file seeargs in directory /export/home/faculty/parson/unix file seeargs.c in directory /export/home/faculty/parson/unix/seeargs file seeargs.zip in directory /export/home/faculty/parson/unix
10
Know how to write a shell function
arg1=$1 # $1 is the first argument to a function when inside that function. arg2=$2 echo f: $arg1 $arg2 } set -- Y Z echo $1 $2 Y Z f a b f: a b f 1 2 f: 1 2 echo $1 $2 # the “global $1 $2” are outside the scope of the function’s $1 $2 etc.
11
Know how to write a Python function with a simple “if” “while” or “for” control construct.
def f(a,b): if a <= b: for i in range(a,b+1): print "Range:", i else: while a >= b: # a > b the first time through print "Downrange:", a a -= 1 # See example run on next slide.
12
Know how to write a Python function with a simple “if” “while” or “for” control construct.
>>> f(0,0) Range: 0 >>> f(0,2) Range: 1 Range: 2 >>> f(2,0) Downrange: 2 Downrange: 1 Downrange: 0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.