Can PipeRead be nested?

Is it possible/safe/sane to call a script with PipeRead that calls another function that uses PipeRead?

For example:

DestroyFunc FlipDesk
AddToFunc FlipDesk
+ I PipeRead "echo SetEnv previous_desk $[desk.n]; if [ $[desk.n] = $0 ]; then echo MyGotoDesk 0 $[previous_desk]; else echo MyGotoDesk 0 $0; fi"

DestroyFunc MyGotoDesk
AddToFunc MyGotoDesk
+ I PipeRead "echo Next (State $[desk.n]) Focus"

The question is: Is it necessary? If you’re already evaluating the
commands at the shell level, calling another subshell to evaluate more
code is probably superfluous.

Think about it. The PipeRead command in “MyGotoDesk” is superfluous in this context. The
evaluation it performs is nothing – the $[desk.n] variable is still
interpolated without it, hence you can just do:

DestroyFunc MyGotoDesk
AddToFunc MyGotoDesk
+ I Next (State $[desk.n]) Focus

… of course, if that “MyGotoDesk” function is only ever called from the
‘FlipDesk’ function then you can just change the FlipDesk function:

DestroyFunc FlipDesk
AddToFunc FlipDesk
+ I PipeRead `echo SetEnv previous_desk $[desk.n]; [ $[desk.n] = $0 ] &&
  echo "Next (...) Focus" || echo "Next (...) Focus"`

Note also that in your original function:

+ I PipeRead "echo SetEnv previous_desk $[desk.n]; if [ $[desk.n] = $0 ]; then echo MyGotoDesk 0 $[previous_desk]; else echo MyGotoDesk 0 $0; fi"

You’re not making use of any of the values past into the function – so I
am unsure what it is you’re trying to achieve.

A little rambly, but does that answer your question?

– Thomas Adam

No, it’s not necessary. I just wanted to know if it was possible.

For some reason I was under the impression that command expansion didn’t work inside the parenthesis of a conditional command and so I was relying on the PipeRead to do the substitution for me. Thanks for showing me that wasn’t necessary.

I make use of $0 in the test and in the else. What the function does is sends you to the target desktop if you’re not already there. If you are already at the target desktop, it sends you to the previous desktop. There’s probably a better way of doing it, but this is what I tried first.

No, but you did show me that my solution could be greatly improved in ways that wouldn’t require nested PipeReads. Thanks for setting me straight on the command expansion within conditionals bit, that was my primary use for PipeReads (and made for an ugly config file).

So, for the record, can PipeReads be nested?

Thanks again for you help!

Look at the command:

GotoDesk prev

Essentially:

PipeRead `[ $[desk.n] == 0 ] || echo "GotoDesk Prev"`

So much for hoping inferences were possible. :stuck_out_tongue:

In the sense that you mean ‘nested’, the answer is yes, of course. Here’s
a contrived example:

DestroyFunc a
AddToFunc a
+ I PipeRead `echo "echo First"; echo b; echo "echo Third"`

DestroyFunc b
AddToFunc b
+ I PipeRead `echo "echo Second"`

a

– Thomas Adam

thomasadam, your answers are great. I went through the man page specifically looking for something like ‘GotoDesk prev’ before writing my own and managed to miss it.

Thanks again for you help!