2

I use brand new usage of 'bash'.

func(){
  echo Dan 38
}
read name age < <(func)
echo "name=$name age=$age"

How to convert these into dash? (In fact it is busybox's shell)

I use following lines to replace read name age < <(func)

func > /tmp/$$
name=`cat /tmp/$$ | awk '{print $1}'`
age=`cat /tmp/$$ | awk '{print $2}'`
rm /tmp/$$

But, I'm wonder is there better solution?

Daniel YC Lin
  • 15,050
  • 18
  • 63
  • 96
  • 2
    `read` is specified by POSIX ([link](http://pubs.opengroup.org/onlinepubs/7908799/xcu/read.html)), so if nothing else, you should at least be able to write `read name age < /tmp/$$`. – ruakh Oct 01 '13 at 07:20

1 Answers1

0

Solution 1, C-style

Modify the function to accept the names (you can think of them as pointers) of the variables where the result is to be written.

func () { eval "$1=Dan $2=38"; }
func name age
echo "name=$name age=$age"

Solution 2, manual split

This is similar to your solution, but has the advantage that it doesn't use files or call to external programs like awk.

func(){ echo Dan 38 }
both=$(func)
name=${both%% *}
age=${both#* }
echo "name=$name age=$age"
Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121
  • Thanks, I use the C-style, I just know eval can simulate C-style pointers. And we can assign multiple variables in one line. – Daniel YC Lin Oct 01 '13 at 07:42