21 July 2011

Command Substitution and the Interactive Fish Shell

The Fluidinfo Shell, Fish (documented here) has a gained a few important new features.   In summary, these are
  1. Interactive Shell.   If invoked without any parameters,
    Fish will start an interactive session that allows you to issue a sequences of Fish commands (without prefixing them by fish).
    $ fish
    This is fish version 3.12.
    > whoami
    > ls
    books/              favourite-things    private/            test-fish/
    comment             has-read            rating              to-read
    > show -a 'book:animal farm (george orwell)' alice/rating
    Object with about="book:animal farm (george orwell)":
      alice/rating = 2
    > show -a "`about book 'animal farm' 'george orwell'`" alice/rating
    Object with about="book:animal farm (george orwell)":
      alice/rating = 2
    > ^D
    Points to note include:
    • If the readline library is available to python, it will be used, allowing Emacs key bindings to be used to edit the command line (as well as arrow keys) and saving a command history, which may be navigated with CTRL-P and CTRL-N or up and down arrows.  You can also view the history by typing history or h.
    • The interactive session is exited by typing the end-of-file
      character—CTRL-D on Unix and CTRL-Z on Windows.
    • Command substitution with left quoting (“backticks”) is supported
      as illustraed in the last example of the interactive session shown (explanation below).
    • Single and double quotes may be used interchangeably, and
      are protected by each other.   Quotes and other special characters may be escaped using backslash, which also escapes itself.   So, for example, Alice's Adventures in Wonderland may be passed as a parameter with
      "Alice's Adventures in Wonderland"
      'Alice\'s Adventures in Wonderland'
    • Obviously, on Unix, when using the interactive shell, the
      Unix shell does not process the command line, meaning, among other things, that there is no globbing (wildcard expansion).
  2. Command Substitution.    Unix users will be familiar with
    command substitution, which now comes to Fish.   The way this works in Unix shells is that any part of a command enclosed in left quotes (“backticks”) is evaluated before the enclosing command, and its output replaces it in that enclosing command. This is particularly relevant to the commands that build Fluidinfo about tags—abouttag, amazon and the new normalize command (see below). In Unix shells, it has always been possible to say:
    $ fish show -a "`fish abouttag book 'animal farm' 'george orwell'`" alice/rating
    The shell first evaluates the subcommand, which produces the output book:animal farm (george orwell) and then, after substitution, the shell executes:
    $ fish show -a "book:animal farm (george orwell)" alice/rating
    In Unix, double quotes are weaker than single quotes, and command substitution can still occur inside them, which is exactly what we need in this case. Fish now makes almost identical functionality available in its new interactive mode.    In the interactive shell you can say
    > show -a "`about book 'animal farm' 'george orwell'`" alice/rating
    This command is identical except that the leading fish is no longer needed within the substitution command (though it can be used), just as it not needed in the outer show command.
  3. Command substitution in Shell-Fish.   The online version of
    Fish, Shell-Fish also benefits from the new command substution capabilities, but does not yet include the history feature.   Whether readline-like command line editing is available is a feature of the browser (today).
  4. The new normalize command.   A new normalize command has
    been added to Fish.   This simply performs some standardization on text, consisting primarily of mapping to lower case, removing most punctuation and standardizing whitespace; accents are preserved. If multiple arguments are passed, the components are joined with colons.   For example:
    $ fish normalize 'A *very* strange (and    wonderful) fish'
    a very strange and wonderful fish
    $ fish normalize golfer 'Darren Clarke'
    golfer:darren clarke
Happy fishing!

No comments:

Post a Comment