Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs288 Intensive Programming in Linux

Similar presentations


Presentation on theme: "Cs288 Intensive Programming in Linux"— Presentation transcript:

1 Cs288 Intensive Programming in Linux
Instructor: C F Yurkoski Section web site: 6-9-15

2 6-9-15

3 1st Homework results average grade 70% indent your code properly!
Don’t submit .txt file Don’t submit .tgz Test on afs Questions after class ………………………………………………….. 6-9-15

4 Webex test 6-9-15

5 Project revu project using cut, grep, wget, loops etc.
In  are 4 files: names contains a list of names, one per line. id contains a list of semicolon separated tuples, one per line consisting of a name and the literal ID followed by a numeric id. hours  contains list of semicolon separated tuples, one per line consisting of a numeric id and a number indicating the number hours worked that week. rate  contains list of semicolon separated tuples, one per line consisting of a numeric id and an hourly rate for that id.  Assume the rate is USD per hour. Your goal is to write a script which calculates the compensation for the names in first file for the week and output the names followed by a tab followed by a dollar sign followed by that name’s pay. 6-9-15

6 or I may call it like this:
Output one name and pay amount per line.  The output should be in alphabetical order by name regardless of the order in the “names” file. Write the results to standard out, output any error messages to standard error. Your script should accept 5 command line arguments.   The first will be the name of a directory like the one above, the next 4 are assumed be file names in that directory whose contents are formatted as the four URLs above and are in the same order as above.   Base on the contents of the files calculate the pay for name in the names file pay by using it to retrieve the id from the id file and using that in turn to obtain the pay rate and hours worked.   E.g. I may call your script  (where Yourscript is the name of the file you submit to moodle) like this: Yourscript   ids hours rate 2> errors or I may call it like this: Yourscript   IDs hours Rate 2> errors 6-9-15

7 Your output should be formatted like this: Al Aho $4567
So you can be guaranteed of the order of the arguments and of the format of the files but not their actual names or actual contents. Your output should be formatted like this: Al Aho     $4567 Brian Kernighan $0 Peter Weinberger $1234 Be sure to write your error messages to standard error because your standard output will automatically be compared to the expected output.  You should check for (at least) the follow error conditions: The wrong number of arguments. There is a name in the names file for which there is no id in the id file. If you encounter the first condition you should exit with a false exit value, for the other you should log a message to stderr and continue. If you find no hours for a name in the hours file, assume she worked 0 hours that week.   If you find no rate for a worker assume she is working pro bono. If there are no errors, you should exit with a true exit code. Do your own work!   Otherwise it won’t be counted. 6-9-15

8 Two part quiz You’ll find thes 2 URLs:
and hourlyrate contains 1 line, a number in USD. employees contains several lines with tab separated fields of the format: status<TAB>name <TAB>social security number<TAB> hours 6-9-15

9 Write a small bash script which accepts as arguments those two URLs and which calculates the pay for each hourly employee and print their names and the amount of their pay assuming they all work 30 hours per week. The names and the amount should be tab separated. E.g: >: ./cfyscript Jane Deardon $ Jill Deardon $ J Deardon $ Jill Jackson $ >: Complete in 15 minutes. 6-9-15

10 quiz revu Given this input file: afsconnect1-59 quizes >: cat input
afsconnect1-59 quizes >: cat input ab,cd,efghijk,lmn,opqurstuvwxyz ab,cd,efghijk,lmn, efghijk,efghijk,opqurstuvwxyz,, efghijk,lmn,opqurstuvwxyz,, efghijk,,lmn,z, a,b,c,n, afsconnect1-60 quizes >: what is the output of this command line: afsconnect1-61 quizes >: cat input | grep "^e.*n" | cut -f4 -d"," | while read x; do echo ${x} $x x "x"; done 6-9-15

11 >: cat input | grep "^e
>: cat input | grep "^e.*n" | cut -f4 -d"," | while read x ; do echo ${x} $x x "x"; done x x z z x x 6-9-15

12 ab,cd,efghijk,lmn,opqurstuvwxyz ab,cd,efghijk,lmn,
cat input | grep "^e.*n" | cut -f4 -d"," | while read x ; do echo ${x} $x x "x"; done ab,cd,efghijk,lmn,opqurstuvwxyz ab,cd,efghijk,lmn, efghijk,efghijk,opqurstuvwxyz,, efghijk,lmn,opqurstuvwxyz,, efghijk,,lmn,z, a,b,c,n, 6-9-15

13 efghijk,lmn,opqurstuvwxyz,, efghijk,,lmn,z,
cat input | grep "^e.*n" | cut -f4 -d"," | while read x ; do echo ${x} $x x "x"; done efghijk,lmn,opqurstuvwxyz,, efghijk,,lmn,z, 6-9-15

