[ home ]
Computer Science 001
Introduction to Unix
September 1998
Part Two
An Example Shell script:
#! /bin/csh -f
# must have at least one argument on the command line
if ($# < 1) then
cat <<EOF;
Usage:
$0:t [argument]+
EOF
exit
endif
# x becomes the number of command line arguments
set x = $#
# print out info on the command line arguments
while ($x > 0)
echo "=========== $1 ==============="
if ( -e /home/staff/dutton/$1) then
echo "Exists in /home/staff/dutton"
if ( -r /home/staff/dutton/$1)
then
echo "and is readable"
else
echo "but is not readable"
endif
if ( -w /home/staff/dutton/$1)
then
echo "and is writeable"
else
echo "but is not writeable"
endif
else
echo "Does not exist in
/home/staff/dutton"
endif
## determine the system we are on
## useful in a .cshrc file
switch ("`uname -a`")
case IRIX64*:
echo "You're on and Irix
6.4 machine"
breaksw
case IRIX*:
exho "Your on an Irix 5.3
machine"
breaksw
case SunOS*4\.1*:
echo "You're on a SunOS
machine"
breaksw
case SunOS*5\.*:
echo "You're on a Solaris
machine"
breaksw
case Linux*:
echo "You're on a Linux
machine"
breaksw
default:
echo "You're not on our
system"
breaksw
endsw
@ x--
shift
# shifts argv to the left one place
end
foreach i ( `/bin/ls `)
if ( ! -d /home/staff/dutton/oo1/$i) then
# if it's not a d
irectory
## print some info on it (notice the back ticks)
/usr/bin/file /home/staff/dutton/oo1/${i}
endif
end
some sample output...
dutton@coral:oo1>./cshdemo
Usage:
cshdemo [argument]+
dutton@coral:oo1>!!
./cshdemo one
=========== one ===============
Does not exist in /home/staff/dutton
You're on a Linux machine
/home/staff/dutton/oo1/#outline#: ASCII text
/home/staff/dutton/oo1/Daytwo: HTML document text
/home/staff/dutton/oo1/Notes: HTML document text
/home/staff/dutton/oo1/cshdemo: C shell script text
/home/staff/dutton/oo1/kernel-2.0.35-2.i386.rpm: RPM v3 bin i386 kernel-2.0.35-2
/home/staff/dutton/oo1/manpage: English text
/home/staff/dutton/oo1/outline: HTML document text
/home/staff/dutton/oo1/source.tar: GNU tar archive
/home/staff/dutton/oo1/webmin-0.54.tar.gz: gzip compressed data, deflated,
original filename, last modified: Thu Jul 2 06:24:32 1998, os: Unix
/home/staff/dutton/oo1/xmeter: sparc demand paged dynamically linked
executable
dutton@coral:oo1>./cshdemo .cshrc .login .logout
=========== .cshrc ===============
Exists in /home/staff/dutton
and is readable
and is writeable
You're on a Linux machine
=========== .login ===============
Does not exist in /home/staff/dutton
You're on a Linux machine
=========== .logout ===============
Does not exist in /home/staff/dutton
You're on a Linux machine
/home/staff/dutton/oo1/#outline#: ASCII text
/home/staff/dutton/oo1/Daytwo: HTML document text
/home/staff/dutton/oo1/Notes: HTML document text
/home/staff/dutton/oo1/cshdemo: C shell script text
...
cat <<EOF;
Usage:
$0:t [argument]+
EOF
exit
set x = 1
while ($x >= 1)
if ($x == 3) then
@ x++
continue
endif
if ($x == 5) then
break
endif
echo "Loop iteration
$x"
@ x++
end
echo "End of script"
output of % ./cshdemo2
Loop iteration 1
Loop iteration 2
Loop iteration 4
End of script
start editor emacs edit 'myfile' emacs myfile edit 'myfile', put cursor on line 12 emacs myfile +12
suspend Emacs (or iconify it under X) C-z exit Emacs permanently C-x C-c
read a file into Emacs C-x C-f save a file back to disk C-x C-s insert contents of another file into this buffer C-x i write buffer to a specified file C-x C-wIn the Minibuffer (the bottom line of the screen), pressing TAB will complete the filename/command you are typing, or show you a list of possible completions.
tutorial C-h t apropos: show commands matching a string C-h a show the function a key runs C-h k describe a function C-h f read "info" pages C-h i when you can't find your answer anywhere else M-x doctor
abort partially typed or executing command C-g undo an unwanted change C-x u or C-` redraw garbaged screen C-l
search forward C-s search backward C-r repeat last search C-s C-s repeat last search backwards C-r C-r goto line XX M-x goto-line
entity to move over backward forward character C-b C-f line C-p C-n screen C-v M-v go to line beginning (or end) C-a C-e go to buffer beginning (or end) M-< M->
character delete DEL or C-d kill (to end of) line C-k set mark here C-@ or C-SPC exchange point and mark C-x C-x kill region C-w copy region to kill ring M-w yank back last thing killed C-y replace last yank with previous kill M-yTo kill 5 lines, pass a numeric argument to the kill function:
interactively replace a text string M-%At each occurrance you will be asked if you really, really want to replace. Answer either y or n, or press ! to replace all.
delete all other windows C-x 1 split window in two C-x 2 switch cursor to another window C-x o
select another buffer C-x b list all buffers C-x C-b kill a buffer C-x k
start defining a keyboard macro C-x ( end keyboard macro definition C-x ) execute last-defined keyboard macro C-x e
;; The following line of code allows the BACKSPACE key to be used ;; as the DELETE key. (define-key global-map "\C-h" 'delete-backward-char) ;; The following line defines C-c C-h to get help. ;; Normally the BACKSPACE key is used for help. (define-key global-map "\C-c\C-h" 'help-for-help) ;; make f8 the goto-line key (global-set-key [f8] 'goto-line) ;; show what line number you're on at the bottom of the screen (setq line-number-mode t).emacs can also be used to change to fonts/colours for comments or strings in your programs. For an example see ~rf/.emacs
Copyright c 1993 Free Software Foundation, Inc.
designed by Stephen Gildea, May 1993 v2.0
for GNU Emacs version 19 on Unix systems
Permission is granted to make and distribute copies of this card provided the
copyright notice and this permission notice are preserved on all copies.
For copies of the GNU Emacs manual, write to the Free Software Foundation,
Inc., 675 Massachusetts Ave, Cambridge MA 02139.