An Introduction to the Scenic (or C/*nix) Programming Seminar Admin * Stuff can be found at http://www.cs.grinnell.edu/~rebelsky/Courses/Cnix/2012S/ * EBoards, Outlines, Examples, Links (?) * Informal * Show up when you want to * Leave when you want to * Expect me to be underprepared (even for me) Overview * Background: Why this seminar? * Some Key C/*nix Principles * Additional SamR Principles * Detour: A C Problem * Some Simple *nix Tasks * Important *nix Tools * C Concepts * Looking Ahead BACKGROUND: WHY AM I DOING THIS? * Things I think you should know * And since you don't, I have a responsibility to help you learn SOME KEY C/*NIX PRINCIPLES * Don't do by hand what you can do by code (assuming by code is faster) aka use your tools to make yourself more efficient * Small programs and small libraries, focused on single or a few tasks * Gain power by combining (usually with pipes or something similar) * Detour question: How do you combine stdout and stderr? (E.g., I run a program, and I want to look at both output and errors through less.) Usually, something like 2>&1 will work This means "Take output port 2 and redirect it to output port 1" (But only in bash) * Detour note: print does not immediately go to stdout (or whever) Instead, it gets buffered You can deal with buffering with the fflush command * Share! What you need to achieve these principles * Knowledge of what tools are available on most *nix systems * Generally what they do - details through man pages and Web * Knowledge of C so that you can throw together a small fast tool * Knoledge of a scripting language so that you can throw things together in fancier ways * Sam uses Perl * Bash does a lot, if you learn it * But Python, Ruby, et al. are nice, too ADDITIONAL PRINCIPLES THAT SAMR EXPECTS YOU TO FOLLOW * Document! * You will forget how to use your code! * Test! * Automated unit testing. * But remember: For a lot, your goal is efficiency * Refactor! * Copy paste change => write something more general * Format! * Using GNU standards for C. DETOUR: A C PROBLEM x = malloc (...); foo (); bar (); free (x); // BOOM! Doesn't crash if free is before bar. bar does not use x. bar does not call free. What would you do to track down this bug? What if you were working on a coctail napkin? SOME SIMPLE *NIX TASKS * Given a DOS-formatted text file (lines end with \r\n rather than just \n), convert it to a standard text file by dropping all of the \r's. 'Twas brillig and the slithy toves did gyre and gimble in the wabe Each line ends with a hidden character to that the computer interprets as "go on to the next line" * Unix: '\n' * DOS: '\r' '\n' * On a text editor, we *might* be able to delete the \r's * Write a C program that does it. (5 minutes) * Google a program that does it * tr is a program designed for manipulating single characters * And od is a program for dumping data in octal, hex, and other useful form. * Given a standard text file, convert all uppercase letters to lowercase. * Write a C program (5 minutes) * Google or man * tr is a program designed for manipulating single characters * Given a standard text file, remove all blank spaces at the end of lines. * Do it in a text editor (1 minute) vi: :1,$s/ *$// * Write a C program (15 minutes) * Download a regular expression library and write a C program (10 minutes) * Use awk? (2 hours to learn enough awk) * Use Perl, because you know enough Perl (5 minutes) while () { s/ *$//; print $_; } * Use sed - stream editor * Given a CSV in which each line has LastName,FirstName,Assignment,NumericGrade find the the five highest grades on homework 2. * Given an HTML file, find the URLs of all images. IMPORTANT *NIX TOOLS IMPORTANT C TOOLS/CONCEPTS LOOKING AHEAD Next week * Continue with the stuff we didn't finish * Do a few more simple *NIX problems * And then look ahead again