shell变量中再包含变量的情况!

来源:互联网 发布:安卓转发小视频源码 编辑:程序博客网 时间:2024/06/05 08:05

#!/bin/bashERR_0="The $1 is exist!"ERR_1="The $1 is not exist!"function test(){ if [ -e $1 ] ; then return 0 else return 1 fi}test $1ret=$?eval result=\$ERR_$retecho $result

执行结果为:

CS> ./test.sh /root/test.sh
The /root/test.sh is not exist!
CS> ./test.sh /root/qx/test.sh
The /root/qx/test.sh is exist!
CS> ./test.sh /root/test.sh
The /root/test.sh is not exist!
CS> ./test.sh /root/qx/test.sh
The /root/qx/test.sh is exist!

通过上述方法,可以将错误码对应的输出来,已达到目的!
0 0