proxy代理   soft软件   IT 业界特快   norton 诺顿病毒库   代理列表   search FTP搜索   whois IP地理位置   blog 追求完美  
money理财   life生活   RSS聚合门户   firefox WEB浏览器   免费域名   typeset 假古文   AntiVirus 反病毒   ipcn 站点导航  

« ICP 终于批下来了 | Main | 归整 ipcn ipchina 等域名 整理 DNS Server »

July 15, 2005

bash script 字符串比较容错性问题

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明。
http://windtear.net/archives/2005/07/15/000718.html http://windtear.net/archives/2005/07/15/000718.html

bash script 中

字符串比较是
if [ "$stra" = "$strb" ] 或者 if [ "$stra" == "$strb" ]
!= 不等于

这里会有一个问题就是如果 $stra 包括了回车会造成语法错误
解决办法就是用 [[ ]]  然后做for循环

% a="a
b
c"

% if [[ ! -z $a ]]; then echo $a; fi
a b c

% if [ "a"$a"a" != "aa" ]; then echo $a; fi     
bash: [: too many arguments

% if [[ "a"$a"a" != "aa" ]]; then echo $a; fi
a b c

% if [[ "a"$a"a" != "aa" ]]; then for i in $a;do echo $i;echo -;done; fi
a
-
b
-
c
-

有时往往需要循环嵌套 只要注意选择不通的循环因子就行了
% for a in a b c;do echo $a;for b in 1 2 3;do echo $b;done;done    
a
1
2
3
b
1
2
3
c
1
2
3


《Advanced Bash-Scripting Guide》第52页还给出了几个特殊的示例供理解

[[ $a == z* ]] # 模式匹配 $a 如果开头字母是 z  true
[[ $a == "z*" ]] # $a 等于 z*   true

[ $a == z* ]  #File globbing and word splitting take place.
[ "$a" == "z*" ] # $a 等于 z*  true
本blog WWW

Posted by windtear at July 15, 2005 8:58 PM

Trackback Pings

TrackBack URL for this entry:
http://cgi.windtear.net/cgi-bin/wt-tb.cgi/718

Comments

Post a comment

Remember Me?



我爱网络
2007-12-31 23:51 2007年度 ipcn.org/ipchina.org 总结报告
2007-12-31 00:31 vmware gsx server service
2007-12-31 00:15 squid 对 404 302 缓存的相关问题
2007-12-30 14:24 升级到 MovableType 3.3x 并修改 entry_basename 兼容性
2007-12-30 13:52 [php] realip judge 前面补0的ip格式剔除
2007-12-14 16:34 rm -rf * 系统管理员要排查的定时炸弹
2007-12-13 19:03 山西的宽带运营商太过分了 私自窜改dns解析
2007-12-13 16:35 F5 MIB 再学习 唯一性标记用地址的小bug
2007-12-11 22:15 perl 用Spreadsheet::WriteExcel写了个处理/读写excel的小程序
2007-12-11 13:39 perl 变量转义的问题

本站使用中的任何问题,请与 windtear SPAM windtear SPAM net 联系
Copyright© 1999-2008 Windtear. All rights reserved.
京ICP备05050670号