0%

Shell

In general, the Goal shell mode behavior mimics that of bash.

The following documentation describes specific use-cases.

Environment variables

  • • 

    set <var> <value> (space delimited as in all shell mode, no equals)

Quoting

As in bash, an argument with spaces or other special characters can be quoted with " or ', or its characters can be escaped with a backslash: ls "My Documents", ls 'My Documents' and ls My\ Documents are the same. Inside ' quotes, everything is literal; inside " quotes, which are Go strings, \ starts an escape as in Go. Tab completion of file names continues the word in the same way, escaping or quoting the characters that need it.

Input and output redirection

  • • 

    Standard output to a file: >, or >> to append to it; >& and >>& also send the standard error to it.

  • • 

    Standard input from a file: <.

  • • 

    Pipes: | sends the standard output to the standard input of the next command, and |& the standard error too. Commands defined with command can be used anywhere in a pipe.

  • • 

    Output of a command as a Go string: $...$, which can include pipes, as in x := $ls | grep go$.

Control flow

  • • 

    Any error stops the script execution, except for statements wrapped in [ ], indicating an “optional” statement, e.g.:

cd some; [mkdir sub]; cd sub
  • • 

    & at the end of a statement runs in the background (as in bash) – otherwise it waits until it completes before it continues.

  • • 

    jobs, fg, bg, and kill builtin commands function as in usual bash.

Shell functions (aliases)

Use the command keyword to define new functions for Shell mode execution, which can then be used like any other command, for example:

command list {
	ls -la args...
}
cd data
list *.tsv

The command is transpiled into a Go function that takes args ...string arguments. In the command function body, you can use the args... expression to pass all of the args, or args[1] etc to refer to specific positional indexes, as usual.

The command function name is registered so that the standard shell execution code can run the function, passing the args. You can also call it directly from Go code using the standard parentheses expression.

Script Files and Makefile-like functionality

As with most scripting languages, a file of goal code can be made directly executable by appending a “shebang” expression at the start of the file:

#!/usr/bin/env goal

When executed this way, any additional args passed on the command line invocation are available via the goalrun.Args() function, which can be passed to a command as follows:

install {goalrun.Args()}

or by referring to specific arg indexes etc, as in goalrun.Args()[0]. Note that these are not the same as the implicit args for command aliases, which are local function args in effect.

To make a script behave like a standard Makefile, you can define different commands for each of the make commands, and then add the following at the end of the file to use the args to run commands:

goal.RunCommands(goalrun.Args())

See make for an example, in cmd/goal/testdata/make, which can be run for example using:

./make build

Note that there is nothing special about the name make here, so this can be done with any file.

The make package defines a number of useful utility functions that accomplish the standard dependency and file timestamp checking functionality from the standard make command, as in the magefile system. Note that the goal direct shell command syntax makes the resulting make files much closer to a standard bash-like Makefile, while still having all the benefits of Go control and expressions, compared to magefile.

TODO: implement and document above.

Interrupting with Ctrl-C

Ctrl-C (Control+C) stops what is running: a shell command, a copy with scp, and also Go code, such as a loop that runs too long, which stops with an interrupted error at the line that it was running. A Go function that waits, such as time.Sleep, stops when it returns. Ctrl-C at the prompt clears the line, and the shell keeps running: use exit or Ctrl-D to leave it.

SSH connections to remote hosts

Any number of active SSH connections can be maintained and used dynamically within a script, including simple ways of copying data among the different hosts (including the local host). The Go mode execution is always on the local host in one running process, and only the shell commands are executed remotely, enabling a unique ability to easily coordinate and distribute processing and data across various hosts.

