- 论坛徽章:
- 0
|
#!/bin/bash
#省去N行
37
为什么后面两个例子会输出不同的结果,和是否有双引号有什么关系??? 38
38 if [ $string1 ] # 这次, 就一个$string1, 什么都不加.
39 then
40 echo "String \"string1\" is not null."
41 else
42 echo "String \"string1\" is null."
43 fi
44 # 这种情况运行的非常好.
45 # [ ] 测试操作符能够独立检查string是否为null.
46 # 然而, 使用("$string1")是一种非常好的习惯.
47 #
48 # 就像Stephane Chazelas所指出的,
49 # if [ $string1 ] 只有一个参数, "]"
50 # if [ "$string1" ] 有两个参数, 一个是空的"$string1", 另一个是"]"
51
52
53
54 echo
55
56
57
58 string1=initialized
59
60 if [ $string1 ] # 再来, 还是只有$string1, 什么都不加.
61 then
62 echo "String \"string1\" is not null."
63 else
64 echo "String \"string1\" is null."
65 fi
66 # 再来试一下, 给出了正确的结果.
67 # 再强调一下, 使用引用的("$string1")还是更好一些, 原因我们上边已经说过了.
68
69
70 string1="a = b"
71
72 if [ $string1 ] # 再来, 还是只有$string1, 什么都不加.
73 then
74 echo "String \"string1\" is not null."
75 else
76 echo "String \"string1\" is null."
77 fi
78 # 未引用的"$string1", 这回给出了错误的结果!
79
80 exit 0
81 # 也感谢Florian Wisser, 给出了上面这个"足智多谋"的例子. |
|