execline
Software
www.skarnet.org
You can apply 3 kinds of transformations to a value which is to be substituted for a variable: crunching, chomping and splitting. They always occur in that order.
The transformations work around delimitors. Delimitors are the semantic bounds of the "words" in your value. You can use any character (except the null character, which you cannot use in execline scripts) as a delimitor, by giving a string consisting of all the delimitors you want as the argument to the -d option used by substitution commands. By default, the string " \n\r\t" is used, which means that the default delimitors are spaces, newlines, carriage returns and tabs.
You can ask the substitution command to merge sets of consecutive delimitors into a single delimitor. For instance, to replace three consecutive spaces, or a space and 4 tab characters, by a single space. This is called crunching, and it is done by giving the -C switch to the substitution command. The remaining delimitor will always be the first in the sequence.
Crunching is mainly useful when also splitting.
Warning: crunching was turned on by default for split values in all execline versions up to execline-1.05. This is not the case anymore; if you want crunching along with splitting, you have to specify it explicitly. The -c option, which was used to disable crunching, is still supported.
Sometimes you don't want the last delimitor in a value. For instance, when using the backtick command, you most probably don't want a terminating newline as part of the value, even if the forked command outputs such a newline.
Chomping deletes the last character of a value if it is a delimitor. It can be requested by giving the -n switch to the substitution command. Note that chomping always happens after crunching.
In a shell, when you write
$ A='foo bar' ; echo $A
the echo command is given two arguments, foo and bar. The $A value has been split, and the space between foo and bar acted as a delimitor.
If you want to avoid splitting, you must write something like
$ A='foo bar' ; echo "$A"
The doublequotes "protect" the spaces. Unfortunately, it's easy to forget them and perform unwanted splits during script execution - countless bugs happen because of the shell's splitting behaviour.
execline provides a splitting facility, with several advantages over the shell's:
#!/command/execlineb -S1 define -sd" " ARG1S $1 blah $ARG1Sand $ARG1S will be split using the space character as only delimitor.
Netstrings are a way to reliably encode strings containing arbitrary characters. execline takes advantage of this to offer a completely safe splitting mechanism. If a substitution command is given an empty delimitor string (by use of the -d "" option), the splitting function will try to interpret the value as a sequence of netstrings, every netstring representing a word. For instance, in the following command line:
$ define -s -d "" A '1:a,2:bb,0:,7:xyz 123,1: ,' echo '$A'
the echo command will be given five arguments:
However, if the value is not a valid sequence of netstrings, the substitution command will die with an error message.
The dollarat command, for instance, can produce a sequence of netstrings (encoding all the arguments given to an execline script), meant to be decoded by a substitution command with the -d "" option.