Chinaunix首页 | 论坛 | 博客
  • 博客访问: 383118
  • 博文数量: 146
  • 博客积分: 7142
  • 博客等级: 少将
  • 技术积分: 975
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-03 09:43
文章分类

全部博文(146)

文章存档

2012年(1)

2011年(5)

2010年(24)

2009年(116)

我的朋友

分类:

2009-12-23 23:07:23

################################Start
Script#######################################
1 #!/bin/bash
2 # str-test.sh: 测试null 字符串和非引用字符串,
3 #+ but not strings and sealing wax, not to mention cabbages and kings . . .
4 #+ 上边这句没看懂
5 # Using if [ ... ]
6
7
8 # 如果一个字符串没被初始化,那么它就没有定义的值
9 # 这种状态叫做"null"(与zero 不同)
10
11 if [ -n $string1 ] # $string1 没被声明和初始化
12 then
13 echo "String \"string1\" is not null."
14 else
15 echo "String \"string1\" is null."
16 fi
17 # 错误的结果.
18 # 显示$string1 为非空,虽然他没被初始化.19
20
21 echo
22
23
24 # 让我们再试一下.
25
26 if [ -n "$string1" ] # 这次$string1 被引用了.
27 then
28 echo "String \"string1\" is not null."
29 else
30 echo "String \"string1\" is null."
31 fi # ""的字符串在[]结构中
32
33
34 echo
35
36
37 if [ $string1 ] # 这次$string1 变成"裸体"的了
38 then
39 echo "String \"string1\" is not null."
40 else
41 echo "String \"string1\" is null."
42 fi
43 # 这工作得很好.
44 # 这个[]test 操作检测string 是否为null.
45 # 然而,使用("$string1")是一种很好的习惯
46 #
47 # As Stephane Chazelas points out,
48 # if [ $string1 ] 有1 个参数 "]"
49 # if [ "$string1" ] 有2 个参数,空的"$string1"和"]"
50
51
52
53 echo
54
55
56
57 string1=initialized
58
59 if [ $string1 ] # 再来,$string1"裸体了"
60 then
61 echo "String \"string1\" is not null."
62 else
63 echo "String \"string1\" is null."
64 fi
65 # 再来,给出了正确的结果.
66 # 不过怎么说("$string1")还是好很多,因为. . .
67
68
69 string1="a = b"
70
71 if [ $string1 ] # 再来,$string1 再次裸体了.
72 then
73 echo "String \"string1\" is not null."
74 else
75 echo "String \"string1\" is null."
76 fi
77 # 非引用的"$string1"现在给出了一个错误的结果!
78
79 exit 0
80 # Thank you, also, Florian Wisser, for the "heads-up".
################################End
Script#########################################
 
阅读(807) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~