在子脚本中导出的变量是不确定的父脚本(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}"
儿童脚本(b.sh
)看起来是这样的:
#!/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
如果我现在运行使用./a.sh
父,输出将是:
res = 5n
result = 4n
所以这里有一些舍入问题。任何人都知道为什么以及如何解决它?
我想你想要做'./b.sh $ var',否则你提供字符串“var”到'b.sh'而不是变量'$ var'。 – fedorqui 2014-09-11 10:45:30
这不是'export'应该做的事情。它将变量传递给子元素,无法在父元素中设置变量。 – Barmar 2014-09-11 10:46:58