Chapters:
1: Introduction
2: Simple example
3: Invocation
4: Finer Control
5: X-Y Plots
6: Contour Plots
7: Image Plots
8: Examples
9: Gri Commands
10: Programming
11: Environment
12: Emacs Mode
13: History
14: Installation
15: Gri Bugs
16: Test Suite
17: Gri in Press
18: Acknowledgments
19: License
Indices:
Concepts
Commands
Variables

## 10.16: Interaction Between Gri and Operating System

- Using OS Inside Gri: Accessing the OS from inside Gri
- Using Gri Inside OS: Tricks for using Gri in unix

### 10.16.1: Using the OS from within Gri

Gri uses the operating system internally for things like paging through help information.

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`', ``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 has 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 the ``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 by using the ``\synonym = system ...`' assignment command.
- subroutine 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`'. In many cases you'll want to remove the file within Gri, once you're done, and that can be done with ``unlink`' (see Unlink) or with a ``system rm -f`' command. A useful bit of code is as follows

      \file = tmpname
      system ... SOME_SYSTEM_COMMANDS ... > \file
      ... use this new file for something ...
      unlink \file

### 10.16.2: Using Gri from within the OS

This section only applies to unix systems.

Save the following into a file called ``p`' and then make it executable using ``chmod`'. 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