Download presentation
Presentation is loading. Please wait.
Published byAlexia Grant Modified over 8 years ago
1
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones
2
Lecture 202 Overview Announcements Story Hour, Houser 108, 3PM Friday MP2 Shell Programming
3
Lecture 203 Concepts of shell programming Prerequisites—Operating system provides or can be made to provide the following: Files are streams of bytes i.e. not records Multiple processes Processes have associated with them a standard input, standard output, and standard error
4
Lecture 204 Standard I/O Streams standard input or files command, options standard error standard output C++CJava inputcinstdinSystem.in outputcoutstdoutSystem.out errorcerrstderrSystem.err
5
Lecture 205 Concepts of shell programming Commands are executed by their name Not with special command “run command” Shell is not a special program directly supported by the operating system Lots of small programs that do one thing only Regular expressions are used by the shell and many of the small programs I/O can be redirected Shell is an interpreted programming langauge
6
Lecture 206 Commands General syntax: command flags arguments Searched for using PATH variable Colon separated list of directories PATH=/bin:/usr/bin:~/bin Searched in order
7
Lecture 207 Lots of small, single purpose programs wc Counting words, characters lines pr Formatting text to fit on a page cat Direct contents of file(s) to standard output ls List contents of directories grep Search files or standard input for pattern sort Sort contents of file or standard input tail print last lines of file or standard input diff print differences between two files sed print, substitute files or standard input
8
Lecture 208 Regular Expressions are used by shell and many small programs Shell’s regular expressions are simplified * match any string of zero or more characters in file names ? match any single character in file names \c take character c literally ‘…’take … literally [ccc] match any single character from ccc in file names, ranges like 0-9 or a-z are legal Commands like grep are more sophisticated (re | re ) matches first or second r.e.
9
Lecture 209 I/O Redirection Files > standard output to file, overwriting >> standard output to file, appending < standard input to file <<str standard input from shell until str 2> standard error to file Pipes | direct standard output of command on left to standard input of command on right
10
Lecture 2010 Shell is an interpreted programming language Control Flow (useful interactively) p1 & runs p1 without waiting for completion Also called running in “background” p1 ; p2 sequencing, run p1, then p2 p1 && p2 conditional execution run p1, if successful, run p2 p1 || p2 conditional execution, run p1 if unsuccessful, run p2 `…` run commands in … ; output replaces `…` (…) run commands in sub-shell Useful for running commands in different directory E.g. (cd ~/foo; grep i *.c)
11
Lecture 2011 Shell is an interpreted programming language (cont.) Variables var=value variable assignment $1, $2, $3 arguments to shell script $var value of shell variable var ${var} values of var, useful when concatenating Comments # if # starts word, rest of line is a comment
12
Lecture 2012 Shell is a programming language also Control flow if command then commands else commands fi for var in stuff do commands done case expr in pat) command … esac
13
Lecture 2013 Example - which command # which cmd: which cmd in PATH is executed, version 1 case $# in 0) echo ‘Usage: which command’ 1>&2; exit 2 esac for i in `echo $PATH | sed ‘s/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g’` do if test -f $i/$1 then echo $i/$1 # found it exit 0 fi done exit 1 # not found
14
Lecture 2014 Example which command What happens if there is a program called test in the current directory? (assuming that test isn’t a shell built-in)
15
Final version of which # which cmd: which cmd in PATH is executed, final version opath=$PATH PATH=/bin:/usr/bin case $# in 0) echo ‘Usage: which command’ 1>&2; exit 2 esac for i in `echo $opath | sed ‘s/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g’` do if test -f $i/$1 then echo $i/$1 # found it exit 0 fi done exit 1 # not found
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.