在子腳本中導出的變量是不確定的父腳本(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