今天晚上付出了1个小时的代价去写一个shell程序,很简单if [] ;then xxxxx else xxxx fi
但是付出了血的代价,原因很简单,太相信课本了,特别的大学的课本
在百度文库里搜索到西安理工大学计算机工程学院的shell编程课件,看课件,做的不错
然后就一点点的看,一点点的做,直到碰到前面写的
一直调试,一直不对
还是闷头傻调,没出结果
很纳闷,难道老师写的课件里的程序会是错的吗
一个劲的找到底哪里错误了
最后发现这个老师的课件完全有问题,误导学生了
在shell的条件判断里
#!/bin/sh
echo "input first num:"
read first
echo "input second num:"
read second
if [${first} -gt ${second}];then
echo "$first is greater than $second"
elif [${first} -lt ${second}];then
echo "$first is lessthan $second"
else
echo "$first is greater $second"
fi
其中在if条件里要去变量时应该写成 ${变量名},而不是课件里写的$变量名,后者写的方法,在编译时会出现 变量值:not found
以此引以为戒,告诫自己改变学习方法。
2011-7-23 21:36
纠正自己的错误:
不是别人的问题,应该先从自己身上找原因
#!/bin/sh
echo "input first num:"
read first
echo "input second num:"
read second
if [ ${first} -gt ${second} ];then #这个中括号前后应该有空格 $first这样取变量也是对的
echo "$first is greater than $second"
elif [ ${first} -lt ${second} ];then
echo "$first is lessthan $second"
else
echo "$first is greater $second"
fi