supervision - discussion about system services, daemon supervision, init, runlevel management, and tools such as s6 and runit
 help / color / mirror / Atom feed
From: Colin Booth <colin@heliocat.net>
To: supervision@list.skarnet.org
Subject: Re: Does execline natively do arithmetic and branching
Date: Thu, 19 Dec 2019 16:31:01 +0000	[thread overview]
Message-ID: <20191219163101.GA12551@cathexis.xen.prgmr.com> (raw)
In-Reply-To: <20191219080317.0b93894d@mydesk.domain.cxm>

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


      reply	other threads:[~2019-12-19 16:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-19 13:03 Steve Litt
2019-12-19 16:31 ` Colin Booth [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191219163101.GA12551@cathexis.xen.prgmr.com \
    --to=colin@heliocat.net \
    --cc=supervision@list.skarnet.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).