I need to write an sh script for embedded linux, which only has BusyBox (v1.30.1). I can use "sh", "awk", or "sed".
I have a script which receives 2 variables:
columns="usr sys nic idle io irq sirq"
points="2.2 4.5 0.0 93.1 0.0 0.0 0.0"
Both variables can have arbitrary number of measurements (from 1 to ~15).
I need to combine them to get a 3'd variable:
payload="usr=2.2, sys=4.5, nic=0.0, idle=93.1, io=0.0, irq=0.0, sirq=0.0"
Sh doesn't support arrays, so I can't iterate through "columns" and "points". Is there any way to achieve this task inside sh script (ideally) or with external commands available in BusyBox?
Edit: added what I tried as per James Brown request (obviously it didn't work as sh doesn't support arrays):
for index in ${!columns[*]}; do
payload="${payload} ${columns[$index]}=${points[$index]}"
done
Using Busybox awk
:
$ payload=$(busybox awk -v c="$columns" -v p="$points" '
BEGIN {
n=split(c,cc)
m=split(p,pp)
if(m==n)
for(i=1;i<=n;i++)
printf "%s=%s%s",cc[i],pp[i],(i==n?ORS:", ")
}')
$ echo $payload
usr=2.2 sys=4.5 nic=0.0 idle=93.1 io=0.0 irq=0.0 sirq=0.0