Calvin's Tech Space

成于坚忍,毁于浮躁

   :: 首页 :: 联系 :: 聚合  :: 管理
比较两个字符串是否相等的办法是: 
if [ "$test"x = "test"x ]; then
这里的关键有几点:
1 使用单个等号,也可以使用两个等号
2 注意到等号两边各有一个空格:这是unix shell的要求
3 注意到"$test"x最后的x,这是特意安排的,因为当$test为空的时候,上面的表达式就变成了x = testx,显然是不相等的。而如果没有这个x,表达式就会报错:[: =: unary operator expected

以上在android shell下不能运行,android shell不认识。
posted on 2011-04-13 18:04 calvin 阅读(625) 评论(1)  编辑  收藏 所属分类: Linux

Feedback

# re: shell字符串比较 2012-09-13 21:24 rrt
[ccripts]$ unset test


[scripts]$ if [ $test = '111' ] ; then echo 1 ;fi
-bash: [: =: unary operator expected


[scripts]$ if [ "$test" = '111' ] ; then echo 1 ;fi
[chaolm@rrt-dev-centos6_2 scripts]$

加了引号就相当于 "" = '1111',是可以比较的  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: