DOC PREVIEW
UW CSE 303 - I/O Redirection, Shell Scripts Emacs

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2005Lecture 3— I/O Redirection, Shell Scripts EmacsDan Grossman CSE303 Spring 2005, Lecture 3 1'&$%Where are We• A simple view of the system: files, users, processes, shell• Lots of small useful programs; more to come• An ever-more- complicated s hell definition:– Filename expansion– History expansion– Command-line editing– I/O redirection– Programming constructs– VariablesDan Grossman CSE303 Spring 2005, Lecture 3 2'&$%Simple view of input/output• Old news: Programs take an array of strings as arguments• Also: Programs return an integer (convention: 0 for “success”)The shell also sets up 3 “streams” of data for the program to access:• stdin: an input stream• stdout: an output stream• stderr: another output streamThe default shell behavior uses the ke yboard for stdin and the shellwindow for stdout and stderr.Examples:ls prints files stdout and “No match” to stderr.mail takes m ess age body from stdin (waiting for C-d to end the file).Dan Grossman CSE303 Spring 2005, Lecture 3 3'&$%File RedirectionUsing arcane characters, we can tell the shell to use files instead of thekeyboard/screen:• redirect input: cmd < file• redirect output, overwriting file: cmd > file• redirect output, appending to file: cmd >> file• redirect output and e rror output to file: cmd >& file• ...Examples:• How I put the histories on the web page.• ls uses stdout and stderr.• Mailing a file’s contents.Dan Grossman CSE303 Spring 2005, Lecture 3 4'&$%Pipescmd1 | cmd2Change t he stdout of cm d1 and the stdin of cmd2 to be the same,new stream!Very powerful idea:• In the shell, larger command out of smaller com mands• To the user, combine small programs to get more usefulness– Each program can do one thing and do it well!Examples:• foo --help | less• djpeg me.jpg | pnmscale -xysize 100 150 | cjpeg >me thumb.jpg• your homework... (with grep, commonly used in pipes)Dan Grossman CSE303 Spring 2005, Lecture 3 5'&$%cat and redirectionJust to show there is some math underlying all this nonsense, here aresome fun and useless equivalences (like 1 · y = y):• cat y = cat < y• x < y = cat y | x• x | cat = xDan Grossman CSE303 Spring 2005, Lecture 3 6'&$%Combining CommandsCombining s impler com mands to form m ore c omplicated ones is veryprogramming-like. In addition to pipes, we have:• cmd1 ; cmd2 (sequence)• cmd1 || cmd2 (or, using int result – the “exit status”)• cmd2 && cmd2 (and, like or)• cmd1 ‘cmd2‘ (use output of cmd2 as input to cmd1 ).– Useless example: cd ‘pwd‘.– Non-useless example: mkdir ‘whoami‘.Note: Previous line’s exit status is in $?.Dan Grossman CSE303 Spring 2005, Lecture 3 7'&$%Non-alphabet soupList of characters with special (before program/built-in runs) meaningis growing: ‘ ! % & * ~ ? [ ] " ’ \ > < | $ (and we’re notdone).If you ever want these characters or (space) in som ething like anargument, you nee d some form of escaping ; each of " ’ \ haveslightly different meaning.Dan Grossman CSE303 Spring 2005, Lecture 3 8'&$%Toward Scripts...A running shell has a state, i.e., a current• working directory• user• collection of aliases• history• ...In fact, ne xt time we w ill learn how to extend this state with new shellvariables.We learned that source can execute a file’s contents, which can affectthe shell’s state.Dan Grossman CSE303 Spring 2005, Lecture 3 9'&$%Running a scriptWhat if we want to run a bunch of com mands without changing ourshell’s state?Answer: start a new shell (sharing our stdin, stdout, stderr), run thecommands in it, and exit.Better answer: Automate this process.• A shell script as a program (user doesn’t even know it’s a script).• Now we’ll want the shell to end up being a programming language• But it will be a bad one except for simple thingsDan Grossman CSE303 Spring 2005, Lecture 3 10'&$%Writing a script• Make the first line exactly: #!/bin/csh• Give yourself “exec ute” permission on the file• Run itNote: The shell consults the first line:• If a shell-program is there, launch it and run the script• Else if it’s a “real executable” run it (more later).Example: listhomeDan Grossman CSE303 Spring 2005, Lecture 3 11'&$%Accessing argumentsThe script accesses the arguments with $i to get the ithone.Example: make thumbnail1We would like optional arguments and/or usage mes sages. Need:• way to find out the number of arguments• a conditional• some stuff we already haveExample: make thumbnail2Dan Grossman CSE303 Spring 2005, Lecture 3 12'&$%More expressionstcsh expres sions c an be math (+, *, -, ...), logic (&& , ||, ! ), or filetests.Example: dcdls (double cd and ls) can check that arguments aredirectories.Exercise: Do make thumbnail3.Exercise: script that replaces older file with newer oneExercise: make up your ownDan Grossman CSE303 Spring 2005, Lecture 3 13'&$%Review• The shell runs programs and builtins, interpreting specialcharacters for filenames , history, I/O redirection.• Some builtins like if support rudimentary programming.• A script is a program to its use r, but is written using shellcommands.So the shell language is okay for interaction and “quick-and-dirty”programs, making it a strange beast.For both, shell variables are extremely useful.Note: enough already for your homework except for grep, but askquestions!Dan Grossman CSE303 Spring 2005, Lecture 3 14'&$%Variablessetset i = 17setecho $iset | grep iset iecho $iunset iecho $iDan Grossman CSE303 Spring 2005, Lecture 3


View Full Document

UW CSE 303 - I/O Redirection, Shell Scripts Emacs

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download I/O Redirection, Shell Scripts Emacs
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 I/O Redirection, Shell Scripts Emacs 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 I/O Redirection, Shell Scripts Emacs 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?