Re: Conditional define in execline

From: Laurent Bercot <ska-skaware_at_skarnet.org>
Date: Fri, 20 Mar 2015 20:48:25 +0100

On 20/03/2015 20:21, Gorka Lertxundi wrote:
> Is there an elegant way to achieve a conditional define?

  That is the very question that keeps me awake at night and
is singlehandedly responsible for the majority of my headaches.

  I don't think there's an elegant way to do it, and it is, I
believe, the main weakness of the execline design and syntax.
  Results of conditional expressions are stored in the command line or
the environment, and you can transmit neither of those things to a
parent, so you have to work around it somehow.


> More or less this is what I tried, and it works:
>
> export fn "\
> ifelse { test }
> {
> define A 0
> \\$_at_
> }
> define A 1
> \\$_at_"
>
> execlineb -S0 -c ${fn}
> s6-echo ${A}

  Yes, that will work, because you copy all the rest of the script
via $_at_ : you basically fork the whole script into two branches so
you don't have to transmit information backwards. As you said,
that's not elegant.

  A bit less ugly is to perform a non-conditional define by using an
exit code :

foreground { test }
importas -u A ?
do-something-with-${A}

but that only works if A can be expressed as an exit code.
If A cannot be expressed that way, you need to write and
read the intermediate result:

backtick -n A
{
   ifte { s6-echo true } { s6-echo false } test
}
import -u A
do-something-with-${A}

  and I believe that is the least ugly way of accomplishing
conditional transmission of generic information.


  ...


  ... finally, I'm going to show you a way of defining A
directly. You're going to look at it once, and then you're
going to forget I ever mentioned it.

ifthenelse -s { test }
{ define A true }
{ define A false }
do-something-with-${A}

  *puts on dark glasses*
  *flash*

-- 
  Laurent
Received on Fri Mar 20 2015 - 19:48:25 UTC

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