Usage of parentheses in shell _ MySQL

Source: Internet
Author: User
Tags echo command
I couldn't remember the title of the brackets in shell, so I got such a Inappropriate title.
Here I want to talk about the parentheses, braces, and variables in several shells. The Command usage is as follows:
PHP code:
1. $
2. $ (cmd)
3. () and {}
4. $ {var:-string}, $ {var: + string}, $ {var: = string}, $ {var :? String}
5. $ (exp ))
6. $ (var % pattern), $ (var % pattern), $ (var # pattern), $ (var # pattern)

I couldn't remember the title at all, so I took this inappropriate title.
Here I want to talk about the parentheses, braces, and variables in several shells. The Command usage is as follows:

PHP code:
1. $
2. $ (cmd)
3. () and {}
4. $ {var:-string}, $ {var: + string}, $ {var: = string}, $ {var :? String}
5. $ (exp ))
6. $ (var % pattern), $ (var % pattern), $ (var # pattern), $ (var # pattern)


The statement is as follows:
1. original form of variables in Shell: $
Common variables are $ var, such
PHP code:
$ Var = test
$ Echo $ var
Test

However, when you want to display variable values with random characters (I use AA here), an error will occur, as shown below:
PHP code:
$ Echo $ varAA

$

In this case, we should use the original form of the variable: $, which is to add a braces to limit the scope of the variable name, as shown below:
PHP code:
$ Echo $ AA
TestAA
$

With this feature, we can easily write a program with the suffix modified in batches. I name it mymv. The program is as follows:
PHP code:
#! /Bin/bash

Tail = {GetProperty (Content )}
For filename in 'Ls'
Do
Mv $ filename $ {filename}. $ tail
Done

The program needs to provide a suffix, such as c, to indicate the c program file with a suffix of C, see the following test:
PHP code:
$ Ls
A B c
$ Mymv c
$ Ls
A. c B. c
$

It seems that the program runs well, but this is an incomplete program. There are two issues to note:
A. There are no subdirectories in the directory. if there is A directory, if it is dir, it will be changed to dir. c. This is obviously not what we want. we should correct this program to recognize the directory.
B. It does not help to process program parameters. The program should be friendly enough and should be able to process the program when the user does not specify the suffix. for example, the above will directly add a dot (.) to the file (.), this is obviously not what we want.

Because our purpose is to explain $, this is enough, so the above program will not be modified here.

2. replace $ (cmd) with the command)
Replace $ (cmd) with the 'cmd' symbol (note that this is not a single quotation mark. on an American keyboard,' is the key under ESC ).
PHP code:
$ Ls
A B c
$ Echo $ (ls)
A B c
$ Echo 'Ls'
A B c

Let's analyze the command echo $ (ls) to understand what the so-called command replacement means:
Shell scans the command line once and finds the $ (cmd) structure. then, it executes cmd in $ (cmd) once to get its standard output, then place the output to the $ (ls) position in the original command echo $ (ls), that is, $ (ls) is replaced, and then the echo command is executed.
As follows:
Echo $ (ls) is replaced with echo a B c
Note that the error output of the command in $ (cmd) will not be replaced, but only the standard output will be replaced:
PHP code:
$ Var = $ (cat d) ### the file d does not exist in the current directory
Cat: d: no file or directory
$ Echo $ var

{GetProperty (Content)} nbsp; ### the value of the var variable is empty.

3. a string of command execution () and {}
() And {} both execute a string of commands, but there are some differences:
A, () just re-open A sub-shell for executing A string of commands
B, {} executes a string of commands in the current shell
C, () and {} both place a string of commands in brackets and separate them with a comma (;).
D, () The Last Command does not need a semicolon.
E, {} The Last Command should use a semicolon
There must be a space between the First Command of F, {} and the left parenthesis.
The commands in G, () do not have to have spaces with parentheses
The redirection of a command in the brackets H, () and {} only affects this command, but the redirection outside the brackets affects all the commands in the brackets.

Let's look at several examples:
PHP code:
$ Var = test
$ (Var = notest; echo $ var) ### the variable var value is notest, which is valid in the sub-shell.
Notest
$ Echo $ var ### the value of the parent shell is still test
Test
$ {Var = notest; echo $ var ;}### note that there must be a space between the left brace and var.
Notest
$ Echo $ var ### change the value of the var variable in the parent shell to notest
Notest
${Var1 = test1; var2 = test2; echo $ var1> a; echo $ var2 ;### the output test1 is redirected to file,
Test2 ### while the test2 output is still output to the standard output.
$ Cat
Test1
${Var1 = test1; var2 = test2; echo $ var1; echo $ var2;}> a ### the standard output of the commands in parentheses is redirected to file.
$ Cat
Test1
Test2

4. several special replacement structures: $, $
A, $ and $
If the var variable is empty, replace $ with string in the command line. otherwise, replace $ with the var variable when the var variable is not empty.
For example:
PHP code:
$ Echo newvar

$ Echo $ {newvar:-}
A
$ Echo newvar ### the value of the newvar variable is still null, but $ is replaced with a in the previous command line.

$ Newvar = B
$ Echo $ {newvar:-a }### when the value of the newvar variable is not empty, $ in this command line is replaced with $ newvar, that is, B
B
$

The replacement rule for $ is the same as that for $. The difference is that if var is null, $ is replaced with string, and the string is assigned to the variable var:


PHP code:
$ Echo newvar

$ Echo $ {newvar: =}
A
$ Echo newvar ### the variable newvar is assigned as a, and $ is replaced with
A
$ Echo $ {newvar: = B }### the newvar variable is not empty (its value has been assigned as a), then $ is replaced with the value of newvar (B)
A
$ Echo $ newvar
A

$ Is commonly used to determine whether a variable is assigned a value. if not, assign a default value to it.
For example, set the default editor:
PHP code:
Echo You use editor :$ {EDITOR :=/ bin/vi}

B, $
The replacement rule of $ is opposite to the above, that is, it is replaced with string only when var is not empty. If var is empty, it is not replaced or replaced with the value of var, null. (Because the variable var is empty at this time, the two statements are equivalent)
PHP code:
$ Echo $ newvar
A
$ Echo $ {newvar: + B}
B
$ Echo $ newvar
A
$ Newvar =
$ Echo $ {newvar: + B}

$

C, $
If the var variable is not empty, replace $ with the var value. if the var variable is empty, output the string to the standard error, and exit from the script. We can use this feature to check whether the variable value is set.
PHP code:
$ Newvar =
$ Echo $ {newvar :? Newvar value not Set}
Bash: newvar: the value of newvar is not set.
$ Newvar =
$ Echo $ {newvar :? Newvar value not Set}
A
$


Supplementary extension: In the above five replace structures, string is not necessarily a constant value. it can be the value of another variable or the output of a command.
PHP code:
$ Echo $ {var:-'date '}
January 1, March 6 02:10:39 CST 2005
$ Echo $ {var:-$ (date )}
January 1, March 6 02:11:46 CST 2005
$ A = test
$ Echo $ {var:-$}
Test
$

5. POSIX standard extended computing: $ (exp ))
This computation is an operator that complies with the C language, that is, any operator that complies with C can be used in $ (exp) or even a three-object operator.
Note: this extension is an integer computation and does not support floating point computation. if the expression exp is true, it is 1, and false is 0.
PHP code:
$ Echo $(3 + 2 ))
5
$ Echo $ (3> 2 ))
1
$ Echo $(25 <3? 2: 3 ))
3
$ Echo $ var