Each host maintains its own working directory and environment variables, which can be configured and re-used by default whenever using a given host.

  • • 

    gossh hostname.org [name] establishes a connection, using given optional name to refer to this connection, and makes it the default host (see below). If the name is not provided, a sequential number will be used, starting with 1, with 0 referring always to the local host. The host can be user@hostname.org, and can have a port, as in hostname.org:2222. The connection uses the key ~/.ssh/id_rsa, and the host must be in ~/.ssh/known_hosts (as it is after a first connection with the standard ssh command).

  • • 

    @name then refers to the given host in all subsequent commands, with @0 referring to the local host where the goal script is running.

  • • 

    You can use a variable name for the server, like this (the explicit $ $ shell mode is required because a line starting with { is not recognized as shell code):

server := "@myserver"
${server} ls$

Explicit per-command specification of host

@name cd subdir; ls

Default host

@name // or:
gossh @name

uses the given host for all subsequent commands (unless explicitly specified), until the default is changed. Use @0 or gossh @0 to return to localhost. The builtins that act on the shell itself always run on the local host: gossh, scp, exit, jobs and source.

Interactive commands and shells on a host

Each command on a host runs in a terminal there, which gets the input of the terminal of the shell while the command runs, so interactive programs such as more, less, top and editors work, and Ctrl-C interrupts the command on the host. Tab completion of file names completes the files on the host of the command: @name ls da and ls da with @name as the default host complete the files on @name, as does @name:da in scp and redirections, and @ at the start of a command completes the names of the hosts.

The commands get the environment of an interactive login shell of the user on the host, such as the PATH set in ~/.bashrc, which is read once when the connection is made.

The commands are still run by the shell one at a time. For a full interactive shell on a host, use:

ssh @name

which runs the login shell of the user on that host, on the existing connection, in its current directory, until it ends with exit or Ctrl-D. Other uses of ssh run the standard ssh command.

Redirect input / output among hosts

The output of a remote host command can be sent to a file on the local host:

@name cat hostfile.tsv > @0:localfile.tsv

Note the use of the : colon delimiter after the host name here. Likewise, the output of any command can be sent to a file on a remote host, and >> adds to it:

ls *.tsv > @name:files.txt

The output of any command can also be piped to a remote host as its standard input, and a file without a host is on the host of the command:

ls *.tsv | @name cat > files.txt

scp to copy files easily

The builtin scp function allows easy copying of files across hosts, using the persistent connections established with gossh instead of creating new connections as in the standard scp command.

scp is always run from the local host, even when the default host is a remote one, with a remote host filename specified as @name:remotefile, and a local filename specified as just the filename, or @0:localfile. A relative filename on a remote host is relative to its current directory (see cd above), and @name: is that directory.

scp @name:hostfile.tsv localfile.tsv

As with cp, files are copied into the destination if it is a directory, with their own names, as here into the current local directory:

scp @name:hostfile.tsv .

There can be several files to copy, and file wildcard globbing works as expected, on the local and remote hosts. A destination that ends with a / is a directory, which is made if needed:

scp @name:*.tsv @0:data/
scp file.tsv *.go @name:

Entire directories can be copied, as in cp -a or cp -r (this behavior is automatic and does not require a flag), and files can also be copied between two remote hosts. The files are copied with the SFTP protocol on the existing connection, which works with large files and keeps the permissions of the files.

Close connections

gossh close

Will close all active connections and return the default host to @0. All active connections are also automatically closed when the shell terminates.

Other Utilties

** TODO: need a replacement for findnm – very powerful but garbage..

Rules for Go vs. Shell determination

These are the rules used to determine whether a line is Go vs. Shell (word = IDENT token):

  • • 

    $ at the start: Shell.

  • • 

    Within Shell, {}: Go

  • • 

    Within Go, $ $: Shell

  • • 

    Line starts with go keyword: if no ( ) then Shell, else Go

  • • 

    Line is one word: Shell

  • • 

    Line starts with path expression (e.g., ./myexec) : Shell

  • • 

    Line starts with "string": Shell

  • • 

    Line starts with word word: Shell

  • • 

    Line starts with word {: Shell

  • • 

    Otherwise: Go

TODO: update above

Multiple statements per line

  • • 

    Multiple statements can be combined on one line, separated by ; as in regular Go and shell languages. Critically, the language determination for the first statement determines the language for the remaining statements; you cannot intermix the two on one line, when using ;