Sunday, 15 September 2013

bash - Calling programs inside a loop -



bash - Calling programs inside a loop -

i have code this

alien=(misterx 3010 6400 9920 12750 3010 6400 9920 12750 9920) opponent=(mistery 3090 6450 10020 12820 10020 12820 3090 6450 9910) (( y=1 ; y<${#alien[@]} ; y++ )); echo "scale=2;(${opponent[$y]}-${alien[$y]})/1000" | bc | sed -e 's/^\./0./g' -e 's/^-\./-0./g' done

with can easly asign output array

(( y=1 ; y<${#alien[@]} ; y++ )); wynik+=($(echo "scale=2;(${opponent[$y]}-${alien[$y]})/1000" | bc | sed -e 's/^\./0./g' -e 's/^-\./-0./g')) done echo ${wynik[@]}

but slow after googling have much faster version like

( echo "scale=2;" (( y=1 ; y<${#alien[@]} ; y++ )); echo "(${opponent[$y]}-${alien[$y]})/1000" done ) | bc | sed -e 's/^\./0./g' -e 's/^-\./-0./g'

partially understand how works, can't find way move output array. way found is

( echo "scale=2;" (( y=1 ; y<${#alien[@]} ; y++ )); echo "(${opponent[$y]}-${alien[$y]})/1000" done ) | bc | sed -e 's/^\./0./g' -e 's/^-\./-0./g' > filename wynik+=($(cat filename)) echo ${wynik[@]}

still faster, @ to the lowest degree ssd, not right, know this. how should this?

wrap look in ( $( ... ) ) , assign variable.

wynik=( $( ( echo "scale=2;" (( y=1 ; y<${#alien[@]} ; y++ )); echo "(${opponent[$y]}-${alien[$y]})/1000" done ) | bc | sed -e 's/^\./0./g' -e 's/^-\./-0./g' ) )

syntax $( ... ) evaluates look , additional pair of parentheses around denote array.

bash

No comments:

Post a Comment