lecture
in color
ps -ef
ps
-ef
man ps
-.
ls -l
- by itself means use standard input or output as a file.
tar -cf - sends output to standard output.
~/.namerc where
name is the name of the command.
.cshrc - startup for csh and tcsh
.mailrc - startup commands for mail and mailx.
.pinerc _ startup commands for pine.
.profile - startup for sh.
.emacs - non-conformist.
argv and envp that contain
the arguments (as strings). You can grab them
and use them in your program!
contents of chainsaw/args.c...
/* simple C program does nothing but print its arguments and environment */
#include <stdio.h>
int main (int argc, char **argv, char **envp) {
/* ^^^^^^^^^^^ array of environment strings
^^^^^^^^^^^ array of argc strings
^^^^^^^^ argument count
*/
int i;
printf("I have %d arguments\n",argc);
for (i=0; i<argc; i++) /* there are argc of these */
printf("argv[%d] is '%s'\n", i, argv[i]);
printf("Here's my environment:\n");
for (i=0; envp[i]!=0; i++) { /* last string is NULL (0) */
printf("envp[%d] is '%s'\n",i,envp[i]);
}
}
...end of chainsaw/args.c
gcc -g args.c -o argsto create a command
args, and then invokes the command with
args foo bar catthen the result is:
contents of chainsaw/args.out... I have 4 arguments argv[0] is 'args' argv[1] is 'foo' argv[2] is 'bar' argv[3] is 'cat' Here's my environment: envp[0] is 'MANPATH=/local/sol/5.7/man:/local/man:/usr/share/man:/usr/openwin/share/man' envp[1] is 'DTDATABASESEARCHPATH=/u/f/couch/.dt/types,/etc/dt/appconfig/types/%L,/etc/dt/appconfig/types/C,/usr/dt/appconfig/types/%L,/usr/dt/appconfig/types/C' envp[2] is 'DTXSERVERLOCATION=local' envp[3] is 'LANG=C' envp[4] is 'HELPPATH=/usr/openwin/lib/help' envp[5] is 'PATH=.:/u/f/couch/bin:/u/f/couch:/local/sol/5.7/bin:/local/bin:/local/games:/usr/openwin/bin:/usr/bin:/usr/ucb:/usr/ccs/bin:/local/hosts' envp[6] is 'AB_CARDCATALOG=/usr/dt/share/answerbooks/C/ab_cardcatalog:/usr/dt/share/answerbooks/C/ab_cardcatalog' envp[7] is 'DTUSERSESSION=couch-blackhole-0' envp[8] is 'XMICONBMSEARCHPATH=/u/f/couch/.dt/icons/%B%M.bm:/u/f/couch/.dt/icons/%B%M.pm:/u/f/couch/.dt/icons/%B:/etc/dt/appconfig/icons/%L/%B%M.bm:/etc/dt/appconfig/icons/%L/%B%M.pm:/etc/dt/appconfig/icons/%L/%B:/etc/dt/appconfig/icons/C/%B%M.bm:/etc/dt/appconfig/icons/C/%B%M.pm:/etc/dt/appconfig/icons/C/%B:/usr/dt/appconfig/icons/%L/%B%M.bm:/usr/dt/appconfig/icons/%L/%B%M.pm:/usr/dt/appconfig/icons/%L/%B:/usr/dt/appconfig/icons/C/%B%M.bm:/usr/dt/appconfig/icons/C/%B%M.pm:/usr/dt/appconfig/icons/C/%B' envp[9] is 'SESSION_SVR=blackhole' envp[10] is 'OPENWINHOME=/usr/openwin' envp[11] is 'EDITOR=vi' envp[12] is 'LOGNAME=couch' envp[13] is 'DTSCREENSAVERLIST=StartDtscreenSwarm StartDtscreenQix StartDtscreenFlame StartDtscreenHop StartDtscreenImage StartDtscreenLife StartDtscreenRotor StartDtscreenPyro StartDtscreenWorm StartDtscreenBlank' envp[14] is 'MAIL=/var/mail/couch' envp[15] is 'USER=couch' envp[16] is 'DISPLAY=:0.0' envp[17] is 'SHELL=/usr/bin/tcsh' envp[18] is 'DTAPPSEARCHPATH=/u/f/couch/.dt/appmanager:/etc/dt/appconfig/appmanager/%L:/etc/dt/appconfig/appmanager/C:/usr/dt/appconfig/appmanager/%L:/usr/dt/appconfig/appmanager/C' envp[19] is 'HOME=/u/f/couch' envp[20] is 'XFILESEARCHPATH=/usr/openwin/lib/locale/%L/%T/%N%S:/usr/openwin/lib/%T/%N%S:/usr/openwin/lib/locale/%L/%T/%N%S:/usr/openwin/lib/%T/%N%S' envp[21] is 'XMICONSEARCHPATH=/u/f/couch/.dt/icons/%B%M.pm:/u/f/couch/.dt/icons/%B%M.bm:/u/f/couch/.dt/icons/%B:/etc/dt/appconfig/icons/%L/%B%M.pm:/etc/dt/appconfig/icons/%L/%B%M.bm:/etc/dt/appconfig/icons/%L/%B:/etc/dt/appconfig/icons/C/%B%M.pm:/etc/dt/appconfig/icons/C/%B%M.bm:/etc/dt/appconfig/icons/C/%B:/usr/dt/appconfig/icons/%L/%B%M.pm:/usr/dt/appconfig/icons/%L/%B%M.bm:/usr/dt/appconfig/icons/%L/%B:/usr/dt/appconfig/icons/C/%B%M.pm:/usr/dt/appconfig/icons/C/%B%M.bm:/usr/dt/appconfig/icons/C/%B' envp[22] is 'TERM=dtterm' envp[23] is 'dtstart_sessionlogfile=/dev/null' envp[24] is 'TZ=US/Eastern' envp[25] is 'DTHELPSEARCHPATH=/u/f/couch/.dt/help/couch-blackhole-0/%H:/u/f/couch/.dt/help/couch-blackhole-0/%H.sdl:/u/f/couch/.dt/help/couch-blackhole-0/%H.hv:/u/f/couch/.dt/help/%H:/u/f/couch/.dt/help/%H.sdl:/u/f/couch/.dt/help/%H.hv:/usr/dt/appconfig/help/%L/%H:/usr/dt/appconfig/help/%L/%H.sdl:/usr/dt/appconfig/help/%L/%H.hv:/usr/dt/appconfig/help/C/%H:/usr/dt/appconfig/help/C/%H.sdl:/usr/dt/appconfig/help/C/%H.hv' envp[26] is 'XMBINDDIR=/usr/dt/lib/bindings' envp[27] is 'SHLVL=2' envp[28] is 'HOST=blackhole' envp[29] is 'HOSTTYPE=sun4' envp[30] is 'CLASSES=' envp[31] is 'LD_LIBRARY_PATH=/local/sol/5.7/lib:/usr/openwin/lib:/usr/dt/lib:/loc/X11/lib:/local/lib:/usr/ccs/lib' envp[32] is 'FONTPATH=/usr/openwin/lib/fonts' envp[33] is 'XAPPLRESDIR=/local/X11/lib/X11/app-defaults' envp[34] is 'MOZILLA_HOME=/loc/xclients/netscape-v461' envp[35] is 'MORE=-c' envp[36] is 'WINDOWID=79691829' envp[37] is 'TERMINAL_EMULATOR=dtterm' envp[38] is 'PWD=/g/150NET/public_html/n/chainsaw' envp[39] is 'PGLIB=/loc/postgres/lib' envp[40] is 'PGDATA=/loc/postgres/data' envp[41] is 'SEEPLEXPATH=/u/f/couch/current/seeplex/environ:.:../Defaults' envp[42] is 'CLASS=15' ...end of chainsaw/args.out
csh script that tells you about its arguments.
contents of chainsaw/args.csh... #! /bin/csh # demonstration of argument variables inside scripts echo I was invoked by the command \'$0\' echo All my arguments are \'$argv[*]\' echo My first argument is \'$1\' or \'$argv[1]\' echo My second argument is \'$2\' or \'$argv[2]\' set i = 1 while ($i <= $#argv) echo argument $i is \'$argv[$i]\' @ i = $i + 1 end ...end of chainsaw/args.csh
chmod +x args.cshand then execute
args.csh foo bar catthen the result is
contents of chainsaw/args.csh.out... I was invoked by the command 'args.csh' All my arguments are 'foo bar cat' My first argument is 'foo' or 'foo' My second argument is 'bar' or 'bar' argument 1 is 'foo' argument 2 is 'bar' argument 3 is 'cat' ...end of chainsaw/args.csh.out
$0 is the command name for the script.
$argv[*] (or $*) is an array of all arguments.
$argv[1] (or $1) is the first argument.
$argv[2] (or $2) is the first argument.
$#argv.
find: search directories for files matching
particular criteria.
awk: a language for processing tables.
sed: the stream editor: a text editor that works on pipes.
man find for details.
find . -name foo -print
find: the command name.
.: the directory in which to start looking
for files matching criteria. . means your current
directory.
-name foo: the name of the file should be foo.
-print: what to do with that file: print its name.
find skips the current file or directory.
find /bin -type f -owner root -lssays the following:
/bin
-type f)
-owner root)
ls it (-ls).
find /bin -name *b -printand
find /bin -name \*b -print
*b with a list of
every file in the current directory ending with b,
before calling find.
*b to find itself.
The find command checks every file in the whole hierarchy
against this pattern.
./bar ./foo ./joe/dab ./joe/naband that your current directory contains the hierarchy of files:
./cat/dog ./fob ./junk ./habCompare what happens when the two
find commands above are
executed.
find parensfind, parens group sections of a program.
find /usr \( -name cat -o -name dog \) -lsfinds all files or directories in
/usr that are named
either cat or dog.
-o: "or", allows more than one possibility.
\( .. \): the scope of the possibilities.
find /usr -name cat -o -name dog -lsmeans
find /usr \( -name cat -o -name dog -ls \)which does something truely weird.
cat are found but not printed.
dog are listed with ls!
find /usr (-name cat -o -name dog) -lswithout escaping
()?
find
find to scan content of files.
% ls % echo $status 0 % # means everything was OK
% grep foo file % echo $status 1 % # means that the wordfoowasn't found infile
man pages for what they mean.
grep, 0 means it worked, 1 means "no match".
find, each step can be either a test or an
action (or both).
-ls, -print always succeed.
-exec to run a command,
it succeeds if the exit code of the command is 0.
find trickfoo
find . -type f -exec grep -l foo {} \; -print
-type f makes sure we have a file, not a directory!
-exec grep -l foo {} \; executes an embedded command
within the find.
-exec starts an embedded command.
\; ends an embedded command. ; without \ would end the whole find command.
{} represents the pathname to be tested.
If that file was ./cat/dog the command would be
grep -l foo ./cat/dog
awkls -l output contains fields
separated by spaces.
awk syntaxawk program: patterns and actions.
{} describe criteria that determine
whether a a line of input will be processed.
{} actions say what to do (when a line matches).
$1,$2,$3,... are fields in the line.
$ needed).
awk examples
awk '$2 == "couch" { print }' <joe
print every line of the file joe in which couch
is the second word in the line. Note use of shell quote ''
to avoid shell evaluation of the awk program!
ps -ef | awk '{ print $2 }'
Print the process identifier (PID : field 2) of
every executing process.
ps -ef | awk '$1 == "root" { print $2 }'
Print the process number of all processes owned by the superuser (root).
/../.
[abc]: one of the characters
a, b, or c
[^abc]: any character that's not
a, b, or c
[a-z]: any lowercase character.
.: one character of any kind, except newline.
(similar to ? in the shell)
\(ab|bz\): either 'ab' or 'bz'
(similar to {ab,bz} in the shell)
X?: 0 or 1 appearances of X.
X*: 0 or more appearances of X.
X+: 1 or more appearances of X.
X can be replaced by a bracket
expression, a parenthetic expression, etc. Each of ?,
* operates on the single thing immediately preceding it.
^ represents the beginning of a line.
$ represents the end of a line.
\t: matches a tab character.
\n: matches a newline character.
\ in front.
\[ and
\] represent the literal characters open
and close bracket (instead of the pattern operators).
\* represents a literal asterisk.
\. represents a literal period.
\/ represents a literal / (which would
otherwise end the pattern).
awk patterns need not represent
the complete pattern unless you want them to.
/./: matches any text at all
(not containing a \n).
/a/: matches any text containing an a anywhere
within it.
/b$/: matches any text ending in b.
/^c/: matches any text beginning with c.
/[^x]/: matches any text containing any character that
isn't x (including, e.g., xy).
c and that she's logged on now.
users | awk '$1 ~ /^c/ { print }'
will print all people satisfying those criteria!
ls -l prints
drwx------ 2 couch student 512 Jun 20 2000 Archives drwxr-xr-x 2 couch student 512 May 17 2000 Fall99 drwxr-xr-x 2 couch student 512 May 17 2000 NEWnotes -rwxr-xr-x 1 couch student 2355 Jan 20 1999 NOTES drwxr-xr-x 2 couch faculty 2048 Sep 7 14:14 OldHtml -rwxr-xr-x 1 couch student 726 Jan 19 1999 SCHEDULE -rwxr-xr-x 1 couch student 657 Jan 19 1999 TODOthen what does this command do?
ls -l | awk '$1 ~ /^-/ && $5 > 100 { print $9 "(" $6 ")" }'
awk.
awk,
arrays are indexed by strings.
% ps -e
PID TTY TIME CMD
0 ? 0:01 sched
1 ? 0:47 init
2 ? 0:37 pageout
3 ? 52:56 fsflush
203 ? 1:10 utmpd
101 ? 0:04 keyserv
295 ? 0:00 sac
99 ? 0:07 rpcbind
ps -e | awk 'NR != 1 {print}'
ps -e | \
awk 'NR != 1 { if (run[$4]=="") cmd[++len]=$4; run[$4]++ }'
Really subtle things going on here!
cmd[1] to cmd[len] are a list of commands
seen so far.
run["foo"] is the number of times the command
"foo" is running.
if (run[$4]=="")
Check if this command has been encountered before. If not,
we won't have a record of it.
cmd[++len]=$4 make a list of ones you haven't seen.
len is how many things you have in it.
run[$4]++ keep track of how many times this
command is running. This array is indexed by command name.
awk, things are arranged so that the simplest
depiction of a program usually works.
"".
"" is 0.
len is undefined, ++len sets len to 1.
BEGIN: specifies actions to occur before reading input.
END: specifies actions to occur after reading all input.
ps -e |
awk 'NR != 1 { if (run[$4]=="") cmd[++len]=$4; run[$4]++ }
END { for (i=1; i<=len; i++) print run[cmd[i]] "\t" cmd[i] }'
(indented here for readability).
len is the number of commands seen,
cmd contains those commands,
cmd[i] is a command name (1<=i<=len)
run["foo"] is the number of times that foo
is running then:
cmd[i] is running run[cmd[i]] times!
awk scripts
can get long.
contents of chainsaw/runner.awk...
BEGIN { len=0; }
NR != 1 { if (run[$4]=="") cmd[len++]=$4;
run[$4]++;
}
END { for (i=0; i<len; i++)
print run[cmd[i]] "\t" cmd[i];
}
...end of chainsaw/runner.awk
and you invoke awk by giving the name of the file:
awk -f runner.awkthen all will work as before.
BEGIN>
sedawk works on structured input
(such as tables and reports).
sed
sed conceptssed program, like an awk program,
is a list of pattern/action pairs.
-f option just
as in awk)
sed examplessed -e '5d'
contents of chainsaw/append.sed... 3a\ kilroy was here. ...end of chainsaw/append.sedCall this with
sed -f append.sed
couch:
contents of chainsaw/insert.sed... /couch/i\ (All opinions expressed herein are the sole responsibility of the author!) ...end of chainsaw/insert.sed
/couch/ is a pattern match.
This one selects all lines containing couch.
sed: pattern search
and replacement.
s/pattern/replacement/: replace one pattern with another.
couch with the Grinch
everywhere it appears in the input:
sed -e 's/\<couch\>/the Grinch/'
\<, \> match
only word boundaries.
sed: reformat input for awk
processingawk could always do what we want.
awk can
handle it.
awk can't make much sense of it.
sed command to make everything
line up correctly so awk will work.
ps -ef timestampsps -ef isn't perfectly columnar.
$1 $2 $3 $4 $5 $6 $7 $8 $9 couch 11313 11292 0 Jan 24 pts/4 0:12 /usr/bin/tcsh $1 $2 $3 $4 $5 $6 $7 $8 couch 13721 13642 0 12:12:04 pts/3 9:46 netscape http://www.eecs.tufts.edu/g/150NET/n
Jan 24 to Jan:24.
ps -ef | sed -e 's/Jan /Jan:/'
ps -aux: BSD
ps -efl: SysV
ps aux: linux
/usr/local/bin: locally installed commands for most people.
/local/bin: our old convention inside EECS.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|
| apropos | at | atq | atrm | awk | basename | bash | bz2unzip | bz2zip | cancel |
| cat | chgrp | chmod | chown | cmp | cp | csh | cu | date | dd |
| df | diff | dig | dos2unix | du | echo | ed | ed | egrep | ex |
| fgrep | file | find | finger | ftp | g++ | gcc | grep | gunzip | gzip |
| head | join | kill | ksh | less | ln | lp | lpstat | mailx | |
| make | man | mkdir | more | mv | netstat | nice | niscat | nismatch | nroff |
| nslookup | passwd | pine | pkill | pwd | rcp | rdate | rdist | rehash | rlogin |
| rmdir | rsh | scp | sed | sh | sleep | sort | spell | split | ssh |
| stty | sync | tail | tcsh | tip | tty | uname | unix2dos | unzip | uptime |
| users | vi | w | wc | whatis | where | who | whois | write | zip |
lecture
in color