Introduction To Advanced UNIX 2010
Introduction To Advanced UNIX 2010
March 9 2010
Kevin Keay
Outline
Overview Resources The basic form of a typical C-shell script Introduction to C-Shell Programming Advanced UNIX materials: The following are PDF documents that may be used for reference but wont be discussed in detail during this short course: Part 1 : Scripts Part 2 : A summary of useful software Part 3 : Reanalysis products
Overview
This is a practical course that is designed
to give you the basic skills to write UNIX C-shell scripts You can only truly understand the concepts by putting them into practice!
Overview (2)
We will look at the basic form of a C-shell script There will be a quick tour through Introduction to C-Shell
There are some extra advanced UNIX materials (Parts 1-3)
that you may need to reference. Some aspects will be looked at during the lab session but they are intended for personal reference Part 1 covers C-shell scripts and some additional concepts Part 2 is a summary of some useful software Part 3 focuses on downloading and decoding reanalysis data (NetCDF and GRIB) Finally, there is a short Lab session
Programming
Resources
A very useful and concise reference book covering UNIX and the C shell is:
UNIX in a Nutshell, OReilly and Associates (Engineering Library: 005.43
GILL).
A useful online resource from the University of Surrey, UNIX Tutorial for
Beginners, is available at: http://www.ee.surrey.ac.uk/Teaching/Unix/
http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/ you are using e.g. man awk. However sometimes the pages are not installed or are in an unexpected location. It may be more convenient to use a search engine like Google to find information on a command.
All UNIX commands should have a manual (man) page on the machine that
omitted with using set or @ - see below) The shell variable $#argv contains the number of arguments (items) typed on the command line. The items are referred to as shell variables $1, $2, $3, , $#argv
For example:
myscript jja -4.56 yes would give: $#argv = 3 $1 = jja $2 = -4.56 $3= yes
Shell variables are either string (text) or numeric A string variable is set (defined) by:
set var = value e.g. set x = Fred Note: Variables and values are case-sensitive i.e. x is not the same as X Variable names can be several characters e.g. set Case2 = 3 To print (display) the value of a variable: echo $var e.g. echo $x Fred
@ var = integer e.g. @ k = -7 Simple calculations may be performed e.g. @ j = ($k + 1) [j would be 7 + 1 = -6] @ k = ($k + 1) would change k from -7 to -6 Could also use C notation: @ k ++ @ j = (2 * $k - 5) [k= -7 would give j= -19] Note: Put spaces around operators like *, + etc. Floating point operations are not normally used in scripts but can be done with the command bc e.g. echo "2.1 + 6.3" | bc would print 8.4 on the screen set y = `echo "2.1 + 6.3" | bc` would save the answer in variable y
of a command (or user program) as in the bc example. In general: set var = `command` e.g. set ff = `ls *.dat` Remember: ls is used to list files. If the files are 5.dat 12.dat 13.dat then echo $ff would display: 5.dat 12.dat 13.dat
variable ff we can use $#ff to find the number of items (similar to $#argv) e.g. echo $#ff would display 3 echo $ff[1] would display 5.dat echo $ff[2] would display 12.dat echo $ff[3] would display 13.dat
repetitive procedure involving files: foreach var (files) commands end e.g. foreach f ([A-Z]*) # f is a file starting with a capital letter echo $f # Print filename mv $f $f.cap # Rename file to have extension .cap end
e.g.
if ($x == 6 || $s == sea) then
do something
else if ($x > 10) then
do something else
else
go here if neither of the above conditions is true
endif Simpler or more complex forms are possible e.g. if ($y == 2) ls l *.dat
An example
#!/bin/csh f if ($#argv != 1) then echo Usage: myscript name exit # If incorrect usage end the script else # If only 1 argument, branch here set n = ($1) # Put n equal to $1 endif echo Your name is $n
# You could do other things here
exit
Another example
# ff contains filenames *.dat @ nf = $#ff # nf equals the no. of files (items in ff) @j=1 # Set counter j to 1 while ($j <= $nf) # Loop while j <= nf set f = $ff[$j] # Set f to be the jth file ls l $f # List the details of f # We could use the file f as an argument to a command or program
myprog $f @j = ($j + 1) # Increment counter j by 1 end # Go here when j > nf echo We are finished! # A message to the screen
exit
Lab session
Lab session (informal) 2-3 PM UNIX
Lab See:
http://www.earthsci.unimelb.edu.au/~kevin/UNIX _Course/Advanced_UNIX_Lab_Session_2010.pdf