14 cat input | grep "^e.*n" | cut -f4 -d"," | while read x ; do echo ${x} $x x "x"; done
6-9-15

15 Quiz 1, Part 2 rate=`wget -q -O - $1 ` let pay=$rate*30 wget -q -O - $2 | grep "^hourly" | grep -v "hourly status" | cut -f 2 |while read name do echo -e ${name} \\t \$${pay}.00 done 6-9-15

16 Ascii table http://www.ascii-code.com/
6-9-15

17 other Linux tools and built ins
shift sort uniq awk Aho, Weinstein & Kernighan. sed stream editor. 6-9-15

18 Basic awk 6-9-15

19 Usage: sort [OPTION]... [FILE]...
-b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters -g, --general-numeric-sort compare according to general numerical value -i, --ignore-nonprinting consider only printable characters -M, --month-sort compare (unknown) < `JAN' < ... < `DEC' -h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G) -n, --numeric-sort compare according to string numerical value -R, --random-sort sort by random hash of keys --random-source=FILE get random bytes from FILE -r, --reverse reverse the result of comparisons 6-9-15

20 Other reminders to redirect stdout to stderr: 1>&2
to redirect stderr to stdout: 2>&1 to test for null strings: –z to test for non-null strings: -n 6-9-15

21 sed 6-9-15

22 sed examples sed -e "/pool-17/d" hosts
sed -e "s/pool-1[1-9]/poolA/" hosts sed -f script hosts cat script s/pool-1[1-9]/poolA/ s/pool-[1-9]/poolB/ 6-9-15

23 environment . “pathname” source “pathname” e.g.: . ./.env
source /home/.env cfy

24 More useful bash commands
tail touch find man source cfy

25 in class assignment Write a script that takes 3 arguments; The first arg is a pathname of a file that contains a list of script names, the 2nd is a pathname of file that is to be passed as input to each of those scripts, and the 3rd is the expected output when those scripts when passed that input as it first arg. Your script should assume that each line in arg1 is a script in its PATH, it should execute each script passed to it with its arg1 your scripts arg2. Your script should output the name of each script it runs followed by the output that script produces when run with the input specified. 6-9-15

26 e.g. >: yourscript students input1 output student output cifer,l input1 emlin,g 42 lem,s 2 assuming: afsconnect1-43 quizes >: cat input1 21 afsconnect1-44 quizes >: 6-9-15

27 afsconnect1-45 quizes >: cat students cifer,l emlin,g lem,s
and afsconnect1-47 quizes >: cat students | while read x > do > echo $x > cat $x > echo > done echo $1 y=`cat $1` let x=y*2 echo $x echo 2 6-9-15

28 cat $1 | while read testscript do echo -e -n "$testscript\t"
>: cat yourscript echo student output echo cat $1 | while read testscript do echo -e -n "$testscript\t" bash $testscript $2 done >: 6-9-15

29 A better solution Compare the output of each script to the contents of the third file. Output the name of the script followed by a tab and a indication as to whether the results are correct (the same as the contents of the output file) or not. 6-9-15

30 assuming: afsconnect1-50 quizes >: cat answertoeverything 42 afsconnect1-51 quizes >:
6-9-15

31 extra credit example >: bash extracredit students input1 ./answertoeverything student grade cifer,l is wrong emlin,g is correct lem,s is wrong 6-9-15

32 echo student grade echo cat $1 | while read testscript do rm /tmp/xyzzy echo -e -n "$testscript\t" bash $testscript $2 > /tmp/xyzzy diff /tmp/xyzzy $3 >/dev/null if [ "$?" -eq 0 ] then echo is correct else echo is wrong fi done 6-9-15

33 cat $1 | while read testscript do echo -e -n "$testscript\t"
echo student output echo cat $1 | while read testscript do echo -e -n "$testscript\t" bash $testscript $2 done 6-9-15

34 cat $1 | while read testscript do rm /tmp/xyzzy
echo -e -n "$testscript\t" bash $testscript $2 > /tmp/xyzzy diff /tmp/xyzzy $3 >/dev/null if [ "$?" -eq 0 ] then echo is correct else echo is wrong fi done 6-9-15

35 homework Write a script that accepts as it two command line argument two urls, for example, but not necessarily: and These files contain 3 comma separated fields: customer,sales,product where sales in a number and customer and product are strings. Find any customers that appear in both files that have total sales of more than and print the customer name and products they purchased. 6-9-15

36 https://web.njit.edu/~yurkowsk/29sept/competitorA
customerA, ,router customerC, ,router customerE,2345,switch customerF,234567,server customerG,123456,router 6-9-15


Download ppt "Cs288 Intensive Programming in Linux"

Similar presentations


Ads by Google