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.
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.