comparison operator "= =" versus "-eq" in Linux shell

Source: Internet
Author: User
Tags sorts

In Linux shell programming, it is often used to determine whether a string is equal, the operator that can be used to determine whether a string is equal (equal), ' (not equal To), ' (less than), ' (Less -eq -ne -lt -le than or equal -gt to), ' (greater than) or ' -ge (greater than or equal to), and =,==,!=,<,>.

In the Bash guide, the letter operator and the symbol operator have different arguments in English, and the symbol operator uses a string, and the letter operator is ARG.

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


    • string1==string2


  • string1=string2

  • True if the strings is equal. ' = should is used with the test command for POSIX conformance.


  • string1!=string2

  • True if the strings is not equal.


  • string1<string2

  • True if string1 sorts before string2 lexicographically.


  • string1>string2

  • True if string1 sorts after string2 lexicographically.


  • arg1OParg2

  • OPis one of ', ', ', ', ', ', -eq -ne -lt -le -gt or ' -ge . These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal arg2 to, respectively. Arg1 And may be arg2 positive or negative integers.

In practical programming, when using the letter operator, although the effect is the same as the symbol operator, it produces an error "[[: Arg2:syntax Error:operand Expected (Error token is" arg2 ")".

As the original

[["-eq" "]] && echo" Delete all spaces and comments of specialized file, using with [email protected] Filena Me "&& exit 1

Revision changed to

[["] = = ["]] && echo "Delete all spaces and comments of specialized file, using with [email protected] Filenam E "&& exit 1

will not be prompted again.

Comes with a handy little script that uses grep to remove space and comment (#), which is simple and practical.

#!/bin/bash # Delete all spaces and comments of specialized file, using with [email protected] filename [["] = =" " ] && echo "Delete all spaces and comments of specialized file, using with [email protected] filename" && Exit 1 grep-v \# $ | Grep-v ^$

Add to the operating system:

Cat > Delsc.sh << eof #!/bin/bash # Delete all spaces and comments of specialized file, using with [email PR otected] filename [["\$1"-== "]] && echo" Delete all spaces and comments of specialized file, using with \[ Email protected] 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

Usage:

DELSC filename


This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1542048

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.