#! /bin/sh ############################################################ # This shell script is a "front-end" to 2body which # expedites the analysis of the results from that code, # including the generation of Postscript plots of the # particle position, d(energy), d(angular momentum) as a # function of time using gnuplot. # # The script is also intended as a general illustration of # shell programming. A key idea here is that many things # which are cumbersome in Fortran (or C, or C++) # (such as file manipulation, and piecing together different # commands) are easily accomplished with a script. # # See the Unix Programming Environment by Kernighan and Pike # for classic documentation on shell programming and more! ############################################################ ############################################################ # Note that the first line of this file is very important. # It allows Unix to identify the contents of the file as # a shell script ('/bin/sh' is the shell executable on # ALL non-brain-dead Unix systems), so that, provided the # file is made executable and resides in a directory in # the current path, typing the name of the file will # result in execution of the script. The space between # the '#!' and '/bin/sh'is recommended for maximum script # portability. # # A variant initial line, particularly useful for script # development is # # #! /bin/sh -x # # The '-x' flag enables tracing, so that assignments of # shell variables, execution of commands etc. will be # echoed to standard error. ############################################################ ############################################################ # ASSIGNMENT STATEMENTS # # The following code assigns some default values to some # shell variables. # # NOTE: There can be *no* white # space around the '=' in an assignment statement. If, # for example you type # # tmax = 5.0 # # then, when you execute the script, you will get the # cryptic error message # # tmax: Not found # # since the shell will be interpreting the line as the # *command* 'tmax' with arguments '= 5.0'. ############################################################ tmax=5.0 dt=0.05 tol=1.0d-6 ############################################################ # FUNCTIONS # # Usage function (subroutine). Illustrates one way to # "do usage" in shell script (using 'cat' plus a # "here document"), as well as the useful "here document" # construction. # # Here documents can be used anywhere # in a shell script (and in C-shell, Korn-shell, perl etc. # script) to provide "in-place" input for the standard input # of a command. You can refer to the man page on 'sh' # for full details, but the idea and mechanics are # simple. To provide "in-place" input to an arbitrary # command, append '< [] Default tol: $tol y0 = 1.0 will produce circular orbit. To enable automatic previewing of Postscript files set GHOSTVIEW environment variable to any non-blank value, e.g. setenv GHOSTVIEW on END ############################################################ # Exit with non-zero status (failure). ############################################################ exit 1 } ############################################################ # Subroutine (fcn) to produce postscript version of # gnuplot plot of data stored in file $1. Postscript # file will be called $1.ps. If optional second argument # is supplied, the resulting Postscript file will be # 'ghostview'ed. Again, note use of "here document"; # indentation of "here document" is strictly for readability. ############################################################ gnuplot_it(){ gnuplot< $ofile ############################################################ # Use 'nth' to create-two column data files as follows # # xcyc_$tag: xc yc # xc_$tag: t xc # yc_$tag: t yc # dEtot_$tag t dEtot # dJtot_$tag t JEtot ############################################################ nth 2 3 < $ofile > xcyc_$tag nth 1 2 < $ofile > xc_$tag nth 1 3 < $ofile > yc_$tag nth 1 4 < $ofile > dEtot_$tag nth 1 5 < $ofile > dJtot_$tag ############################################################ # FOR LOOPS # # General form: # # for name [ in word ... ] do list done # # In the case below, the shell variable 'f' takes on, # successively, the values, xcyc_$tag, xc_$tag, ... # # Note the semi-colon between the iteration values and the # 'do' keyword (a new-line will also work), and that 'done' # terminates the loop. ############################################################ for f in xcyc_$tag xc_$tag yc_$tag dEtot_$tag dJtot_$tag; do ############################################################ # Shell scripts inherit all environment variables, such # as 'GHOSTVIEW'. If 'GHOSTVIEW' has not been set in # the invoking environment, '$GHOSTVIEW' will evaluate to # the null string. ############################################################ gnuplot_it $f $GHOSTVIEW /bin/rm $f done /bin/ls -l *$tag*.ps ############################################################ # Normal exit (success) ############################################################ exit 0