path.gex - GrADS Extensions for Pathname Manipulation and Regular Expressions


NAME

path.gex - GrADS Extensions for Pathname Manipulation and Regular Expressions


SYNOPSIS

GrADS Commands:

abspath PATHNAME - Return the absolute pathname

basename PATHNAME - Return filename part of a pathname

cd PATHNAME - Change current directory, same as chdir

chdir PATHNAME - Change current directory, same as cd

dirname PATHNAME - Return directory name part of a pathname

exists PATHNAME - Check whether a pathname exists

getatime PATHNAME - Get last access time of a pathname

getctime PATHNAME - Get metadata change time of a pathname

getmtime PATHNAME - Get modification time of a pathname

glob PATTERN - Return a list of paths matching a pathname pattern

isfile PATHNAME - Test whether a path is a regular file

isdir PATHNAME - Test whether a path is a directory

match PATTERN STRING - Regular expression matching of string

sed PATTERN STRING - SED-like search and replace

wordexp STRING - Perform word expansion like a POSIX shell

GrADS Scripting Language Functions:

abspath(PATHNAME) - Return the absolute pathname

basename(PATHNAME) - Return filename part of a pathname

chdir(PATHNAME) - Change current directory, same as cd

dirname(PATHNAME) - Return directory name part of a pathname

exists(PATHNAME) - Return 1 if pathname exists, 0 otherwise

getatime(PATHNAME) - Get last access time of a pathname

getctime(PATHNAME) - Get metadata change time of a pathname

getmtime(PATHNAME) - Get modification time of a pathname

glob(PATTERN) - Return a list of paths matching a pathname pattern

isfile(PATHNAME) - Return 1 path is a regular file, 0 otherwise

isdir(PATHNAME) - Return 1 path is a directory, 0 otherwise

match(PATTERN,STRING) - Return 1 if PATTERN matches STRING

sed(PATTERN,STRING) - Return result of SED-like search and replace

wordexp(STRING) - Return string with word expansion like a POSIX shell


DESCRIPTION

This library implements GrADS extensions (gex) with commands for pathname manipulation and perl-compatible regular expression matching, search and replace of strings. In addition, it also provides glob and wordexp commands for shell-like file name and variable expansions; wordexp also provides the back-tick operator for executing shell commands and capture its output. These features are available at the GrADS prompt or as GrADS Scription Language functions.


EXAMPLES

Using abspath to get full path name of current directory

   ga-> abspath .
   /Users/adasilva

Notice that abspath also expands symlinks, e.g.,

   ga-> ! ln -s /opt xxx
   ga-> abspath xxx
   /opt

Parsing file and directory names,

   ga-> basename /path/to/somefile.nc4
   somefile.nc4
   ga-> dirname /path/to/somefile.nc4
   /path/to

Changing directories

If you ever tried to use !cd to change directories you realized that it does not work,

   ga-> !pwd
   /Users/adasilva
   ga-> !cd /opt
   ga-> !pwd
   /Users/adasilva

This is because the directory is only changed in the subprocess spawned by the "!" operation; it does not propagate to the environment of of the parent process. The newly introduced chdir command allows you to change the current directory,

   ga-> chdir /opt
   New directory: /opt
   ga-> !pwd
   /opt

Checking whether a file/directory exists

   ga-> exists /usr/bin/tar
   yes
   ga-> exists /usr/bin/!@#$%^^&
   no

Checking whether a pathname is a file or directory

   ga-> isfile /usr/bin
   no
   ga-> isfile /usr/bin/tar
   yes
   ga-> isdir /usr/bin
   yes
   ga-> isdir /usr/bin/tar
   no

Notice these commands can only be used with existing files or directories,

   ga-> isfile /usr/bin/!@#%^&
   *** ERROR *** cannot stat file /usr/bin/!@#%^&

Getting the modification time of a file/directory

   ga-> getmtime /usr/bin/sed
   Fri Oct 25 17:05:00 2013

Likewise, you can query the last accesss and metadata change times:

   ga-> getatime /usr/bin/sed
   Mon May 26 15:12:53 2014
   ga-> getctime /usr/bin/sed
   Fri Oct 25 17:05:00 2013

Getting a list of path names matching a pattern,

   ga-> glob '/opt/X11/*'
   Found 6 matches
   /opt/X11/bin
   /opt/X11/etc
   /opt/X11/include
   /opt/X11/lib
   /opt/X11/share
   /opt/X11/var

Use the isfile, isdir to find out if these are files or directories, e.g.,

   ga-> isdir /opt/X11/include
   yes

Notice that glob does not perform expansion of the shell home diretory character ~,

   ga-> glob ~
   *** ERROR *** cannot find matches for ~

Use wordexp if you would like expansion of this character.

Performing word expansion: files and directories

   ga-> wordexp '/opt/X11/include/*.h'
   Found 5 words
   /opt/X11/include/Xplugin.h
   /opt/X11/include/png.h
   /opt/X11/include/pngconf.h
   /opt/X11/include/pnglibconf.h
   /opt/X11/include/xpyb.h

Unlike glob, wordexp expands ~:

   ga-> wordexp ~
   Found 1 words
   /Users/adasilva

