Daily Shell(2)

 1 #!/bin/bash
 2 a=4
 3 b=5
 4 
 5 echo
 6 if [ "$a" -ne "$b" ]
 7 then
 8         echo "$a is not equal to $b"
 9         echo "(arithmetic comparison)"
10 fi
11 
12 echo
13 
14 if [ "$a" != "$b" ]
15 then
16         echo "$a is not equal to $b"
17         echo "(string comparison)"
18 fi
19 
20 echo
21 
22 exit 0
 1 #!/bin/bash
 2 # Testing null string and unquoted strings, but not strings
 3 # and sealing wax, not to mention cabbages and kings...
 4 
 5 # If a string has not been initialized, it has no defined value.
 6 # This state is called `null`, not the same as zero!
 7 
 8 if [ -n $string1 ] # string has not been declared or initialized.
 9 then
10         echo "String \"string1\" is not null."
11 else
12         echo "String \"string1\" is null."
13 fi
14 
15 # show $string1 is not null, although it was not initialized.
16 
17 echo
18 
19 # let's try it again
20 if [ -n "$string1" ] # this time, $string1 is quoted!
21 then
22         echo "String \"string1\" is not null."
23 else
24         echo "String \"string1\" is null."
25 fi # Quoted strings within test brackets!
26 
27 echo
28 
29 if [ $string1 ] # this time, $string1 stands naked.
30 then
31         echo "String \"string1\" is not null."
32 else
33         echo "String \"string1\" is null."
34 fi
35 
36 # The [ ... ] test operator alone detects whether the string is null.
37 # However it is good practice to quote it like if [ "$string1" ]
38 # if [ $string1 ] has one arguments, "]"
39 # if [ "$string1" ] has two arguments, the empty "$string1" and "]"
40 
41 echo
42 
43 string1=initialized
44 if [ $string1 ]
45 then
46         echo "String \"string1\" is not null."
47 else
48         echo "String \"string1\" is null."
49 fi # Still, it is better to quote it as "$string1"
50 
51 string1="a = b"
52 if [ $string1 ]
53 then
54         echo "String \"string1\" is not null."
55 else
56         echo "String \"string1\" is null."
57 fi
58 
59 exit 0
posted on 2012-09-19 00:24  coanor 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/coanor/archive/2012/09/19/2692412.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值