Bash:导出未正确将变量传递给父级 [英] Bash: export not passing variables correctly to parent
本文介绍了Bash:导出未正确将变量传递给父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
子脚本中导出的变量未定义父脚本( a.sh
):
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}"
子脚本( b.sh
)看起来像这样:
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
如果我现在使用 ./a.sh
运行父级,输出将是:
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?
推荐答案
要访问 b.sh
中的变量,请使用 source
:
To access variable's in b.sh
use source
instead :
source b.sh var
它应该提供您想要的东西.
It should give what you wanted.
这篇关于Bash:导出未正确将变量传递给父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文