Linux shell中比较操作符“==”与“-eq”对比

时间:2014-08-19 16:52:26   收藏:0   阅读:2154

在Linux shell编程中,经常会用到判断字符串是否相等,可用于判断字符串是否相等的操作符有‘-eq’(相等), ‘-ne’(不等于), ‘-lt’(小于), ‘-le’(小于或等于), ‘-gt’(大于)或‘-ge’(大于或等于),以及=,==,!=,<,>。

在bash指南中,字母操作符和符号操作符的两端的参数英语表达式不相同,符号操作符用的是string,字母操作符用的是arg。

# http://www.gnu.org/software/bash/manual/bashref.html



在实际编程中发现,当用字母操作符,虽然效果与符号操作符相同,但会产生一个错误提示“[[: arg2: syntax error: operand expected (error token is "arg2")”。

如原文为

[[ "$1" -eq "" ]] && echo "delete all spaces and comments of specialized file, using with $@ filename" && exit 1

修改为

[[ "$1" == "" ]] && echo "delete all spaces and comments of specialized file, using with $@ filename" && exit 1

就不再提示了。

附带一个实用小脚本,用途:grep掉空格和注释符(#),简单实用。

#!/bin/bash   
# delete all spaces and comments of specialized file, using with $@ filename    
[[ "$1" == "" ] && echo "delete all spaces and comments of specialized file, using with $@ filename" && exit 1    
grep -v \# $1 | grep -v ^$

添加到操作系统中:

cat > delsc.sh << eof   
#!/bin/bash    
# delete all spaces and comments of specialized file, using with $@ filename    
[[ "\$1" -== "" ]] && echo "delete all spaces and comments of specialized file, using with \$@ filename" && exit 1    
grep -v \# \$1 | grep -v ^$    
eof    
chmod +x ./delsc.sh    
\mv delsc.sh /usr/local/bin/delsc    
which delsc    
cat /usr/local/bin/delsc

用法:

delsc filename


本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1542048

Linux shell中比较操作符“==”与“-eq”对比,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!