免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3046 | 回复: 10
打印 上一主题 下一主题

shell 字符相等判断问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-11 15:38 |只看该作者 |倒序浏览
#!/bin/sh
cpasswd=sc
b=x
if [ "$cpasswd"="$b" ]
then
  echo "equal"
fi

为什么我运行后会打印equal呢,明明不等嘛。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2010-12-11 15:49 |只看该作者
if [ $cpasswd == $b ]; then echo "equal"; fi

论坛徽章:
0
3 [报告]
发表于 2010-12-11 15:50 |只看该作者
[root@t153 ~]# if [ $cpasswd == $b ]; then echo "equal"; fi
equal
[root@t153 ~]#

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2010-12-11 15:55 |只看该作者
b=sc
if [ $cpasswd == $b ]; then echo "equal"; fi
equal

b=x
if [ $cpasswd == $b ]; then echo "equal"; fi

论坛徽章:
0
5 [报告]
发表于 2010-12-11 16:08 |只看该作者
root@h1:~# cat test2
#!/bin/sh
cpasswd=sc
b=sc
if [ $cpasswd == $b ]; then echo "equal"; fi
b=x
if [ $cpasswd == $b ]; then echo "equal"; fi
root@h1:~# ./test2
[: 4: ==: unexpected operator
[: 6: ==: unexpected operator
root@h1:~# bash -version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
root@h1:~#

论坛徽章:
0
6 [报告]
发表于 2010-12-11 16:09 |只看该作者
root@h1:~# cat test2
#!/bin/sh
cpasswd=sc
b=sc
if [ $cpasswd==$b ]; then echo "equal"; fi
b=x
if [ $cpasswd==$b ]; then echo "equal"; fi
root@h1:~# ./test2
equal
equal
root@h1:~#

论坛徽章:
0
7 [报告]
发表于 2010-12-11 16:11 |只看该作者
root@h1:~# cat test2
#!/bin/sh
cpasswd=sc
b=sc
if [ "$cpasswd"=="$b" ]; then echo "equal"; fi
b=x
if [ "$cpasswd"=="$b" ]; then echo "equal"; fi
root@h1:~# ./test2
equal
equal
root@h1:~#

论坛徽章:
0
8 [报告]
发表于 2010-12-11 16:16 |只看该作者
root@h1:~# cat test2
#!/bin/sh
cpasswd=sc
b=sc
if [ "$cpasswd"=="$b" ]; then echo "equal"; fi
b=x
if [ "$cpasswd"=="$b" ]; then echo "equal"; fi
root@h1:~# ./test2
equal
equal
root@h1:~# cpasswd=sc
root@h1:~# b=sc
root@h1:~# if [ $cpasswd == $b ]; then echo "equal"; fi
equal
root@h1:~# b=x
root@h1:~# if [ $cpasswd == $b ]; then echo "equal"; fi
root@h1:~#

论坛徽章:
0
9 [报告]
发表于 2010-12-11 16:17 |只看该作者
找到原因了。

root@h1:~# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jun 20  2008 /bin/sh -> dash

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2010-12-12 15:22 |只看该作者
  1. # cpasswd=sc
  2. # b=x
  3. # [ "$cpasswd" == "$b" ] && echo "equal"
  4. #
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP