当前位置:网站首页>[learn shell programming easily]-4. The difference between single quotation marks and double quotation marks, the operation of integer values, the definition of arrays in the shell and the detailed us
[learn shell programming easily]-4. The difference between single quotation marks and double quotation marks, the operation of integer values, the definition of arrays in the shell and the detailed us
2022-06-22 18:25:00 【FanMY_ seventy-one】
1、 The difference between single quotation mark and double quotation mark
What you see is what you get

2、 Operation of integer value :

[[email protected] ~]# a=10
[[email protected] ~]# b=20
[[email protected] ~]# echo $(($a+$b))
30
[[email protected] ~]# echo $[$a+$b]
30
# Use expr
[[email protected] ~]# expr $a+$b
10+20
[[email protected] ~]# expr $a + $b
30
Floating point , Just use bc
3、shell In the array
Definition : Use parentheses 、 Use a space as a separator in the middle
[[email protected] case]# a=(xx yy zz ff)
[[email protected] case]# echo $a
xx
[[email protected] case]# echo ${a[*]}
xx yy zz ff
[[email protected] case]# echo ${a[@]}
xx yy zz ff
[[email protected] case]# echo ${a[@]:1:3}
yy zz ff
[[email protected] case]# echo ${#a[@]}
4
[[email protected] case]# a[3]='hh'
[[email protected] case]# unset a[2]
[[email protected] case]# echo ${!a[@]}
0 1 3
4、sed
sed Is a non interactive stream editor that supports regular expressions (stream editor).Linux In the system sed The command mainly uses script To process files .

- In memory about sed There are two spaces :patterrn space( Mode space ; Processing workshop ) and hold space( Reserved space ; Temporary warehouse )
- The two spaces can access each other .
- Generally reserved space is not used , Just use pattern space . The schema space has processed one line , It will be cleared and output to the screen . Then proceed to the next line . If you want to keep this line , You can put it in the reserved space .
The concept of pattern space
Mode space :sed The tool reads a line of text from a file and stores it in a buffer , Then the command operates on the contents of the mode space , In the initial state, the schema space has no content , In the process of reading data in each cycle , The schema space is emptied and new content is written ( Add a little here ,sed Command operation is “ Mode space ” Instead of memory , That's why sed The reason why the contents of the original document will not be changed , If you want to change the contents of the original document, you need to add -i Options )
sed Tool execution flow chart

command n Work flow chart of :
Clear the current mode space , Then read in the next line ( If sed Statement encountered n The command will change the normal execution flow ), The execution process is as follows :

4.1、sed Syntax command format and common options
Command format :

Generally, only the first two grammars are used .
Common options :
sed How to find it :
- According to line number
- According to the pattern --》 Regular expressions = character + Special symbols
- According to the string
4.1.1、sed Common editing commands for

4.1.2、sed Of p Order sample
[[email protected] rough_book]# sed -n '3,5p' passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
# -n, Suppress automatic printing of mode space , Only show script The result of the treatment . Without this -n, Will output the entire "passwd" file , And 3 To 5 Guild output twice . because 3 To 5 Line just meets the conditions we set , So in the patter space Stay in , After processing, it will be output again , So it will cause output twice .
# See clearly in this way
[[email protected] rough_book]# cat passwd -n |sed -n '3,5p'
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- sed -n '10p' passwd According to the first 10 That's ok
- sed -n '$p' passwd Show last line
- sed -n '2+2p' passwd Show 2,3,4 That's ok
- sed -n '10,30!p' passwd It shows 10 To 30 The line of
- sed -n '2 ~2p' passwd Show even rows . from 2 The starting step is 2 Began to take .
4.1.3、sed -n '/ Pattern /p' Input file
- The pattern here can be either a regular expression or an exact character
- Mode use / Include , To extend a regular rule, add -r Options
[[email protected] rough_book]# sed -n '/bash$/p' passwd
# Inside this ^ You can also use Using extended regularization
[[email protected] rough_book]# sed -nr '/^fan|^zhao/p' passwd
fan1:x:7800:7803::/home/fan1:/bin/bash
fan2:x:7801:7804::/home/fan2:/bin/bash
zhaoliying1:x:7802:7805::/home/zhaoliying1:/bin/bash
Use escape characters
[[email protected] rough_book]# df -h|sed -n '/\/$/p'
/dev/mapper/centos-root 17G 3.2G 14G 19% /
4.2、sed The difference between single quotation marks and double quotation marks
- You can use... In double quotation marks shell Variables in
[[email protected] rough_book]# a=5
[[email protected] rough_book]# b=10
[[email protected] rough_book]# cat -n passwd |sed -n "${a},${b}p"
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
- Single quotation marks don't work
- You can store patterns in both single and double quotation marks

边栏推荐
- GPIO operation method of imx6ull
- clickhouse 21. X cluster four piece one copy deployment
- 新东方转型双语直播带货给项目管理人的一场课
- Kibana+elk cluster log processing
- Grafana 9 正式发布,更易用,更酷炫了!
- RSPS2022 Finalist | Dr. Yang Bai 简介
- Heartless sword in Chinese
- "Half of Zhejiang's Venture Capital Circle" must be state-owned assets
- [win11] right click fix to modify the registry but not create a new one
- 国产手机干翻苹果?原来是靠百元机和猛降价实现的
猜你喜欢

Ad20/altium Designer - oil for manhole cover

腾讯云国际版云服务器欠费说明

Tasks and responsibilities of the test team and basic concepts of testing

math_角函数&反三角函数

测试组的任务职责和测试的基本概念

Simple integration of client go gin -update

RSPS2022 Finalist | Dr. Yang Bai 简介

Database industry analysis: from the global IT industry trend to the development of domestic databases

缺失值处理

如何做好研发效能度量及指标选取
随机推荐
Parallel integrates with moonbeam through xcm, bringing para and defi use cases into moonbeam ecology
Pytoch -- error reporting solution: "torch/optim/adamw.py" beta1, unboundlocalerror: local variable 'beta1‘
传输层 知识点总结
当线上线下的融合加速,当信息对接渠道的多样化,传统意义上的中心将没有必要
[applet project development -- Jingdong Mall] subcontracting configuration of uni app development
Content recommendation process
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
How to solve the problem of database?
Tasks and responsibilities of the test team and basic concepts of testing
Short video with goods source code, save pictures to photo album / Gallery
math_角函数&反三角函数
Make it more automatic to transfer slow logs and audit logs of RDS (for MySQL) databases to OBS across spaces
How does flynk MySQL CDC guarantee the server_ Is the ID globally unique?
Huawei cloud "digital intelligence" operation and maintenance
网页制作存在的一些难点
写一本畅销书是怎样的一种体验
ADAU1452开发系统界面与代码数据之间的关系
Nuxt - Universal(SSR / SSG)/ Single Page App(渲染模式)
炒股开户选择哪个券商公司是最安全最好呢
Network intelligent operation and maintenance helps improve operation and maintenance efficiency