Bash函数中的全局变量为空
Bash global variable is empty in a function
我只是想用calc
来计算两个在代码中初始化较高的全局变量,但是当我set -x
进行调试时,其中一个是空的,我没有明白了,怎么了。
在我的 source.sh 顶部,我有这些声明:
userValue=""
newSolde="
currentSolde=""
[...]
// check the format of a float number
// subsitute "," to "." for 'userValue' will be given to calc later
chkValue() {
if [[ "" == +([0-9])?(?(.|,)*([0-9])) ]]
then
userValue=$(echo "" |sed 's/,/\./')
newOp="$newOp $userValue"
return 0
else
echo " Montant invalide"
return 1
fi
}
[...]
这里我调用函数chkValue
:
getOps(){
[[ "$#" -ne 3 ]] && echo -e "missing args" && exit
currentDate=`date +%a%d/%m/%y`
newOp="$currentDate"
# "newOp" is completed by those 3 functions
(chkOperation "" && chkMotif "" && chkValue "") || exit 1
}
calculateNewSolde() {
newSolde=$(calc -p -- "$userValue"+"$currentSolde") // ***** here it is ****
}
[...]
这是我调用 source.sh -c "a string" 58,6
时 set -x
的结果:
+ chkValue 58,6
+ [[ 58,6 == +([0-9])?(?(.|,)*([0-9])) ]]
++ echo 58,6
++ sed 's/,/\./'
+ userValue=58.6 // <--- init there
+ newOp='ven.20/11/15 CREDIT tou hghj 58.6'
+ return 0
+ getCurrentSoldeFrom /iuser/DATABASE/1000/DB_1000
+ database=/iuser/DATABASE/1000/DB_1000
++ grep '^Solde' /iuser/DATABASE/1000/DB_1000
++ awk '{print $NF}'
+ currentSolde=15.6
+ calculateNewSolde
++ calc -p -- +15.6 // <---- ???
+ newSolde=15.6
我真的很想了解这个。
您的问题出在这里:
(chkOperation "" && chkMotif "" && chkValue "") || exit 1
这会导致子 shell,因此值的设置不会传回父进程。您应该使用 {}
重写它,它允许在没有子 shell 的情况下进行分组:
{ chkOperation "" && chkMotif "" && chkValue ""; } || exit 1
已经注意到标准优先规则意味着在这种情况下也不需要使用大括号,因此它简化为:
chkOperation "" && chkMotif "" && chkValue "" || exit 1
我只是想用calc
来计算两个在代码中初始化较高的全局变量,但是当我set -x
进行调试时,其中一个是空的,我没有明白了,怎么了。
在我的 source.sh 顶部,我有这些声明:
userValue=""
newSolde="
currentSolde=""
[...]
// check the format of a float number
// subsitute "," to "." for 'userValue' will be given to calc later
chkValue() {
if [[ "" == +([0-9])?(?(.|,)*([0-9])) ]]
then
userValue=$(echo "" |sed 's/,/\./')
newOp="$newOp $userValue"
return 0
else
echo " Montant invalide"
return 1
fi
}
[...]
这里我调用函数chkValue
:
getOps(){
[[ "$#" -ne 3 ]] && echo -e "missing args" && exit
currentDate=`date +%a%d/%m/%y`
newOp="$currentDate"
# "newOp" is completed by those 3 functions
(chkOperation "" && chkMotif "" && chkValue "") || exit 1
}
calculateNewSolde() {
newSolde=$(calc -p -- "$userValue"+"$currentSolde") // ***** here it is ****
}
[...]
这是我调用 source.sh -c "a string" 58,6
时 set -x
的结果:
+ chkValue 58,6
+ [[ 58,6 == +([0-9])?(?(.|,)*([0-9])) ]]
++ echo 58,6
++ sed 's/,/\./'
+ userValue=58.6 // <--- init there
+ newOp='ven.20/11/15 CREDIT tou hghj 58.6'
+ return 0
+ getCurrentSoldeFrom /iuser/DATABASE/1000/DB_1000
+ database=/iuser/DATABASE/1000/DB_1000
++ grep '^Solde' /iuser/DATABASE/1000/DB_1000
++ awk '{print $NF}'
+ currentSolde=15.6
+ calculateNewSolde
++ calc -p -- +15.6 // <---- ???
+ newSolde=15.6
我真的很想了解这个。
您的问题出在这里:
(chkOperation "" && chkMotif "" && chkValue "") || exit 1
这会导致子 shell,因此值的设置不会传回父进程。您应该使用 {}
重写它,它允许在没有子 shell 的情况下进行分组:
{ chkOperation "" && chkMotif "" && chkValue ""; } || exit 1
已经注意到标准优先规则意味着在这种情况下也不需要使用大括号,因此它简化为:
chkOperation "" && chkMotif "" && chkValue "" || exit 1