top banner
General Issues
1: Introduction
2: Simple example
3: Fancy example
4: Running Gri
5: Programming Gri
6: General Issues
7: X-Y Plots
8: Contour Plots
9: Image Plots
10: Examples
11: Handling Data
12: Gri Commands
13: Gri Extras
14: Evolution of Gri
15: Installing Gri
16: Gri Bugs
17: System Tools
18: Acknowledgments
19: License
20: Newsgroup

21: Concept Index
navigate navigate navigate navigate navigate navigate

6.5: Interaction Between Gri and Operating System

6.5.1: Using the OS from within Gri

Gri uses the operating system internally for things like paging through help information. It also gives access to the operating system from within a gri program, with the `system' and `open' commands, and with the back-tic unix notation. (Note On unix systems, `sh' is normally used, to get the C shell, write `system csh ...', etc.) The operating system may be called within Gri commands, using a syntax borrowed from the `Bash' unix shell. After substituting synonyms in the commandline, Gri scans for dollar-parenthesis blocks (e.g. `\$(system-command)', replacing them with the textual result of sending the indicated system-command to the OS. The replacements are done from left to right in the commandline, starting at the deepest nesting level. Often the dollar-parentheis syntax is used in title commands, to indicate the full pathname of the Gri commandfile, e.g.
draw title "\$(pwd)/\.command_file."
In assignment to synonyms, expansion of dollar-parenthesis is not done. Thus the operating system is called twice on the second line below, and not at all on the first line; to see this, run it as `gri -s8 -t'.
\dir = "\$(echo $MY_DIR)"
show "\$(head -1 \dir/MY_FILE)"
Syntax Note Dollar-parenthesis blocks must be prefixed with backslash to avoid confusion with math expressions within strings, for example to avoid breaking `draw label "$(x,y)$" at 3 3 cm'. This is an example of how TeX notation and unix shell notation collide. Example It is a good idea to employ unix environment variables to name directories containing data, so that Gri scripts will work unchanged even if the data are moved (so long as the environment variables are altered), e.g.
# Figure how many lines in a file
\dir ="$(echo DIRECTORY_WHERE_I_STORE_SOLAR_DATA)"
open "\dir/solar_data_file_name`
...
open "\$(echo DIR_ANOTHER)/another_data_set"
Another method is to pass instructions to the operating system with the `system' command. This discards output. Whatever follows the word `system' is first scanned for synonyms (but not rpn expressions or variables); after replacement of any existing synonyms, the line is passed directly to the operating system. Any results are printed on the terminal. Frequently used system commands are `\.awk.' (a builtin synonym naming the awk program on your system), `head', `grep' and `sed'. Examples:
  • Here's how to paste several files together to form a temporary file for plotting. (Notice that a temporary file incorporating the PID of the job is created and later removed.)
    system paste -d" " 1.dat 2.dat 3.dat > tmp.\.pid.
    open tmp.\.pid.
    read columns x y
    close
    system rm tmp.\.pid.
    
  • Here's how to plot each line in a file called `inp' which do have the string `;' at the start of the line.
    system cat inp | grep -v "^;" | cat > tmp.\.pid.
    open tmp.\.pid.
    read columns x y
    system rm tmp.\.pid.
    ...
    
  • Here's how to use `awk' system command to create a tabulated function for plotting.
    system \.awk. \
        'BEGIN { \
           for (x=0; x<1; x+=0.1) {       \
             printf ("%f %f\n", x, sin(x)) \
           }                              \
         }'                               \
        > tmp.\.pid.
    open tmp.\.pid.
    read columns x y
    close
    system rm tmp.\.pid.
    draw curve
    
    This example is more cleanly written using the piping facility of the `open' command (which automatically creates a temporary file, and destroys it when `close' is done):
    open "\.awk. 'BEGIN {\
      for(x=0;x<1;x+=0.1) {\
        print(x,sin(x)) \
      } \
    }'|"
    read columns x y
    close
    draw curve
    
  • Sometimes you need just a single output item from the operating system. In this case, you can store the results from the operating system in a synonym use the `\synonym = system ...' synonym assignment command.
  • A related command is `\synonym = tmpname', which stores in the synonym the name of a temporary file created by the system. The file is created with the `tmpname' system call, so it is guaranteed not to clash with any existing files. Typically, the filename is something like `/usr/tmp/griAAAa1233'. A useful bit of code is
    \file1 = tmpname
    system ... SOME_SYSTEM_COMMANDS ... > \file
    ... use this new file for something ...
    system rm -f \file
    

6.5.2: Using Gri from within the OS

This section only applies to unix systems. Save the folowing into a file called `p' and chmod it to make it executable. It runs Gri on a named file, with the `-yes' flag set so that any `query' commands are automatically answered in the affirmative, and then displays the results in a Ghostscript window. (USAGE: `p cmdfile.gri')
#!/usr/bin/sh
# Run Gri, then plot in gs window
case $# in
1)
        base=`basename $1 .gri | sed -e s/.*,#`
        gri -yes $base.gri && ghostview $base.ps
        ;;
*)
        echo "Proper usage: $0 cmdfile.gri"
        ;;
esac
bottom banner