Unformatted text preview:

CSCI 3294 February 1, 2005Slide 1Administrivia• Homework 2 on Web; due Monday.• Notice that answers to non-opinion minute essay questions (e.g., firstquestion from Monday) are in final version of notes online.• Reading assignments meant to be skimmed — read carefully only parts thatwe talked about in class, or that interest you.Slide 2More Filters• sed — “stream editor” — non-interactive program, by default does not edit inplace, but works as a filter, transforming input to produce output.• Some simple uses:– Search and replace:sed ’s/old/new/g’ infile >outfile– Delete lines containing some string:sed ’/this/d’ infile >outfile(How else could you do this?)• Especially useful with regular expressions (later).CSCI 3294 February 1, 2005Slide 3More Filters, Continued• awk — implementation of programming language AWK — “pattern scanningand processing language”.• Lines of AWK program specify pattern and action. (Can also include functiondefinitions.)• Basic processing — split each line of input (“record”) into “fields”, compare topatterns in program, execute actions for any patterns that match.• Simple uses:– Print selected fields from input (as in examples from last time).– Print selected lines of input:awk ’/this/’ infile(How else could you do this?)Slide 4More Useful Commands• find. Very powerful/flexible, though if you don’t use it often you probably willhave to read the man page to remember syntax.• Simple examples:– Find all files in the current directory modified in the last week.find . -mtime -7– Find all files in your home directory whose name contains hello.find $HOME -name "*hello*"– Find all files in the current directory that end in .bak and apply rm -ito them.find . -name "*.bak" -exec rm -i {} \;CSCI 3294 February 1, 2005Slide 5More Useful Commands, Continued• diff — compare files or directories. (A good use — “regression testing” ofprograms.)• pushd, popd (actually shell built-ins) — manipulate shell’s stack ofdirectories.Slide 6More Useful Commands, Continued• xargs — “build and execute command lines from standard input”.– Find all processes for program java and kill them:ps aux | grep java | awk ’{print $2}’ | xargskillCSCI 3294 February 1, 2005Slide 7Shell Input as a Programming Language• What bash understands is in a sense a programming language, with theshell as its interpreter:– Variables (untyped).– Expressions (arithmetic and logical).– Conditionals (if/then/else) and loops.– Functions.• Can be used interactively, or collected into “scripts”.• I will talk about bash, but most shells provide similar functionality, justsometimes with different syntax. If you want to write scripts portable to mostUnix systems, probably best to stick to sh subset of bash.Slide 8Shell Scripts• A “shell script” is just a sequence of things you could type at the shell prompt,collected in a (text) file.• Normally, first line of script is #! followed by path for shell (/bin/bash,e.g.), and the file is marked “executable” (with chmod). But you can alsoexecute commands in file anyfile via bash anyfile.• With the exception of the first line, lines starting with # are comments.CSCI 3294 February 1, 2005Slide 9Minute Essay• Write a command to find all the files in the current directory (andsubdirectories) that are less than a week old and list them in reverse order bymodification time (i.e., newest to oldest).Slide 10Minute Essay Answer• The solution I had in mind wasfind . -mtime -7 | xargs ls -ltbut there are undoubtedly other


View Full Document

TRINITY CSCI 3294 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?