shell使用eval进行赋值bc计算,bad substitution

开始我认为是这样的:

[root@jiangyi02.sqa.zmf /home/ahao.mah/ALIOS_TEST]
#cat bbb.sh
#!/bin/sh
eval $1_new=123
eval $1_old=100
eval echo \${$1_new}
eval echo "\${$1_new} - \${$1_old}" | bc
aaa=`eval echo "\${$1_new} - \${$1_old}" | bc`
echo $aaa
[root@jiangyi02.sqa.zmf /home/ahao.mah/ALIOS_TEST]
#sh  bbb.sh  aa
123
23
bbb.sh: line 6: ${$1_new} - ${$1_old}: bad substitution

其实,在使用eval的时候,如果使用了 ``需要 使用两个\

#cat  bbb.sh
#!/bin/sh
eval $1_new=123
eval $1_old=100
eval echo \${$1_new}
eval echo "\${$1_new} - \${$1_old}" | bc
aaa=`eval echo "\\${$1_new} - \\${$1_old}" | bc`
echo $aaa
[root@jiangyi02.sqa.zmf /home/ahao.mah/ALIOS_TEST]
#sh  bbb.sh aa
123
23
23

http://www.linuxquestions.org/questions/linux-general-1/why-i-get-bad-substitution-when-using-eval-898198/

posted @ 2017-02-16 10:45  苏小北1024  阅读(759)  评论(0编辑  收藏  举报