[Shell Programming Guide] Several syntax usages of () and {} in shell


Several grammatical uses of () and {} in shell

The prototype of the variable in Shell: ${var}

When you want to display the variable value plus random characters (XX), an error will occur.

At this time, the prototype of the variable should be used: ${var}, which means adding a brace to limit the scope of the variable name.

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

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

$(cmd)

echo $(ls) execution process

The shell scans the command line and finds the $(cmd) structure, and executes the cmd in $(cmd) once to obtain its standard output.

Then put this output into the $(ls) position in the original command echo $(ls), that is, replace $(ls), and then execute the echo command

as follows:

echo $(ls) is replaced by echo 1.txt 2.txt

What should be noted here is that the error output of the command in $(cmd) will not be replaced, only the standard output will be replaced.

[root@bogon t]# ls
1.txt  2.txt
[root@bogon t]# echo $(ls)
1.txt 2.txt

[root@bogon t]# var=$(cat 3.txt)
cat: 3.txt: 没有那个文件或目录
[root@bogon t]# echo $var
#$var显然是空的

A series of commands executed () and {}

() and {} both execute a series of commands, but there are differences:

Same point:

  1. () and {} both put a series of commands in parentheses, and the commands are separated by ; signs

difference:

  1. () just reopens a subshell for execution of a series of commands, and {} executes a series of commands in the current shell.
  2. ()The last command does not need a semicolon, {}The last command requires a semicolon.
  3. There does not have to be a space between the first command in () and the left parenthesis, and there must be a space between the first command in {} and the left parenthesis.
  4. The redirection of a command inside the brackets () and {} only affects that command, but the redirection outside the brackets affects all commands in the brackets.
######  在{}中 第一个命令和{之间必须有空格,结束必须有;
######  {}中的修改了$var的值 说明在当前shell执行
[root@bogon t]# var=test
[root@bogon t]# echo $var
test
[root@bogon t]# (var=notest;echo $var)
notest
[root@bogon t]# echo $var
test
[root@bogon t]# { var=notest;echo $var;}
notest
[root@bogon t]# echo $var
notest

Several special replacement structures

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

If the variable var is empty or undefined, use string to replace ${var:-string} in the command line.

Otherwise, if the variable var is not empty, replace ${var:-string} with the value of the variable var.

###### ${var:-string}和${var:=string}
###### 比较 后者发现$var为空时,把string赋值给了var
###### 后者是一种赋值默认值的常见做法
[root@bogon ~]# echo $a

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

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

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

*** ${var:+string}

The rules are completely 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 variable var, that is, a null value.

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

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

[root@bogon ~]#

*** ${var:?string}

Replacement rule: If the variable var is not empty, replace ${var:?string}** with the value of the variable var

If the variable var is empty, output the string to standard error and exit from the script.

You can use this feature to check whether the value of a variable is set

[root@bogon ~]# echo $a

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

$((exp)) POSIX standard extended calculation

This kind of calculation is an operator that conforms to the C language. That is to say, any operator that conforms to C can be used in $((exp)), including the ternary operator.

Note: This extended calculation is an integer calculation and does not support floating point types, strings, etc.

If it is a logical judgment, the expression exp is 1 if it is true and 0 if it is false.

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

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

**

Four pattern matching replacement structures:

Only when wildcards are used in pattern can there be the longest and shortest match, otherwise there is no longest or shortest match.

The pattern in the structure supports wildcards

* represents zero or more arbitrary characters


? represents zero or one arbitrary character


[…] means matching the characters inside the square brackets


[!..] means that the characters inside the square brackets are not matched.

**

**

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

${var%pattern},${var%%pattern} #从右边开始匹配
${var#pattern},${var##pattern} #从左边开始匹配
${var%pattern} ,${var#pattern} #表示最短匹配,匹配到就停止,非贪婪
${var%%pattern},${var##pattern} #是最长匹配

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

Conclusion: Programming, thinking and continuous growth

In the world of programming, every line of code is an expression and every problem is a challenge. Just like the Bash scripts we've explored in this article, simple code often contains a wealth of thinking and problem-solving wisdom. Programming is not only a technical activity, but also an exercise in thinking and a manifestation of human creativity.

As Bjarne Stroustrup said in "The C++ Programming Language": "Programming is the art of understanding."

From a programming perspective, every mistake and every challenge is an opportunity for growth. These "errors" are actually a deep dialogue between us and the computer, with the problem, and even with ourselves. They are not just troubles, but also a revelation, guiding us to optimize our thinking and improve solutions.

I sincerely invite everyone to join this journey of programming and thinking. Whether you are new to programming or an experienced developer, please feel free to share your insights and experiences. Every interaction you have, whether it's likes, comments, shares or follows, is the greatest encouragement and support for me to continue creating and sharing.

Thank you for taking the time to read this article. If you find it valuable, please don't forget to bookmark and share it. We also welcome your valuable suggestions and questions about the blog content, because every question may be the starting point for the next article.

Let us continue to learn and grow on the road of programming together.

"Code is like poetry, wisdom is like light." —— Unknown

Thanks for reading, and I look forward to meeting you again at the intersection of programming and thinking.


Read my CSDN homepage and unlock more exciting content: Bubble’s CSDN homepage
Insert image description here

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/132930647