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

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://dgd2010.blog.51cto.com/1539422/1542048

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

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

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

  • string1==string2
  • string1=string2
  • True if the strings are equal. ‘=’ should be used with the test command for POSIX conformance.
  • string1!=string2
  • True if the strings are not equal.
  • string1<string2
  • True ifstring1sorts beforestring2lexicographically.
  • 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

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

修改为


1

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

就不再提示了。

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


1

2

3

4

#!/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 ^$

添加到操作系统中:


1

2

3

4

5

6

7

8

9

10

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

用法:


1

delsc filename

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

时间: 2024-08-03 19:09:42

Linux shell中比较操作符“==”与“-eq”对比的相关文章

一些Linux Shell中的权限相关知识总结

  这篇文章主要介绍了一些Linux Shell中的权限相关知识总结,使Linux入门学习中的基础知识,需要的朋友可以参考下 一个文件一经创建,就具有三种访问方式: 1) 读,可以显示该文件的内容. 2) 写,可以编辑或删除它. 3) 执行,如果该文件是一个s h e l l脚本或程序. 按照所针对的用户,文件的权限可分为三类: 1) 文件属主,创建该文件的用户. 2) 同组用户,拥有该文件的用户组中的任何用户. 3) 其他用户,即不属于拥有该文件的用户组的某一用户 文件的全部信息包括以下: 文

linux shell 中 2>&amp;amp;1的含义_linux shell

linux shell 中"2>&1"的含义 脚本: nohup /mnt/Nand3/H2000G  >/dev/null  2>&1  & 对于& 1 更准确的说应该是文件描述符 1,而1 一般代表的就是STDOUT_FILENO,实际上这个操作就是一个dup2(2)调用.他标准输出到all_result ,然后复制标准输出到文件描述符2(STDERR_FILENO),其后果就是文件描述符1和2指向同一个文件表项,也可以说错误的输出

Linux shell中的竖线(|)——…

原文地址:Linux shell中的竖线(|)--管道符号作者:潇潇 管道符号,是unix一个很强大的功能,符号为一条竖线:"|". 用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为command 2的输入传给command 2,例如: $ls -s|sort -nr (请注意不要复制$符号进去哦) -s 是file size,-n是numeric-sort,-r是reverse,反转 该命令列出当前目录中的文档(含size),

[转载]Linux shell中的竖线(|)——管道符号

原文地址:Linux shell中的竖线(|)--管道符号作者:潇潇 管道符号,是unix一个很强大的功能,符号为一条竖线:"|". 用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为command 2的输入传给command 2,例如: $ls -s|sort -nr (请注意不要复制$符号进去哦) -s 是file size,-n是numeric-sort,-r是reverse,反转 该命令列出当前目录中的文档(含size),

linux shell中的比较符号与特殊符号介绍_linux shell

shell字符串比较.判断是否为数字 二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别. 整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" -ne "$b" ] -gt 大于,如:if [ "$a" -gt "$b" ] -ge 大于等于,如:if [ "$a" -ge "$b"

linux shell中的单引号与双引号的区别(看完就不会有引号的疑问了)(转)

tips: ============================= IFS - LINUX字段分隔符,内部字段分隔符 IFS(Internal Field Seperator)在Linux的shell中预设的分隔符,用来把command line分解成word(字段). IFS可以是White Space(空白键).Tab( 表格键).Enter( 回车键)中的一个或几个. IFS是shell脚本中的一个重要概念,在处理文本数据时,它是相当有用的.内部字段分隔符是用于特定用途的定界符.IFS

Linux shell中的I/O重定向相关(转)

1. 基本概念(这是理解后面的知识的前提,请务必理解)  a. I/O重定向通常与 FD有关,shell的FD通常为10个,即 0-9: b. 常用FD有3个,为0(stdin,标准输入).1(stdout,标准输出).2(stderr,标准错误输出),默认与keyboard.monitor.monitor有关: c. 用 < 来改变读进的数据信道(stdin),使之从指定的档案读进: d. 用 > 来改变送出的数据信道(stdout, stderr),使之输出到指定的档案: e. 0 是 &

linux shell中获取mongodb最大连接数、内存使用情况等

前两天接到了一个新的需求,需要在linux shell脚本中监控到mongodb最大连接数.内存使用情况等. 但是我对于linux shel很不了解,只是会一些简单常用的linux的操作而已,只要一顿狂搜,最终多番尝试下终于获取到这些值. 成功的步骤大致如下: 0. ./mongostat -u admin -p admin --authenticationDatabase admin -n 1 --json >> aaa.txt 把mongodb监控到的一行数据以json格式写入到aaa.t

Linux Shell中的特殊符号和含义简明总结(包含了绝大部份)_linux shell

在Linux Shell中有很多的特殊符号,这对于我们写Shell脚本时要特别留意:一方面要知道这些特殊符号的用法,这些符号用好了可以达到事半功倍的效果:但另一方面要避免这些特殊符号的过度使用而导致脚本难以调试.难以阅读. 这些特殊符号罗列出来大致如下: 复制代码 代码如下: # ; ;; . , / / 'string'| ! $ ${} $? $$ $* "string"* ** ? : ^ $# $@ `command`{} [] [[]] () (()) || &&am