Shell in (), {} syntax uses several - Summary alone

(), {} Syntax usage for several of the shell

View script syntax for errors:
bash -n modify_suffix.sh
tracking the implementation of
sh -x modify_suffix.sh aaa

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

The prototype 1.Shell variable: $ {var}

But when you want to display the variable value plus random character (I use _AA), an error occurs

Variable one should use the prototype: $ {var}, that is, add a brace to define the scope of the variable name

[root@localhost sh]# aa='ajax'
[root@localhost sh]# echo $aa
ajax
[root@localhost sh]# echo $aa_AA

[root@localhost sh]# echo ${aa}_AA
ajax_AA

Bulk edit a file directory name

[root@localhost ~]# cat modify_suffix.sh
#!/bin/bash
dst_path=$1
for file in `ls $dst_path`
do
        if [ -d $1/$file ]
                 then echo `$0 $1/$file`
        elif [ -f $1/$file ]
                then    mv $1/$file $1/${file}._mod
        else
            echo $1/${file} is unknow file type
        fi

done;
./modify_suffix.sh  ./f
将 ./f 下的所有文件文件名添加了.mod

Demonstrate the use of $ {} brackets number of special characters% and #

[root@localhost ~]# file="modify_suffix.sh.tar.gz"
[root@localhost ~]# echo "${file%%.*}"
modify_suffix
[root@localhost ~]# echo "${file%.*}"
modify_suffix.sh.tar
[root@localhost ~]# echo "${file#*.}"
sh.tar.gz
[root@localhost ~]# echo "${file##*.}"
gz

2. $ (Cmd) variable call

[root@localhost t]# ls
1.txt  2.txt
[root@localhost t]# echo $(ls)
1.txt 2.txt
echo $(ls) 执行过程
shell扫描一遍命令行,发现了$(cmd)结构,便将$(cmd)中的cmd执行一次,得到其标准输出,
再将此输出放到原来命令 echo $(ls)中的 $(ls)位置,即替换了$(ls),再执行echo命令
如下:
echo $(ls)被替换成了echo 1.txt 2.txt
这里要注意的是$(cmd)中的命令的错误输出是不会被替换的,替换的只是标准输出
[root@localhost t]# var=$(cat 3.txt)
cat: 3.txt: 没有那个文件或目录
[root@localhost t]# echo $var

$var显然是空的

