5. Connections of commands

Connection of commands is the most important task in those of GlueLang. In GlueLang, Haskell like symbols are used for connecting commands. Some types of conditional branching are represented by the combination of the symbols. Keywords like if, then, else are not used since they look like commands.

5.1. Pipeline

The symbol of a pipe is >>= in GlueLang. Though | which is used in conventional shells is easier to be typed than >>=, we thing that it is inconspicuous. Moreover, the role of pipelines on shells is similar to a sequence of functions connected by >>= in Haskell. This is an example.
1
2
3
4
5
6
$ cat pipeline2.glue
import PATH
echo 'abc' >>= rev

$ glue pipeline2.glue
cba

We can give a pipeline some returns as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ cat pipeline2_fold.glue
import PATH

echo 'efg'
>>= rev

echo 'hij' >>=
rev

$ glue pipeline2_fold.glue
gfe
jih

5.2. And (>>)

&& in B shells is represented by >> in GlueLang.

1
2
3
4
5
6
$ cat and.glue
/bin/echo 'a' >> /bin/echo 'b' >> /bin/echo 'c'
$ glue ./and.glue
a
b
c

In the following case, echo 'c' is not executed because false returns an error.

1
2
3
4
5
$ cat and2.glue
import PATH
echo 'a' >> false >> echo 'c'
$ glue and2.glue
a

GlueLang can distinguish command errors and the others. A script stops immediately at an error that is not a command error. The following is an example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ cat and2_error.glue
import PATH

echo 'a' >> aaaa >> echo 'c'
echo 'never come here'
$ glue and2_error.glue
Parse error at line 3, char 13
        line3: echo 'a' >> aaaa >> echo 'c'
                           ^

        Command aaaa not exist
        process_level 0
        exit_status 2
        pid 46505
ERROR: 2

5.3. Or (!>)

|| in B shells are represented by !>. Here is an example.

1
2
3
4
5
6
7
$ cat or.glue
import PATH

false !> echo 'Echo is executed.'
true !> echo 'Echo is not executed.'
$ glue or.glue
Echo is executed.

When !> is combined with >>, some types of conditional branching can be reprsented. We here give three cases as examples.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ cat or2.glue
import PATH

true >> echo 'if' !> echo 'else'    # case 1
false >> echo 'if' !> echo 'else'   # case 2
false >> false !> echo 'else'       # case 3

$ glue or2.glue
if
else
else

In the case 1, echo 'else' is skipped because echo 'if' returns 0. In the case2, echo 'if' is skipped because false returns a command error. The error of false is carried to !> and echo 'else' runs. In the case 3, the second false is not executed and the first false invokes echo 'else' as the case 2.

5.4. Then (?>)

5.4.1. Composition of if then else

When two commands are connected with ?>, the right command runs only when the left command returns 0. When the right command returns any error, the script stops. We can implement “if-then-else” rules by combinations of >>, !> and ?> as the following way.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$ cat if_then_else.glue
import PATH

true ?> echo 'if' !>
true ?> echo 'else' !>
echo 'otherwise'

false ?> echo 'if' !>
true ?> echo 'else' !>
echo 'otherwise'

false ?> echo 'if' !>
false ?> echo 'else' !>
echo 'otherwise'

$ glue if_then_else.glue
if
else
otherwise

The clunky shape of this if-then-else can be a little bit relieved by “do blocks,” which are explained later. We give an example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ glue if_then_else2.glue
if
$ cat if_then_else2.glue
import PATH

true ?> do
  echo 'if'
!> true ?> do
  echo 'else'
!> do
  echo 'otherwise'

$ glue if_then_else2.glue
if

However, do blocks enhance computational costs.

5.4.2. Rule of ?> at errors

When the command at the right side of ?> returns any error, the script stops immediately. In the following example, false at the second if-then connection stops the script.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ cat then_stop.glue
import PATH

false ?> true
echo 'ok'

true ?> false
echo 'do not come here'

$ glue then_stop.glue
ok
Execution error at line 6, char 9
        line6: true ?> false
                       ^

        Error at then part
        process_level 0
        exit_status 8
        pid 51633
ERROR: 8

As shown in the output, the glue command gives 8 as the exit status when it stops by the right side of ?>.