<hr
size=2 width="100%" align=center>
Using The Shell
A Bit
of History
The
Command Line
Metacharacters
Shell
variables
Command
arguments
Command
output as arguments
I/O
redirection
Other
features
Reading:
The Unix Programming Environment, Chapter 3
- The book refers to "the
shell"
- Not "the Bourne
shell,"
- Not "the
C-shell" (aka “csh”)
- Not "the
Bourne-Again shell" (aka. "bash")
- "The shell" here
means "Bourne shell"
- The primary shell
around at the time the book was written
- The C-shell did exist
at the time the book was written
- It is mentioned
briefly on p. 100
- csh added many useful
interactive features
- command history
- job control
- the ability to edit
previous commands
- Why focus on Bourne Shell?
- It's standard
- It's available
everywhere
- It's the shell most
Unix system scripts are written in
- There are some serious flaws
that make the C-shell less suitable for programming
- Many newer shells,
such as bash, zsh, and ksh, offer the better syntax of Bourne Shell along
with the better interactive features of C-shell
- The shell interperets each
line of text as a whole and then executes the specified commands with the
specified arguments
- Generally it's one command
per line
- Commands may also be
separated by ';'
- Commands may be grouped into
a subshell
- Using parentheses
- Can be rather useful
- The output of the
subshell can be piped
- The whole subshell
can be put into the background
- You can do some
limited scoping of variables
- Commands can be put into the
background with '&'
- "In the background"
means the shell doesn't wait for the command to complete
- Useful for long jobs
- The '|' character creates a
pipe
- The output of the
first command is the input of the second command
- date; who | wc
- (date; who) | tee save | wc
cat save
- (sleep 5; echo boo)
& echo date
- sleep 5; echo boo;
date
- sleep 5; echo boo
&
- (sleep 5; echo boo)
&
- Many characters are
"special" to the shell and are interpreted before any commands
are run
- These are collectively
known as "metacharacters"
- Table 3.1 on p. 76
gives a complete list
- String matching
metacharacters
- Matches zero or more
characters, except for an initial '.'
- echo *
- echo a*
- echo *.*
- echo *a*
- Matches a single
character
- echo ?
- echo abcd?
- Matches a single
character from the list enclosed in brackets
- echo [ax]*
- echo abc[0-9]
- What if you want to
match a metacharacter itself?
- Use a ' \'
- Use single quotes
(like in the line above)
- You can even escape
the newline
- Variables are assigned with
the syntax var=value
- There must be no
whitespace around the '='
- The shell treats
whitespace as significant
- Variables do not need
to be declared before use
- There are no data
types
- Variables are referenced with
the syntax $var
- Variables are specific to
their shell
- Not passed up from
subshells
- The '.' construct lets
you read in a file and run it in the current shell
- Useful for things
like .profile
- Or loading some
default values from a file
- The arguments to a shell
command are placed into special variables
- The first is $1
- The second is $2
- etc. through $9
- The command itself is $0
- $* and $@
refer to all of the arguments, with some differences we'll cover when we cover
shell scripting
- The output of one command can
become a parameter for another
- By using backquotes
'`'
- (The character next to
'1' on most keyboards)
- echo "The time is `date`, you know"
- Here is what Prof. Bakken
wants on his tombstone below the usual stuff:
% echo “GO
COUGS!”
% echo “The Huskies
still suck as of `date`”
- The alert reader will notice
that the above could also be done without the backquotes
command output:
% echo “GO
COUGS!”
% echo –n
“The Huskies still suck as of ”; date
- The Bourne shell offers a
great deal of control over I/O
- >
- Sends standard out
(stdout) to the named file
- Appends stdout
to the named file
- Takes standard in
(stdin) from the named file (rather than the terminal)
- All files that are
open in a shell have a number assigned to them
- That number can be
used to refer to that particular file
- Remember that
everything is a file, including stdin and stdout
- stdin is 0
- stdout is 1
- standard error
(stderr) is 2
- Made a separate thing
so that errors can go to the terminal even if the command output is
redirected
- A file other than stdout (i.e
stderr) can be redirected with the syntax grep
x * 2>/tmp/error.out
- There can't be a space
between the file descriptor and the redirect
- 2>>file does what you would
expect
- time ls /bin >/tmp/ls.out 2>&1
- The merge must be done
after the redirection
- The parsing happens
left-to-right
- This sort of
redirection is one of the things that C-shell is weak in
·
od (-bc)
: peek at contents in files
·
ps (-aux): see what processes are
running
·
uptime:
see how long computer has been up, what load is, etc.
·
top:
see the most resource-hogging processes
·
cat (-n):
dump out (concatenate) contents of files
·
kill (-9
or %1 etc): kill a running process (that you
own)
Other
features
We'll discuss more shell features (including control structures like if, for,
and while) when we talk about
shell scripting