문제

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?

도움이 되었습니까?

해결책

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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top