Re: Does execline natively do arithmetic and branching

From: Colin Booth <colin_at_heliocat.net>
Date: Thu, 19 Dec 2019 16:31:01 +0000

On Thu, Dec 19, 2019 at 08:03:17AM -0500, Steve Litt wrote:
> Hi all,
>
> I'm messing around with execline, in the hopes that in long tight loops
> it can be faster than /bin/sh. Now I want to do incrementing and other
> add/subtract. Is there any kind of native way, or do I need to backtick
> dc?
It depends on what you're trying to do. If you're trying to generate an
iterative set to work on (for i in `seq 1 10 ; do ...`) you can do it with
`forbacktickx VAR { seq 1 10 } ... '. However, as mentioned in the
loopwhilex documentation:
    Be careful: execline maintains no state, in particular it uses no
    real variables, and environment will be of no use here since every
    instance of prog... runs as a separate child process.
Which makes doing true incrementers not possible without using the file
syustem (for example, you can't run a program in a loop and feed the
results back into the next execution of that loop without offloading
your computation results to disk).

If instead you're trying to do actual math (`$((N+1))' and the like), no
execline does not support that. At least, not as far as I know. This is
because once a script has gone through the initial parsing and
environmental manipulation it stops being an execlineb script and
instead becomes a whatever-the-next-program-in-the-chain-is script.
Unlike shell there is no overarching program managing everything so you
can't differ higher level processing and data storage once the program
has started.
>
> Second question: Is there a way to find out whether a variable is ten
> or above without using execline's ifthenelse to query the test
> executable?
>
Just like in shell you need to call test (either the builtin or
stand-alone variety). Depending on how you want the program to proceed
if, ifelse, ifte, or ifthenelse are all perfectly valid callers, but
`if [ 10 -lt $VAR ] ; then do thing ; else do other ; fi ...'
is written
`importas VAR VAR ifthenelse { test 10 -lt ${VAR} } { do thing } { do
other } ...' (the importas is, of course, not necessary if you've
imported/defined/whatevered it earlier).

Anyway, the only difference is that execline doesn't have a built-in
mechanism for truth testing but having that somewhere on a system is a
requirement for POSIX so execline itself doesn't need to ship one. Of
course, execline itself doesn't have any builtins per-se (the commands
shipped with execline are stand-alone utilities) so you can't fudge it
like you can with shell. If you don't want to use the GNU or BSD
coreutils, and are allergic to multi-call binaries, s6-test (in
s6-portable-utils) works quiet well and handles all of the POSIX defined
cases.

-- 
Colin Booth
Received on Thu Dec 19 2019 - 16:31:01 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:44:19 UTC