今天学习shell傻逼了,有几个地方需要注意
shell 13 问 http://blog.csdn.net/freexploit/article/details/626660
1.
----------------返回结果是a-----------------------
t1=a
t2=b
t3=c
t4=a
if [ ${t1} = ${t2} ]; then
echo ${t2}
elif [ ${t1} = ${t3} ]; then
echo ${t3}
elif [ ${t1} = ${t4} ]; then
echo ${t4}
else
echo "end"
fi
------------------返回结果是c---------------------
t1=a
t2=b
t3=c
t4=a
if [ ${t1} = ${t2} ]; then
echo ${t2}
elif [ ${t1}=${t3} ]; then
echo ${t3}
elif [ ${t1} = ${t4} ]; then
echo ${t4}
else
echo "end"
fi
--------------------------------------
看这个ifelse的shell程序,感觉完全一样,但是其实它们的执行结果是不一样的,其原因是因为这句话的格式不同elif [ ${t1}=${t3} ]; then和elif [ ${t1} = ${t3} ]; then,第一句话的执行是错的,第二个是对的,因为第二个的那个=的两边是有空格的,但第一个没有,换句话说是shell的这个对格式的要求很明确,这个大家要注意了
------------------------------------
另外这种对比是比较不出来的,它的返回结果是end,因为其比较的是t1和t2(换句话说不是它们的值是不是相等,而是其指针是不是同一个----我是这么理解的,不知道对不对)
t1=a
t2=b
t3=c
t4=a
if [ t1 = t2 ]; then
echo ${t2}
elif [ t1 = t3 ]; then
echo ${t3}
elif [ t1 = t4 ]; then
echo ${t4}
else
echo "end"
fi
----------------------------------------------
2.${t1}
在引用t1的值时一定要用${t1},不要用$t1,这是格式的问题,不这样就错了
这是我暂时发现的小问题,有了新问题再补充把