3. The string of command ()} and {

(), And {} is a string of commands for execution, but differ:
similarities:
() {} and the series of commands are placed in brackets, and with between the command; spaced No.

Differences:
() just a bunch of command to re-open a sub-shell for execution, {} a string of commands in the current shell execution
() command can not last a semicolon, {} the last command to use a semicolon
() in there must be a space left between the first command and the first command in parentheses and brackets do not have space left, the {}
() {} and a redirect command in brackets only affects the command, but redirect outside the parentheses is affecting all the commands in brackets

[root@localhost t]# var=test
[root@localhost t]# echo $var
test
[root@localhost t]# (var=notest;echo $var)
notest
[root@localhost t]# echo $var
test
[root@localhost t]# { var=notest;echo $var;}
notest
[root@localhost t]# echo $var
notest
[root@localhost t]#

{} In the first command must be a space between the {and end must have;
{} is the modified value $ var shell described in current

[root@localhost t]# { var1=test1;var2=test2;echo $var1>a;echo $var2;}
test2
[root@localhost t]# cat a
test1
[root@localhost t]# { var1=test1;var2=test2;echo $var1;echo $var2;}>a
[root@localhost t]# cat a
test1
test2
脚本实例
(
    echo "1"
    echo "2"
) | awk '{print NR,$0}'

4. Some specific alternative structure:

${var:-string},${var:+string},${var:=string},${var:?string}

(1) \ ({were: -string和} \) {var: = String}

If the variable var is empty or undefined, the command line with the string used to replace \ ({var: -string} ** ** Otherwise, when the variable var is not empty, then the value of var replaced with \) { var: -string}

[root@localhost ~]# echo $a

[root@localhost ~]# echo ${a:-bcc}
bcc
[root@localhost ~]# echo $a

[root@localhost ~]# a=ajax
[root@localhost ~]# echo ${a:-bcc}
ajax
[root@localhost ~]# unset a
[root@localhost ~]# echo $a

[root@localhost ~]# echo ${a:=bbc}
bbc
[root@localhost ~]# echo $a
bbc

Find

${var:-string}和${var:=string}

When comparing the latter found $ var is empty, the string assigned to the var
common practice which is a default value assigned

(2) $ {var: + string}
rules above and opposite
i.e. if only var is not empty when it is replaced with string, var is not replaced when empty or is replaced by the value of the variable var, i.e., a null value

[root@localhost ~]# a=ajax
[root@localhost ~]# echo $a
ajax
[root@localhost ~]# echo ${a:+bbc}
bbc
[root@localhost ~]# echo $a
ajax
[root@localhost ~]# unset a
[root@localhost ~]# echo $a

[root@localhost ~]# echo ${a:+bbc}

[root@localhost ~]# 

. (. 3) \ ({var:? String} ** ** replace rule: If the variable var is not empty, then the value of var replaced with \) {var:? String}
If the variable var is empty, put string output to standard error, and exit from the script.
This feature can be used to check if the value of the variable

[root@localhost ~]# echo $a

[root@localhost ~]# echo ${a:?bbc}
-bash: a: bbc
[root@localhost ~]# a=ajax
[root@localhost ~]# echo ${a:?bbc}
ajax
[root@localhost ~]# a=ajax
[root@localhost ~]# echo ${a:-`date`}
ajax
[root@localhost ~]# unset a
[root@localhost ~]# echo ${a:-`date`}
2017年 02月 21日 星期二 10:13:46 CST
[root@localhost ~]# echo ${a:-$(date)}
2017年 02月 21日 星期二 10:13:59 CST
[root@localhost ~]# b=bbc
[root@localhost ~]# echo ${a:-$b}
bbc

(. 4). \ (((Exp)) extended computing the POSIX standard ** This calculation is consistent with the C language operators, that is to say they meet operator C are available in \) ((exp)) , including ternary operator
Note: this is an extended computing integer calculations, support for floating point and string, etc.
if the logic, the expression exp is true, or false or 0

[root@localhost ~]# echo $(3+2)
-bash: 3+2: 未找到命令

[root@localhost ~]# echo $((3+2))
5
[root@localhost ~]# echo $((3.5+2))
-bash: 3.5+2: 语法错误: 无效的算术运算符 (错误符号是 ".5+2")
[root@localhost ~]# echo $((3>2))
1
[root@localhost ~]# echo $((3>2?'a':'b'))
-bash: 3>2?'a':'b': 语法错误: 期待操作数 (错误符号是 "'a':'b'")
[root@localhost ~]# echo $((3>2?a:b))
0
[root@localhost ~]# echo $((a=3+2))
5
[root@localhost ~]# echo $((a++))
5
[root@localhost ~]# echo $a
6

(5) four kinds of pattern matching replacement structure:

${var%pattern}
${var%%pattern}
${var#pattern}
${var##pattern}

${var%pattern},${var%%pattern} 从右边向左开始匹配,如果正则表达式匹配到就删除
${var#pattern},${var##pattern} 从左边向右开始匹配,如果正则表达式匹配到就删除
${var%pattern} ,${var#pattern} 表示最短匹配,匹配到就停止,非贪婪,如果正则表达式匹配到就删除
${var%%pattern},${var##pattern} 是最长匹配,如果正则表达式匹配到就删除

Only use wildcards in the pattern in the longest match in order to have the shortest, otherwise no points longest shortest match of the
structure pattern wildcard
*** zero or more of any character
? Zero or an arbitrary character
[. ..] means match the characters in brackets
[! ...] in brackets represent the characters do not match **

[root@localhost ~]# f=a.tar.gz
[root@localhost ~]# echo ${f##*.}
gz
[root@localhost ~]# echo ${f%%.*}
a
[root@localhost ~]# var=abcdccbbdaa
[root@localhost ~]# echo ${var%%d*}
abc
[root@localhost ~]# echo ${var%d*}
abcdccbb
[root@localhost ~]# echo ${var#*d}
ccbbdaa
[root@localhost ~]# echo ${var##*d}
aa
#发现输出的内容是var去掉pattern的那部分字符串的值

Reference: https: //www.cnblogs.com/HKUI/p/6423918.html

Guess you like

Origin www.cnblogs.com/passzhang/p/12455223.html