Shell script eval, ' and $ (), [[and] [, $ (())

Source: Internet
Author: User
Tags echo command

Eval

The role of Eval is to perform command-line processing again, that is, to perform two command-line processing on a single command line.


Syntax: eval cmdline

Eval will scan the back of the CmdLine two times, if CmdLine is a normal command after the first scan, execute this command;
If the cmdline contains an indirect reference to a variable, the semantics of the indirect reference is guaranteed.


Eval echo \$$# get last parameter

such as: Cat Last

Eval echo \$$#

./last one Teo three four

Four

After the first scan, the shell removes the backslash. When the shell scans the row again, it replaces the value of the $4 and executes the echo command


`` (anti-quote) and $()

In the bash shell, $ () and "(anti-quote) are used for command substitution.

$ echo the last Sunday was $ (date-d "last Sunday" +%y-%m-%d)

Get the date of the previous Sunday


The reason for using $ ()

1. "Easy to mix with" (single quote). Sometimes in some strange glyphs, the two symbols are identical (vertical two points).

2. In a multi-level compound substitution, ' extra hops (\ ') are required, while $ () is more intuitive. For example:

Command1 ' Command2 ' Command3 '

The original intention is to Command2 ' Command3 ' in the first to replace the Command3 to Command2 processing, and then pass the results to Command1 ' Command2 ... ' to deal with.

However, the real results are divided into ' command2 ' and ' two segments ' in the command line.

The correct input should be as follows:

Command1 ' command2 \ ' command3\ '

Change to $ () at a glance:

Command1 $ (Command2 $ (COMMAND3))

$ () Insufficient

"Basically, it can be used in all of the Unix shells, if it's written in shell script, the transplant is relatively high. and $ () not every shell can be used.



[[And [

1."[[" is the keyword , and many shells (such as ash BSH) do not support this approach. Ksh, Bash (reportedly introduced support for [[support] from 2.02 onwards.

"[" is a command that is equivalent to test and is supported by most shells. In most modern SH implementations, "[" and "Test" are internal (builtin) commands.

2. Both support arithmetic comparisons and string comparison expressions (the use may be a bit different)

(1) "-gt", "-lt" is an arithmetic comparison operator that is used to compare the size of integers.
(2) ">", "<" is a string comparison operator used to compare the size of a string, using the dictionary order, relating to the current locale.

(3) It is recommended to use the Let, (()) command when using numeric comparisons, otherwise error-prone.


[and [[] different comparisons:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/C6/wKiom1dgybyC7296AABFF_FoaIY286.png "title=" 1.PNG " alt= "Wkiom1dgybyc7296aabff_foaiy286.png"/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/C4/wKioL1dgy9qR7r-kAABCgj3jDnY210.png "title=" 2.PNG " alt= "Wkiol1dgy9qr7r-kaabcgj3jdny210.png"/>

As can be seen from the above two graphs, [the character comparison requires the use of the escape character \, and [[No


3. Logical AND Logical OR

(1) "[": Logic and: "-a"; Logic or: "-o";

(2) "[[": Logic and: "&&"; Logic or: "| |"

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/CA/wKioL1dhFlzBJM25AAAuePJaczw923.png "title=" 3.PNG " alt= "Wkiol1dhflzbjm25aaauepjaczw923.png"/>

4.

1) [...] is a shell command, so the expression in it should be its command-line argument, so the string comparison operator ">" and "<" must be escaped, otherwise it becomes IO redirection;

(2) because "[[" is a keyword, does not do the command line extension, so in [["<" and ">" Do not need to escape, but the relative syntax is slightly stricter. For example in [...] You can enclose the operators in quotation marks, because they are removed when you do a command-line extension, and in [[...] is not allowed to do so;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/CC/wKiom1dhFx7QLhRvAABQkCfcDsI538.png "title=" 4.PNG " alt= "Wkiom1dhfx7qlhrvaabqkcfcdsi538.png"/>


5.[[...] Perform arithmetic expansion, while [...] Don't do

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/CB/wKioL1dhGXWyl8XRAAA_2Y0H3q4458.png "title=" 5.PNG " alt= "Wkiol1dhgxwyl8xraaa_2y0h3q4458.png"/>


$(( ))

Used for integer arithmetic. In bash, the integer operation symbol for $ (()) roughly has these:

+-*/Add, subtract, multiply, divide

% remainder operation

& | ^ ! And, or, XOR, not arithmetic

Example:

$ a=5; b=7; c=2

$ echo $ ((a+b*c))

19

$ echo $ (((a+b)/C))

6

$ echo $ (((a*b)%c))

1

The variable name in $ (()) can also be preceded by a $ symbol: $ (($a + $b * $c)) can also get 19 results.

In addition, $ (()) can also be used for different binary (such as binary, octal, hexadecimal) operations, but the output is only decimal.

echo $ ((16#2A)) result is 42 (16 decimal)


To give a practical example:

The current umask is 022, and the permissions for the new file are:

$ umask 022

$ echo "OBASE=8; $ ((8#666 & (8#777 ^ 8#$ (umask))) "| Bc

644

In fact, it is possible to redefine the value of a variable by simply (()), or as a testing:

a=5; ((a++)) to redefine $a to 6

a=5; ((A –)) a=4

a=5; b=7; ((a < b) will get a return value of 0 (TRUE)

Common test symbols for (()) have the following:

< less than

> Greater than

<= less than or equal to

>= greater than or equal to

= = equals

! = does not equal



Part of the above content reproduced

Finish

This article is from the "Zero Egg" blog, please be sure to keep this source http://lingdandan.blog.51cto.com/10697032/1789612

Shell script eval, ' and $ (), [[and] [, $ (())

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.