In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use all kinds of parentheses in shell". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use all kinds of parentheses in shell.
What I want to talk about here are the parentheses, curly braces structures and braced variables in shell, and the use of commands as follows:
1. ${var}
2.$ (cmd)
() and {}
4. ${var:-string}, ${var:+string}, ${var:=string}, ${var:?string}
5.$ ((exp))
6.$ (var%pattern), $(var%%pattern), $(var#pattern), $(var##pattern)
It is now broken down as follows:
The prototype of the variable in 1.Shell: ${var}
The common variable form is $var, such as
$var=test
$echo $var
Test
But when you want to display variable values plus random characters (I use AA here), there will be an error, as follows:
$echo $varAA
$
At this point, you should use the original form of the variable: ${var}, that is, add a curly braces to limit the scope of the variable name, as follows
$echo ${var} AA
TestAA
$
With this feature, we can easily write a batch renamed program, I named it mymv, the program is as follows:
#! / bin/bash
Tail=$1
For filename in `ls`
Do
Mv $filename ${filename}. $tail
Done
The program needs to provide a suffix name, such as c, to represent the C program file with the suffix c, see the following test:
$ls
A b c
$mymv c
$ls
A.c b.c c.c
$
It seems that the program works well, but this is an imperfect program, and there are two problems to pay attention to:
A, there are no subdirectories under the directory, if there is a directory, assuming dir, it will also be changed to dir.c, which is obviously not what we want, we should modify this program to recognize the directory.
B, it does not help to deal with the parameters of the program, the program should be friendly enough to handle when the user does not have a suffix, like the above will directly add a dot (.) to the file, which is obviously not what we want.
Because our purpose is to show that ${var}, this is enough, so there will be no further amendments to the above program.
two。 Command to replace $(cmd)
The command replaces $(cmd) and the symbol `cmd` (note that this is not a single quotation mark; on an American keyboard, `is the key under ESC).
$ls
A b c
$echo $(ls)
A b c
$echo `ls`
A b c
Let's analyze the command echo $(ls) to understand what command substitution means:
Shell scans the command line and finds the $(cmd) structure, then executes the cmd in $(cmd) once to get its standard output, and then puts this output to the $(ls) position in the original command echo $(ls), that is, it replaces $(ls), and then executes the echo command.
As follows:
Echo $(ls) has been replaced with echo a b c
Note here that the error output of the command in $(cmd) will not be replaced, only the standard output:
$var=$ (cat d) # File d does not exist in the current directory
Cat: d: there is no such file or directory
$echo $var
$# obviously the value of the var variable is empty
3. A string of command execution () and {}
() and {} are executed on a string of commands, but there are some differences:
A, () just reopen a subshell for a series of commands to execute
B, {} execute a series of commands in the current shell
C, () and {} all put a string of commands in parentheses and are separated by the; sign
D, () the last command does not need a semicolon
E, {} the last command uses 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 parentheses in H, () and {} affects only that command, but the redirection outside parentheses affects all commands in parentheses.
Let's look at a few examples:
$var=test
$(var=notest; echo $var) # variable varvalue is notest, which is valid in the child shell
Notest
$echo $var # the median of the parent shell is still test
Test
${var=notest; echo $var;} # Note that there is a space between the left parenthesis and var
Notest
$echo $var # # the value of the var variable in the parent shell becomes notest
Notest
${var1=test1;var2=test2;echo $var1 > a TracEchEcho $var2;} # output test1 is redirected to file a
Test2 # # while test2 output is still output to standard output.
$cat a
Test1
The standard output of ${var1=test1;var2=test2;echo $var1;echo $var2;} > a # parentheses commands is all redirected to file a
$cat a
Test1
Test2
Here is an example of a step:
(
Echo "1"
Echo "2"
) | awk & apos; {print NR,$0} & apos
4, several special replacement structures: ${var:-string}, ${var:+string}, ${var:=string}, ${var:?string}
${var:-string} and ${var:=string}
If the variable var is empty, replace ${var:-string} with string on the command line. Otherwise, if the variable var is not empty, replace ${var:-string} with the value of the variable var
Such as:
$echo $newvar
$echo ${newvar:-a}
A
The value of the $echo $newvar # variable newvar is still empty, but ${newvar:-a} was replaced with an in the previous command line
$newvar=b
When the value of the $echo ${newvar:-a} # variable newvar is not empty, the ${newvar:-b} in this command line is replaced with $newvar, b
B
$
The replacement rule for ${var:=string} is the same as for ${var:-string}, except that if var is empty, replace ${var:=string} with string and assign string to the variable var:
$echo $newvar
$echo ${newvar:=a}
A
The $echo $newvar # variable newvar is assigned to a, while ${newvar:=a} is replaced with a
A
The $echo ${newvar:=b} # variable newvar is not empty (its value has been assigned to a), then ${newvar:=b} is replaced with the value of newvar (that is, b)
A
$echo $newvar
A
A common use of ${var:=string} is to determine whether a variable is assigned a value, and if not, give it a default value.
For example, set the default editor:
PHP Code:
Echo You use editor: ${EDITOR:=/bin/vi}
BJI ${var:+string}
The replacement rule of ${var:+string} is contrary to the above, that is, string is replaced only if var is not empty, and if var is empty, it is not replaced or replaced with the value of the variable var, that is, null value. (because the variable var is empty at this time, the two statements are equivalent.)
$echo $newvar
A
$echo ${newvar:+b}
B
$echo $newvar
A
$newvar=
$echo ${newvar:+b}
$
CJI ${var:?string}
The replacement rule is: if the variable var is not empty, replace ${var:?string} with the value of the variable var; if the variable var is empty, output string to standard error and exit from the script. We can use this feature to check whether the value of the variable is set.
$newvar=
$echo ${newvar:? The value of newvar is not set}
Bash: newvar: the value of newvar is not set
$newvar=a
$echo ${newvar:? The value of newvar is not set}
A
$
Supplementary extension: in the above five alternative structures, string is not necessarily constant, you can use the value of another variable or the output of a command.
$echo ${var:- `date`}
March 6 02:10:39 CST 2005
$echo ${var:-$ (date)}
March 6 02:11:46 CST 2005
$a=test
$echo ${var:-$a}
Test
$
Extended calculation of 5.POSIX standard: $((exp))
This kind of calculation is a C-compliant operator, which means that any C-compliant operator can be used in $((exp)) or even a trinomial operator.
Note: this extended calculation is integer and does not support floating-point computing. For logical judgment, the expression exp is 1 if it is true and 0 if it is false.
$echo $((332))
five
$echo $(3 > 2)
one
$echo $(25
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.