<一> eval带例子详解:
a=b;varname=a;cmd=`echo 'if [ -n "\$'${varname}'" ]; then
touch b;else touch a;
fi'`;eval "$cmd" //执行touch b
调试模式看具体执行 //sh -x b
+ a=b //shell执行a=b
+ varname=a //varname=a
++ echo 'if [ -n "$a" ]; then //子shell执行echo命令,因为``的缘故
touch c;else touch a;
fi'
+ cmd='if [ -n "$a" ]; then //echo结果赋给cmd
touch c;else touch a;
fi'
+ eval 'if [ -n "$a" ]; then //eval cmd变量值
touch c;else touch a;
fi'
++ '[' -n b ']' //将$a解析,调用子进程执行判断语句
++ touch c //子进程
<二> if -n 选项奇葩的输出
if [ -n ]; then echo strange; else echo right; fi //输出strange
if [ -n '' ]; then echo strange; else echo right; fi //输出right
a=b;varname=a;cmd=`echo 'if [ -n "\$'${varname}'" ]; then
touch b;else touch a;
fi'`;eval "$cmd" //执行touch b
调试模式看具体执行 //sh -x b
+ a=b //shell执行a=b
+ varname=a //varname=a
++ echo 'if [ -n "$a" ]; then //子shell执行echo命令,因为``的缘故
touch c;else touch a;
fi'
+ cmd='if [ -n "$a" ]; then //echo结果赋给cmd
touch c;else touch a;
fi'
+ eval 'if [ -n "$a" ]; then //eval cmd变量值
touch c;else touch a;
fi'
++ '[' -n b ']' //将$a解析,调用子进程执行判断语句
++ touch c //子进程
<二> if -n 选项奇葩的输出
if [ -n ]; then echo strange; else echo right; fi //输出strange
if [ -n '' ]; then echo strange; else echo right; fi //输出right