summary refs log tree commit diff
path: root/doc/el_semicolon.html
blob: 6519faa0dbd92ede0a4f3c20102726783d82a6bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="en" />
<title>execline: block management</title>
<meta name="Description" content="execline: block management" />
<meta name="Keywords" content="execline block blocks null argument tilda semicolon el_semicolon" />
<!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
</head>
<body>

<p>
<a href="index.html">execline</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> Blocks </h1>

<p>
A command line (and thus an execline script) is one-dimensional. But a
Unix execution flow can be <em>two</em>-dimensional: when two
instructions are sequenced, for instance. In that case, we need a
way to extract <em>two</em> command lines from <em>one</em> argv.
That is precisely what <em>blocks</em> are made for.
</p>

<p>
 execline commands that need more than one linear set of arguments
use blocks. For instance, the
<a href="foreground.html">foreground</a> command needs to spawn a
first process, then execute into a second one. It reads the command
line for the first process from a block, and the command line for the
second process from the rest of the argv. In the following script:
</p>
<pre>
 #!/command/execlineb
 foreground { echo 1 } echo 2
</pre>
<p>
 <tt>echo&nbsp;1</tt> is read from a block and spawned; then
<tt>echo&nbsp;2</tt> is executed.
</p>

<h2> execlineb syntax </h2>

<p>
 In <a href="execlineb.html">execlineb</a> scripts, blocks are
delimited by braces. They can be nested.
</p>

<h2> argv syntax </h2>

<p>
 execlineb reads and parses the script, and converts it into an <em>argv</em>
(a simple Unix command line) with a different syntax for blocks. 
In an argv, blocks are not delimited by braces;
they are made of <em>quoted arguments</em> and terminated by an
empty word (""). A quoted argument begins with a space.
 Nested blocks are represented by arguments being
quoted several times, i.e. having several spaces in front of them;
an empty word inside a block
gets quoted too, i.e. it will be represented as a series of
spaces.
</p>

<p>
 Actually, the block-reading commands know nothing about braces;
they only understand the "quoted arguments + empty word" syntax.
So if you want to use <a href="foreground.html">foreground</a>
from your shell to sequence <tt>echo&nbsp;1</tt> and
<tt>echo&nbsp;2</tt>, you will have to write
</p>

<pre>
 $ foreground ' echo' ' 1' '' echo 2
</pre>

<p>
 You do not really need to quote every argument inside a block in
that simple case. The following command works as well:
</p>

<pre>
 $ foreground echo 1 '' echo 2
</pre>

<p>
 However, this is bad practice, because it leads to a security hole:
commands that perform
<a href="el_substitute.html">substitution</a> inside a block may
produce empty words, which may modify your script's execution flow.
</p>

<pre>
 $ define FOO '' foreground ' echo' ' ${FOO}' ' rm' ' -rf' ' /' '' echo blah
</pre>

<p>
 is safe, whereas
</p>

<pre>
 $ define FOO '' foreground echo '${FOO}' rm -rf / '' echo blah
</pre>

<p>
 has very much unwanted results. (Don't try this at home.)
</p>

<p>
 You can use the <tt>EXECLINE_STRICT</tt> environment variable to
check proper block quoting. If that variable contains <tt>1</tt>,
commands that read blocks will print a warning message every time
they find an unquoted argument inside a block. If that variable
contains <tt>2</tt> or a bigger integer, commands will print an
error message and die on unquoted arguments.
<br /> You can use <a href="execlineb.html">execlineb</a>'s
<tt>-w</tt> or <tt>-W</tt>
switch to set <tt>EXECLINE_STRICT</tt> to <tt>1</tt> or <tt>2</tt>.
</p>

</body>
</html>