在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

本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1542048,如需转载请自行联系原作者

Linux shell中比较操作符“==”与“-eq”对比相关推荐

  1. linux中eq的作用,Linux shell中比较操作符“==”与“-eq”对比

    在Linux shell编程中,经常会用到判断字符串是否相等,可用于判断字符串是否相等的操作符有'-eq'(相等), '-ne'(不等于), '-lt'(小于), '-le'(小于或等于), '-gt ...

  2. linux bash -eq,Linux shell中比较操作符“==”与“-eq”对比

    在Linux shell编程中,经常会用到判断字符串是否相等,可用于判断字符串是否相等的操作符有'-eq'(相等), '-ne'(不等于), '-lt'(小于), '-le'(小于或等于), '-gt ...

  3. linux shell $0怎么输出,linux shell中$0,$?,$!等的特殊用法

    一.Shell脚本中$0.$?.$!.$$.$*.$#.$@等的意义说明 (1)$$ Shell本身的PID(ProcessID,即脚本运行的当前进程ID号) (2)$! Shell最后运行的后台Pr ...

  4. linux判断目录是否存在命令,linux shell 中判断文件、目录是否存在的方法

    本文主要介绍了linux shell 中判断文件.目录是否存在的方法,分享给大家 -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为 ...

  5. linux if else 格式,linux shell中 if else以及大于、小于、等于逻辑表达式

    在linux shell编程中,大多数情况下,可以使用测试命令来对条件进行测试,这里简单的介绍下,方便需要的朋友 比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示 ...

  6. linux的shell的功能,linux shell中${ }的一些特异功能

    linux shell中${ }的一些特异功能,这部分内容是shell中的比较经典的部分之一,建议认真学习,深刻体会. 假设我们定义一个变量为: file=/dir1/dir2/dir3/my.fil ...

  7. Linux shell 中的数组和关联数组

    写在前面的话 我用开始废柴了,这个又是写给我自己看的了. 普通数组 1.数组的定义 在Linux shell中定义一个普通的数组很简单,首先我们先给数组命名(当然要符合所有的命名规则咯).定义数组的一 ...

  8. Linux Shell中的延时函数

    Linux Shell中的延时函数 在 linux shell 脚本中经常需要做一些延时处理. 所以经常要用到 sleep 或 usleep 函数. 下面来说一下  sleep 和 usleep 的区 ...

  9. linux shell 中的sleep命令

    开始还以为是这样的语法: sleep(1), 后面发现是: linux shell 中的sleep命令 分类: LINUX 在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小 ...

最新文章

  1. js创建对象的几种方法
  2. deepin更新失败_检查更新失败
  3. WebSphere Application Server中manageprofiles的使用
  4. python加go_[Python异步博客开发] 加入Golang, go~!
  5. javascript事件处理程序
  6. IndexError: list index out of range coco数据集转换为voc格式出现的错误
  7. win7 下安装ubuntu14.04 本人实测撰写
  8. Ansible详解(十六)——Ansible配合Redis
  9. neo4j安装与示例
  10. CSDN积分获取规则
  11. 蜂鸟处理器+OV5640摄像头模块开发
  12. excel文件工作表保护没有密码进行撤销
  13. oracle vm 安装win server 2012 错误0x000000C4
  14. video标签 或 微软云 azure-media-player 禁止在iphone safari中默认全屏播放
  15. 记事本 批量新建文件夹
  16. 开发者必看 | DevWeekly 第1期:什么是时间复杂度?
  17. Linux获取最后一个参数(参数统计)
  18. 大数据系列 | 全国职业院校技能大赛大数据应用技术赛项笔记分享-离线抽取模块
  19. 百度红包预热活动被封锁
  20. 【转】Ansible 模块之 lineinfile 详细介绍

热门文章

  1. [转]深一层看Singleton
  2. Java定时器的cron设置详解Quartz
  3. python系统信息_Python获得操作系统信息
  4. S - C语言实验——数组逆序
  5. 华为p20pro投屏到笔记本_新荣耀笔记本与微软系统合作,网友:一碰即传投屏功能还有吗...
  6. centos 日志切割_CentOS Linux使用logrotate分割管理日志
  7. matlab 跳棋,MATLAB画跳棋棋盘
  8. VS修改生成应用图标
  9. mysql半连接_MySQL优化案例:半连接(semi join)优化方式导致的查询性能低下(转载)...
  10. 以下构成python循环结构的方法中正确的是_python教程:python循环结构