在Linux shell编程中,常常会用到判断字符串是否相等,可用于判断字符串是否相等的操做符有‘-eq’(相等), ‘-ne’(不等于), ‘-lt’(小于), ‘-le’(小于或等于), ‘-gt’(大于)或‘-ge’(大于或等于),以及=,==,!=,<,>。html
在bash指南中,字母操做符和符号操做符的两端的参数英语表达式不相同,符号操做符用的是string,字母操做符用的是arg。shell
# http://www.gnu.org/software/bash/manual/bashref.html编程
string1==string2
bash
string1=string2
less
True if the strings are equal. ‘=’ should be used with the test
command for POSIX conformance.ide
string1!=string2
spa
True if the strings are not equal.操作系统
string1<string2
code
True ifstring1sorts beforestring2lexicographically.orm
string1>string2
True ifstring1sorts afterstring2lexicographically.
arg1OParg2
OP
is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true ifarg1is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal toarg2, respectively.Arg1andarg2may be positive or negative integers.
在实际编程中发现,当用字母操做符,虽然效果与符号操做符相同,但会产生一个错误提示“[[: 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