When a match cannot be found, wordexp returns the original pattern,

   ga-> glob ~/!@#$%
   *** ERROR *** cannot find matches for ~/!@#$%
   ga-> wordexp ~/!@#$%
   Found 1 words
   /Users/adasilva/!@#$%

Performing word expansion: environment variables

   ga-> wordexp 'I am $USER and I reside at $HOME'
   Found 8 words
   I
   am
   adasilva
   and
   I
   reside
   at
   /Users/adasilva

Performing word expansion: back-tick operator

   ga-> wordexp "I am `whoami` and I reside at ~"
   Found 8 words
   I
   am
   adasilva
   and
   I
   reside
   at
   /Users/adasilva
   ga-> wordexp "Today is `date`"
   Found 8 words
   Today
   is
   Mon
   May
   26
   21:25:12
   EDT
   2014

Regular expressions: matching

   ga-> match /open/ OpenGrADS
   no
   ga-> match /open/i OpenGrADS
   yes
   ga-> match /^grads/i OpenGrADS
   no
   ga-> match /grads$/i OpenGrADS
   yes

Regular expressions: search and replace

   ga->sed s/^open/Open/ opengrads
   Replaced 1 matches
   Opengrads
   ga-> sed s/GRADS/GrADS/i opengrads
   Replaced 1 matches
   openGrADS

GrADS Scripting Functions

The following script shows an example

    function main()
   * Enable library functions
   * ------------------------
      rc = gsfallow('on')
   * Check if a file
   * ---------------
      if ( isfile('/usr/bin') )
            say '/usr/bin is a file'
      else
            say '/usr/bin is NOT a file'
      endif
   * Check if a directory
   * --------------------
      if ( isdir('/usr/bin') )
            say '/usr/bin is a directory'
      else
            say '/usr/bin a NOT file'
      endif
   * Get list of header files
   * ------------------------
      headers = wordexp('/opt/X11/include/png*.h')
      say 'PNG header files are: ' headers
   * Search and replace
   * ------------------
      new = sed('s/grads/GrADS/ig', 'Opengrads provides GRADS extensions')
      say new

Running this script produces the following output:

   /usr/bin is NOT a file
   /usr/bin is a directory
   PNG header files are: /opt/X11/include/png.h /opt/X11/include/pngconf.h /opt/X11/include/pnglibconf.h
   OpenGrADS provides GrADS extensions


REGULAR EXPRESSIONS

A regular expression (or regex for short) is a sequence of characters that forms a search pattern, primarily used in pattern matching with strings, or in find and replace type of operations. The match and sed functions provided here rely extensively on regular expressions, in particular Perl Compatible Regular Expressions as implmented in the PCRE Library (http://www.pcre.org). If you are not familiar with regular expressions you may want to start with this Quick Tutorial (http://www.regular-expressions.info/quickstart.html), or consult the references under SEE ALSO below for additional information.


COMMANDS PROVIDED

The following commands are loosely based on the C-Shell syntax:

abspath PATHNAME

Return the absolute pathname associated with PATHNAME. It will expand the . and .. directories as well as follow symlinks. However, it will not expand the shell character ~.

basename PATHNAME

Return file name part of PATHNAME. This is a simple string manipulation, there is no need for the pathname involved to exist.

cd PATHNAME

Change current directory, same as chdir. An error is returned if PATHNAME does not exists.

chdir PATHNAME

Change current directory, same as cd.

dirname PATHNAME

Return directory name part of PATHNAME. This is a simple string manipulation, there is no need for the pathname involved to exist.

exists PATHNAME

Check whether PATHNAME exists. The pathname can be a file or a directory.

getatime PATHNAME

Return last access time of PATHNAME.

getctime PATHNAME

Return metadata change time of PATHNAME.

getmtime PATHNAME

Return modification time of PATHNAME.

glob PATTERN

Return a list of files or directories matching a pathname pattern. The PATTERN is a shell-like pattern involving wildcards, etc., not a regular expression.

isfile PATHNAME

Test whether PATHNAME is a regular file. An error is returned if the pathname does not exists.

isdir PATHNAME

Test whether PATHNAME is a directory. An error is returned if the pathname does not exists.

match PATTERN STRING

Check whether the (perl-compatible) regular expression PATTERN matches STRING.

sed PATTERN STRING

Return string with the result of a search and replace operation on STRING expressed in the (perl-compatible) regular expression PATTERN. See EXAMPLES above.

wordexp STRING

Perform word expansion like a POSIX shell. Word expansion means the process of splitting a string into words and substituting for variables, commands, and wildcards just as the shell does. See EXAMPLES above.


GrADS SCRIPT FUNCTIONS PROVIDED

As shown in the SYNOPSIS above, each of the command above are provided as a GrADS script function. To enable these Library Functions be sure to add this to the top of your own script:

          gsfallow('on')


SEE ALSO


AUTHOR

Arlindo da Silva (dasilva@opengrads.org)


COPYRIGHT

Copyright (C) 2014 Arlindo da Silva; All Rights Reserved.

This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 path.gex - GrADS Extensions for Pathname Manipulation and Regular Expressions