Re: Execline Help: Need the Return Value and the Stdout of a Program

From: Alex Raschi <raschi.alex_at_gmail.com>
Date: Tue, 23 Feb 2021 09:38:18 +0100

On Tue, Feb 23, 2021 at 03:11:18AM -0500, Scott Colby wrote:
> Hello,
>
> I am having some difficulty translating the following shell script
> to execline:
>
> #!/usr/bin/env sh
> file_loc=$(xmlstarlet sel -t -v '//File/Path' /etc/some-config.xml 2>&1)
> # if //File/Path is missing from some-config.xml,
> # xmlstarlet will have exited non-zero and we skip this
> if test $? -eq 0
> then
> if ! test -e "$file_loc"
> then
> create-file
> fi
> fi
> exec /usr/bin/s6-svscan /service
>
> Here's what I have so far:
>
> #!/usr/lib/execline/bin/execlineb -P
> foreground {
> if -n {
> backtick -i file_loc {
> fdmove -c 2 1 xmlstarlet sel -t -v //File/Path /etc/some-config.xml
> }
> importas -iu file_loc file_loc test -e $file_loc
> }
> create-file
> }
> /usr/bin/s6-svscan /service
>
> The problem is, `if -n` can't differentiate between the failure of
> xmlstarlet within backtick and the failure of `test -e`. I only
> want create-file to be called if xmlstarlet succeeds and `test -e`
> fails. I've tried various permutations of wrapping backtick and
> importas with other if constructs, but couldn't find one that worked.
> Looking at the other conditional commands, maybe I could take
> advantage of ifthenelse setting $? before continuing the exec chain,
> but I'm wondering if there is a better way.
>
> How can I make such a script using execline?
>
> Thank you,
> Scott
>

Hi!

If i understood correctly, this should do what you want:

#!/usr/lib/execline/bin/execlineb -P
foreground {
  backtick -i file_loc {
    fdmove -c 2 1 xmlstarlet sel -t -v //File/Path /etc/some-config.xml
  }
  importas -iu file_loc file_loc
  if -n { test -e $file_loc }
  create-file
}
/usr/bin/s6-svscan /service

Move the backtick out of the if, if xmlstartlet fails backtick (-i) will
not execute the if (and importas) at all.
Received on Tue Feb 23 2021 - 08:38:18 UTC

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