Linux Expr Command (EXT) __linux

Source: Internet
Author: User
Expr Command

Use

Evaluate the value of an expression variable.

Grammar

Expr Expression

Description

The expr command reads the expression parameter, evaluates its value, and writes the result to the standard output.

You must apply the following rule to the Expression parameter: separate each item with a space. Put A/(backslash) in front of the shell-specific characters. Enclose the strings that contain spaces and other special characters in quotation marks.

An integer can be preceded by a unary hyphen. Internally, integers are treated as 32-bit, double complementary numbers. Note: The Expr command returns a 0来 indicating a value of 0 instead of an empty string.

The following items describe the operators and keywords for the expression parameter. The character to be escaped is preceded by A/(backslash). These items are listed in ascending order of precedence, and operators with equal precedence are grouped in {} (curly braces):

Expression1 /| Expression2 Returns Expression1 if it is not a null value or a value of 0, return Expression2.
Expression1/& Expression2 Returns Expression1 returns a 0 value if none of the two expressions are null or 0 values.
Expression1{=,/>,/>=,/<,/<=,!=}Expression2 If all two expressions are integers, the result of an integer comparison is returned, otherwise it returns the result of a string comparison.
Expression1{+,-} Expression2 The addition or subtraction of an integer value variable.
Expression1{ (.*/)"`

the hyphen is dropped as long as there is a hyphen in front of the $FLAG shell variable. The colon operator gives a portion of the FLAG variable, which is matched by a subexpression in the middle of the/(and/) character (backslash, open parenthesis and backslash, closing parenthesis). The colon operator gives the number of characters to match if you omit the/(and/) subexpression characters.

If the $FLAG variable is set to-(hyphen), the command displays a syntax error message. This is because the shell replaces the value of the $FLAG variable before running the expr command. The expr command does not know that a hyphen is a value of a variable. It can only understand:

- : -*/(.*/)

And it interprets the first hyphen as a minus operator. To eliminate this problem, use:

flag= ' expr ' x$flag ': x-*/(. * *) "'
You want to useExprCommand inifStatement, enter:
If expr "$ANSWER": "[YY]" >/dev/null

then return ANSWER to start with "Y" or "Y" fi

If the $ANSWER variable starts with Y or y, the then portion of theif statement executes. If the result of a matching success expression is 1, and the expr command returns an export value of 0, the export value is recognized by the if statement as logical value True. If the match fails, the result is 0, and the export value is 1.

Redirects the standard output of the expr command to the /dev/null special file, discarding the result of the expression. If you do not redirect it, the result is written to standard output, usually your workstation monitor. Consider the following expression:

Expr "$STR" = "="

If the $STR variable has a value of = (equal sign), theexpr command sees the expression after the shell finishes processing the command:

= = =

The expr command interprets this as the three equals operator in a row and displays a syntax error message. This can happen once the value of the shell variable is the same as one in the expr operator. The way to avoid this problem is to write the expression:

Expr "x$str" = "x="
To return the length of the $SHELL environment variable/usr/bin/ksh, enter:
Expr length $SHELL

Display the following:

12
To return to the first position in "abcdef" where any one of the characters in the "de" string appears, enter:
Expr Index abcdef de

Display the following:

4
To return any character of the "FD" string to the first occurrence in "abcdef", enter:
Expr Index abcdef FD

Display the following:

4
To return a string of 6 characters in the "Goodnight Ladies" string starting at position 11, enter:
Expr substr "Goodnight Ladies" 11 6

Display the following:

Ladies

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.