j4ckpug
j4ckpug

Reputation: 13

calculate (substract) two numerary lists stored within $var1 and $var2. storing each result to its own new variable. (shell: /bin/ash; busybox)

I have to lists stored in $var1 (variable) and $var2 (=const.). The values in both $var's are stored space seperated. I'm limited to /bin/sh and I want avoid to store files in /tmp because the data for $var1 is changing every sedond. While the script is refreshing every 3 seconds. Running on nand flash device.

For $var1 we have e. g.:

80000
70000
60000
50000
40000
30000

For $var2 we have e. g.:

6000
5000
4000
3000
2000
1000

Now I want to make a "simple" substraction for each line: $var1(line1) - $var2(line1); e. g. 80000-1234=74000 and so on for each line. I want to store each in its own new variable e. g. $delata{i}.

So the end result look like:

delta_new_1=74000
delta_new_2=65000
delta_new_3=56000
delta_new_4=47000
delta_new_5=38000
delta_new_6=29000

What did I try:

$deltas=$(using grep reading from a file) # static (more or less correction factors)
$data=$(using grep reading some sensors) # variable

var1=$(for i in $deltas; do echo "$i"; done)
var2=$(for i in $data; do cat "$i" 2>/dev/null || echo 0; done)

c=0

while read -r i <&3 && read -r j <&4 ; do

    DELTA=$((j-i))
    eval delta_new_$c="$DELTA"; c=$((c+1));

done 3<"$var2" 4<"$var1"

I'm struggling already to create a linefeed separated list using sed while I'm not even sure if I would need it. I've tried varA=$(sed -i "s/ /\n/g" "$var1") resp. varB=$(sed -i "s/ /\n/g" "$var2"). It did nothing

Without converting (sed lines) I end up with:

-ash: can't open 74000
65000
56000
47000
38000
29000: no such file

The values are substracted. But e. g. echo $delate_new_1 is empty (ofc due to errors before).

Most examples I can find are utilize bash arrays. They are not available on ash. A lot of other tools are not available also. So basically I'm limited to ash.

Upvotes: 0

Views: 24

Answers (1)

j4ckpug
j4ckpug

Reputation: 13

I've solved it. Thx at those for at least reading it. :) The calculation line is adjusted to my needs and could be written probably better.

c=1
set -- $var1
    while [ -n "$1" ]; do
        eval old"$c"="$1"
        c=$((c+1))
        shift
    done

c=1
set -- $var2
    while [ -n "$1" ]; do
        eval delta"$c"="$1"
        c=$((c+1))
        shift
    done

if [ -n "$old1" ] && [ -n "$delta1" ]; then delta_new_1=$(($old1 - $delta1)); else unset $old1 && unset $delta1; fi
if [ -n "$old2" ] && [ -n "$delta2" ]; then delta_new_2=$(($old2 - $delta2)); else unset $old2 && unset $delta2; fi
if [ -n "$old3" ] && [ -n "$delta3" ]; then delta_new_3=$(($old3 - $delta3)); else unset $old3 && unset $delta3; fi
if [ -n "$old4" ] && [ -n "$delta4" ]; then delta_new_4=$(($old4 - $delta4)); else unset $old4 && unset $delta4; fi
if [ -n "$old5" ] && [ -n "$delta5" ]; then delta_new_5=$(($old5 - $delta5)); else unset $old5 && unset $delta5; fi
if [ -n "$old6" ] && [ -n "$delta6" ]; then delta_new_6=$(($old6 - $delta6)); else unset $old6 && unset $delta6; fi
if [ -n "$old7" ] && [ -n "$delta7" ]; then delta_new_7=$(($old7 - $delta7)); else unset $old7 && unset $delta7; fi
if [ -n "$old8" ] && [ -n "$delta8" ]; then delta_new_8=$(($old8 - $delta8)); else unset $old8 && unset $delta8; fi
if [ -n "$old9" ] && [ -n "$delta9" ]; then delta_new_9=$(($old9 - $delta9)); else unset $old9 && unset $delta9; fi
if [ -n "$old10" ] && [ -n "$delta10" ]; then delta_new_10=$(($old10 - $delta10)); else unset $old10 && unset $delta10; fi
if [ -n "$old11" ] && [ -n "$delta11" ]; then delta_new_11=$(($old11 - $delta11)); else unset $old11 && unset $delta11; fi
if [ -n "$old12" ] && [ -n "$delta12" ]; then delta_new_12=$(($old12 - $delta12)); else unset $old12 && unset $delta12; fi
if [ -n "$old13" ] && [ -n "$delta13" ]; then delta_new_13=$(($old13 - $delta13)); else unset $old13 && unset $delta13; fi

Upvotes: 0

Related Questions