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

简介:

在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 ,如需转载请自行联系原作者
相关文章
|
13天前
|
Shell Linux 程序员
【Linux】Shell 命令以及运行原理
【Linux】Shell 命令以及运行原理
|
1天前
|
存储 Shell Linux
【Linux】编写一个简易的shell
【Linux】编写一个简易的shell
【Linux】编写一个简易的shell
|
14天前
|
Shell Linux
【linux课设】自主实现shell命令行解释器
【linux课设】自主实现shell命令行解释器
|
14天前
|
存储 Unix Linux
linux权限管理以及shell
linux权限管理以及shell
|
15天前
|
运维 Linux Shell
day02-Linux运维-系统介绍与环境搭建_硬件 系统核心 解释器shell 外围操作系统
day02-Linux运维-系统介绍与环境搭建_硬件 系统核心 解释器shell 外围操作系统
|
20天前
|
Shell Linux Perl
Linux|如何允许 awk 使用 Shell 变量
Linux|如何允许 awk 使用 Shell 变量
26 2
|
20天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
18 0
|
20天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
19 1
|
20天前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
78 0
|
20天前
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
52 0