Skip to content

Shell Script Ideas

jbw3 edited this page Dec 30, 2016 · 14 revisions

Syntax Specification

Basic Commands

Spaces separate arguments:

>>> cmd arg1 arg2

Commands end at a newline or semicolon:

>>> cmd1
>>> cmd2; cmd3

A backslash continues a command on the following line:

>>> cmd1 arg1 \
... arg2

Parenthesis can be used to delimit commands:

>>> (cmd1 arg1 arg2)

In the following examples, the return value of cmd2 is the first argument of cmd1:

>>> cmd1 (cmd2)
>>> cmd1 (cmd2 arg1.1 arg1.2) arg2.1

A backslash escapes the following character. In the following example, the command has three arguments: "(", "", and "Hello world"

>>> cmd \( \\ Hello\ world

Comments

Comments start with an octothorpe (#) and continue to the end of the line:

>>> # This is a comment

Variables

Set variables with the equals sign.

>>> n = 2
>>> s = "abc"

Access variables with the dollar sign ($). Is there a way to do this without using the dollar sign?

>>> cmd $n $s

Types

Variables can have one of three different types: integer, float, or string. All types are cast to strings when passed to commands. Should there be a bool type?

Integers are numeric digits. Should the negative sign be an operator instead of part of the literal (like C++)? Should support be added for multiple bases (hex, oct, bin)?

>>> i1 = 2
>>> i2 = -35

Floating-point numbers are numeric digits with decimal points. Should scientific notation be supported?

>>> f1 = 2.34
>>> f2 = 5.0
>>> f3 = 1.2e3

Strings are delimited with double quotes or single quotes. Should values without quotes still be considered strings?

>>> s1 = "abc" # string with double quotes
>>> s2 = 'abc' # string with single quotes
>>> s3 = abc # string with no quotes
>>> s4 = "" # empty string
>>> s5 = "2" # string, not an integer

Return Values

Return values are integers.

>>> rv = cmd arg1 arg2

Keywords

The following are reserved keywords:

  • break
  • continue
  • else
  • for
  • if
  • while

Programs with the same name as keywords can be run by enclosing them in parenthesis:

>>> (if)
>>> (while arg1 arg2)

Parsing

First Pass

Split into commands. Need to handle the following:

  1. Escaped characters (backslash preceding character)
  2. Line continuation (backslash preceding newlines)
  3. Semicolons split lines into commands
  4. Parenthesis delimit commands

TODO

  • Define syntax for stdin, stdout, and stderr. Should redirecting output to multiple destinations be supported?
  • Define syntax for piping output of one command to input of another
  • Define how to pipe stdout/stderr to variable. Options: cmd > $var, var < cmd, cmd | var. There should be a way to differentiate between stdout and stderr
  • Should a script be parsed and executed line-by-line or "compiled" into some type of code object before it is executed (similar to Python)?
Clone this wiki locally