1. 大于小于符号必须转义,否则shell会把他们当作重定向符合而把字符串值当作文件名,如果不注意,可能刀子一个不易察觉的严重问题。
编写代码测试下:
root@wl-MS-7673:~# chmod u+x badtest
root@wl-MS-7673:~# ./badtest
baseball is greater than hockey
root@wl-MS-7673:~# cat -n badtest
1 #!/bin/bash
2 val1=baseball
3 val2=hockey
4
5 if [ $val1 > $val2 ]
6 then
7 echo "$val1 is greater than $val2"
8 else
9 echo "$val1 is less than $val2"
10 fi
11
root@wl-MS-7673:~#
shell把大于号当作了重定向,得到里错误结果,修改大于号转义后测试
root@wl-MS-7673:~#
root@wl-MS-7673:~# gedit badtest
root@wl-MS-7673:~# ./badtest
baseball is less than hockey
root@wl-MS-7673:~# cat badtest
#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
root@wl-MS-7673:~#
转义后结果正确。