introduction to advanced unix 2010
TRANSCRIPT
-
8/3/2019 Introduction to Advanced UNIX 2010
1/22
Introduction to Advanced
UNIXMarch 9 2010
Kevin Keay
-
8/3/2019 Introduction to Advanced UNIX 2010
2/22
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 forreference but wont be discussed in detail during this short course:
Part 1 : ScriptsPart 2 :A summary of useful softwarePart 3 : Reanalysis products
Lab session (informal) 2-3 PM UNIX LabSee: http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdf
http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_1_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_2_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_3_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_3_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_2_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_1_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdf -
8/3/2019 Introduction to Advanced UNIX 2010
3/22
Overview
This is a practical course that is designedto give you the basic skills to write UNIX
C-shell scriptsYou can only truly understand the
concepts by putting them into practice!
-
8/3/2019 Introduction to Advanced UNIX 2010
4/22
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
Programming
There are some extra advanced UNIX materials (Parts 1-3)that you may need to reference. Some aspects will belooked at during the lab session but they are intended forpersonal 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 reanalysisdata (NetCDF and GRIB)
Finally, there is a short Lab session
-
8/3/2019 Introduction to Advanced UNIX 2010
5/22
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.43GILL).
A useful online resource from the University of Surrey, UNIX Tutorial forBeginners, is available at: http://www.ee.surrey.ac.uk/Teaching/Unix/
For PDF files of the handouts see:http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/
All UNIX commands should have a manual (man) page on the machine thatyou 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 searchengine like Google to find information on a command.
http://www.ee.surrey.ac.uk/Teaching/Unix/http://www.ee.surrey.ac.uk/Teaching/Unix/http://www.ee.surrey.ac.uk/Teaching/Unix/http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/http://www.ee.surrey.ac.uk/Teaching/Unix/ -
8/3/2019 Introduction to Advanced UNIX 2010
6/22
The basic form of a typical C-shell script
The first line of a C-shell script (text file) is:#!/bin/cshf
Thef option tells the script not to source (read)the users .cshrc file; this is faster and also
makes the script more portable. All other lines starting with # are comments Commands may be continued onto subsequent
lines with \
Multiple commands can be placed on a singleline with ;
Spacesaround operators and commands are
required but there are no other restrictions
-
8/3/2019 Introduction to Advanced UNIX 2010
7/22
The basic form of a typical C-shell script (2)
For neatness, end the script with exit (notessential)
Shell variablesstart with a $ (this is onlyomitted with using set or @ - see below) The shell variable $#argv contains the
number of arguments(items) typed onthe command line. The items are referredto as shell variables $1, $2, $3, , $#argv
-
8/3/2019 Introduction to Advanced UNIX 2010
8/22
The basic form of a typical C-shell script (3)
For example:myscript jja -4.56 yes
would give:
$#argv = 3
$1 = jja
$2 = -4.56
$3= yes
-
8/3/2019 Introduction to Advanced UNIX 2010
9/22
The basic form of a typical C-shell script (4)
Shell variablesare either string(text) or numeric A string variable is set (defined) by:set var = valuee.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 $vare.g. echo $xFred
-
8/3/2019 Introduction to Advanced UNIX 2010
10/22
The basic form of a typical C-shell script (5)
A numeric variable is set (defined) by:
@ var = integere.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 -6Could 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 bce.g. echo "2.1 + 6.3" | bcwould print 8.4 on the screenset y = `echo "2.1 + 6.3" | bc`would save the answer in variable y
-
8/3/2019 Introduction to Advanced UNIX 2010
11/22
The basic form of a typical C-shell script (6)
A shell variable may be given the outputof a command (or user program) as in thebc 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 thenecho $ffwould display:
5.dat 12.dat 13.dat
-
8/3/2019 Introduction to Advanced UNIX 2010
12/22
The basic form of a typical C-shellscript (7)
To extract the individualelements ofvariable ff we can use $#ffto 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.datecho $ff[3] would display 13.dat
-
8/3/2019 Introduction to Advanced UNIX 2010
13/22
The basic form of a typical C-shell script (8)
A while loop is used to set up an iterative orrepetitive procedure:
while (condition is true)
commands
end
e.g. @ k = 1 # Initialise k
while ($k
-
8/3/2019 Introduction to Advanced UNIX 2010
14/22
The basic form of a typical C-shell script (9)
A foreach loop is used to set up an iterative orrepetitive 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 .capend
-
8/3/2019 Introduction to Advanced UNIX 2010
15/22
The basic form of a typical C-shell script (10)
An if-else-endifstructure is used to control scriptbranching:
if (condition 1 is true)
commands
else if (condition 2 is true)
commands
else
commandsendif
Note: use else if notelseif
-
8/3/2019 Introduction to Advanced UNIX 2010
16/22
The basic form of a typical C-shell script (11)
e.g.if ($x == 6 || $s == sea) then
do something
else if ($x > 10) thendo something else
else
go here if neither of the above conditions is trueendif
Simpler or more complex forms are possible
e.g. if ($y == 2) lsl *.dat
-
8/3/2019 Introduction to Advanced UNIX 2010
17/22
The basic form of a typical C-shell script (12)
The while or if(condition) uses C notationfor logical operations:|| or
&& and
== equal>= greater than or equal
greater than
< less than
-
8/3/2019 Introduction to Advanced UNIX 2010
18/22
The basic form of a typical C-shell script (13)
An example#!/bin/cshf
if ($#argv != 1) then
echo Usage: myscript nameexit # If incorrect usage end the script
else # If only 1 argument, branch here
set n = ($1) # Put n equal to $1
endifecho Your name is $n
# You could do other things here
exit
-
8/3/2019 Introduction to Advanced UNIX 2010
19/22
The basic form of a typical C-shell script (14)
Another example
#!/bin/cshfset ff = `ls *.dat` # ff contains filenames *.dat@ nf = $#ff # nf equals the no. of files (items in ff)
@ j = 1 # Set counter j to 1
while ($j
-
8/3/2019 Introduction to Advanced UNIX 2010
20/22
Introduction to C-shell programming
Refer to the PDF guide:http://www.earthsci.unimelb.edu.au/~kevin
/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdf
http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_C-Shell_Programming_2008.pdf -
8/3/2019 Introduction to Advanced UNIX 2010
21/22
Advanced UNIX materials
The following are PDF documents thatmay be used for reference but wont be
discussed in detail during this shortcourse:
Part 1 : Scripts
Part 2 :A summary of useful software
Part 3 : Reanalysis products
http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_1_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_2_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_3_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_3_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_2_2008.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Intro_to_Advanced_UNIX-Part_1_2008.pdf -
8/3/2019 Introduction to Advanced UNIX 2010
22/22
Lab session
Lab session (informal) 2-3 PM UNIXLab
See:http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdf
http://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdfhttp://www.earthsci.unimelb.edu.au/~kevin/UNIX_Course/Advanced_UNIX_Lab_Session_2010.pdf