$ Echo $ (var = 2 + 3 ))
5
$ Echo $ var
5
$ Echo $ (var ++ ))
5
$ Echo $ var
6
$

Well, the above example is enough, which also shows that this expansion operation is very powerful.

6. replace the following four modes: $, $
The meaning of these four structures is: $ and $ indicate matching from the rightmost (I .e. the end), and $ match from the leftmost (I .e. the beginning. $ And $ are the shortest matches, and $ are the longest matches. The longest and shortest matches can be found only when wildcards are used in pattern. Otherwise, there is no longest and shortest matches.

The pattern in the structure supports wildcards. * indicates zero or multiple arbitrary characters ,? It indicates zero or any character, and [...] indicates matching the characters in brackets, [!...] It indicates that the characters in the brackets are not matched.
PHP code:
$ Var = aabbbccbbdbb
$ Echo $ {var % B}
Aabbbccbbdb
$ Echo $ {var % B}
Aabbbccbbdb
$ Echo $ {var #}
Abbbccbbdbb
$ Echo $ {var #}
Abbbccbbdbb
$ Echo $ {var % * B}
Aabbbccbbdb
$ Echo $ {var % * B}

$ Echo $ {var # *}
Abbbccbbdbb
$ Echo $ {var # *}

$

The above is a simple example of the use of four pattern matching replacement structures

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.