The variables exported in a child-script are undefined the parent-script (a.sh
):
#!/bin/bash
# This is the parent script: a.sh
export var="5e-9"
./b.sh var
export result=$res; # $res is defined and recognized in b.sh
echo "result = ${result}"
The child-script (b.sh
) looks like this:
#!/bin/bash
# This is the child script: b.sh
# This script has to convert exponential notation to SI-notation
var1=$1
value=${!1}
exp=${value#*e}
reduced_val=${value%[eE]*}
if [ $exp -ge -3 ] || [ $exp -lt 0 ]; then SI="m";
elif [ $exp -ge -6 ] || [ $exp -lt -3 ]; then SI="u";
elif[ $exp -ge -9 ] || [ $exp -lt -6 ]; then SI="n";
fi
export res=${reduced_val}${SI}
echo res = $res
If I now run the parent using ./a.sh
, the output will be:
res = 5n
result = 4n
So there is some rounding problem going on here. Anybody any idea why and how to fix it?
To access variable's in b.sh
use source
instead :
source b.sh var
It should give what you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With