[clug] capturing stdout of shell function without subprocess or tempfile

Kim Holburn kim at holburn.net
Thu Aug 20 03:12:45 MDT 2009


Temporary var?

>    #!/bin/bash
>
>    function func {
>        foo=$(($foo + 1))
>        temp=2
>    }
>
>    foo=1
>

BTW:
> #   tmpfile="${TMP:-/tmp}/${0##*/}-$$"
is bad, better is:
# TMPFILE=`mktemp /tmp/${0##*/}-$$.XXXXXX` || exit 1

>    func
>    bar=$temp
>
>    echo "$foo $bar"



On 2009/Aug/20, at 7:28 AM, Kevin Pulo wrote:

> All this scripting fun reminded me of a problem I had recently and
> couldn't solve.
>
> It's easy (in bash) to feed the contents of a variable to a program
> without using an extra process by using a here-string:
>
>    somecommand <<< "$foobar"
>
> In the bad old days that had to be a full here-document:
>
>    somecommand <<-EOF
>    $foobar
>    EOF
>
> or worse, something stupid like:
>
>    echo "$foobar" | somecommand
>
> or else you had to use a temporary file:
>
>    echo "$foobar" > tmpfile
>    somecommand < tmpfile
>
> Tempfiles are a pain, because then you have to set up traps to get rid
> of them (which still don't help for SIGKILL), where to put them,
> unique names, races, they assume a read-write filesystem, they're
> slower than being in-memory, etc etc.
>
> I want the reverse.  I want to grab the output of a shell function,
> and stick it in a variable.  But, the catch is, I don't want to use
> tempfiles, and I don't want the shell function running in a subprocess
> (because the function has side-effects, ie. it sets important
> variables).
>
> So things like:
>
>    foobar="$(somefunction)"
>    somefunction | read foobar
>    read foobar < <(somefunction)
>
> and so on are no good.
>
> Is it just me, or is this impossible?  Does bash need something like
> an inverse here-string added to it to be able to do this, eg:
>
>    somefunction >>> foobar
>
> ??
>
> To put it another way, if I have something like this:
>
>    #!/bin/bash
>
>    function func {
>        foo=$(($foo + 1))
>        echo 2
>    }
>
>    foo=1
>
>    tmpfile="${TMP:-/tmp}/${0##*/}-$$"
>    func > "$tmpfile"
>    read bar < "$tmpfile"
>    rm -f "$tmpfile"
>
>    # each no good:
>    #bar=$(func)
>    #read bar < <(func)
>    #func | read bar
>
>    bar=$(($bar + 1))
>
>    echo "$foo $bar"
>
> I want to get rid of the tmpfile use and still have it output
> correctly (ie. "2 3").  Wishful thinking?
>
> Kev.
>
> --  
> .----------------------------------------------------------------------.
> | Kevin Pulo                Quidquid latine dictum sit, altum  
> viditur. |
> | kev at pulo.com.au               _ll l_ng__g_e_ _r_ hi__ly  
> p__d_ct__le. |
> | http://www.kev.pulo.com.au/         God casts the die, not the  
> dice. |
> `--------------- Linux: The choice of a GNU generation.  
> ---------------'
> -- 
> linux mailing list
> linux at lists.samba.org
> https://lists.samba.org/mailman/listinfo/linux

-- 
Kim Holburn
IT Network & Security Consultant
Ph: +39 06 855 4294  M: +39 3494957443
mailto:kim at holburn.net  aim://kimholburn
skype://kholburn - PGP Public Key on request








More information about the linux mailing list