DOC PREVIEW
UW CSE 303 - Lecture Notes

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 DevelopmentHal PerkinsSpring 2008Lecture 3— I/O Redirection, Shell ScriptsCSE303 Spring 2008, 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 shell definition:– Filename expansion– Command-line editing– History expansion– I/O redirection– Programming constructs– VariablesCSE303 Spring 2008, 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 a.k.a. 0: an input stream• stdout a.k.a. 1: an output stream• stderr a.k.a. 2: another output streamThe default shell behavior uses the keyboard for stdin and the shellwindow for stdout and stderr.Examples:ls prints files stdout and “No match” to stderr.mail takes message body from stdin (waiting for C-d (“end of file”)to stop taking input).CSE303 Spring 2008, Lecture 3 3'&$%File RedirectionUsing arcane characters, we can tell the shell to use files instead of thekeyboard/screen (Bash Manual, Section 3.6):• redirect input: cmd < file• redirect output, overwriting file: cmd > file• redirect output, appending to file: cmd >> file• redirect error output: cmd 2> file• redirect output and error output to file: cmd &> file• ...Examples:• How I get the histories for the web page.• ls uses stdout and stderr.• Using cat to copy information to/from files.CSE303 Spring 2008, Lecture 3 4'&$%Pipescmd1 | cmd2Change the stdout of cmd1 and the stdin of cmd2 to be the same,new stream!Very powerful idea:• In the shell, larger command out of smaller commands• 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.jpgCSE303 Spring 2008, 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 = xCSE303 Spring 2008, Lecture 3 6'&$%Combining CommandsCombining simpler commands to form more complicated ones is veryprogramming-like. In addition to pipes, we have:• cmd1 ; cmd2 (sequence)• cmd1 || cmd2 (or, using int result – the “exit status”)• cmd1 && cmd2 (and, like or)• cmd1 ‘cmd2‘ (use output of cmd2 as input to cmd1). (Veryuseful for your homework. Note cmd2 surrounded by backquotes,not regular quotes)– Useless example: cd ‘pwd‘.– Non-useless example: mkdir ‘whoami‘A‘whoami‘.Note: Previous line’s exit status is in $?.CSE303 Spring 2008, 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 something like anargument, you need some form of escaping; each of " ’ \ haveslightly different meaning.CSE303 Spring 2008, Lecture 3 8'&$%Toward Scripts...A running shell has a state, i.e., a current• working directory• user• collection of aliases• history• ...In fact, next time we will 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.CSE303 Spring 2008, Lecture 3 9'&$%Running a scriptWhat if we want to run a bunch of commands 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 thingsCSE303 Spring 2008, Lecture 3 10'&$%Writing a script• Make the first line exactly: #!/bin/bash• Give yourself “execute” 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: listhomeCSE303 Spring 2008, Lecture 3 11'&$%Accessing argumentsThe script accesses the arguments with $i to get the ithone (name ofprogram is $0).Example: make thumbnail1Also very useful for homework: shift (manual Section 4.1)Example: countdownWe would like optional arguments and/or usage messages. Need:• way to find out the number of arguments• a conditional• some stuff we already haveExample: make thumbnail2CSE303 Spring 2008, Lecture 3 12'&$%More expressionsbash expressions can be:• math or string tests (e.g., -lt)• logic (&&, ||, !) (if you use double-brackets)• file tests (very common; see Pocket Guide)• math (if you use double-parens)Gotcha: parens and brackets must have spaces before and after them!Example: dcdls (double cd and ls) can check that arguments aredirectories.Exercise: script that replaces older file with newer oneExercise: make up your ownCSE303 Spring 2008, 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 user, 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.CSE303 Spring 2008, Lecture 3 14'&$%Variablesi=17 # no spacessetecho $iset | grep iecho $iunset iecho $if1=$1(The last is very useful in scripts before shifting, e.g., see homework.)Enough for your homework (arithmetic, conditionals, shift, variables,redirection, ...)Gotcha: using undefined variables (e.g., because of typo) doesn’t fail(just the empty string).CSE303 Spring 2008, Lecture 3


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
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?