当前位置:网站首页>[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

边栏推荐
- 【工具】pip和conda的相關使用
- azkaban启动报错 2022/06/20 21:39:27.726 +0800 ERROR [StdOutErrRedirect] [Azkaban] Exception in thread “m
- 不断重修的计划与变化
- When online and offline integration accelerates and information docking channels are diversified, the traditional center will not be necessary
- You guys, the first time you used flick MySQL CDC, now the program starts without error, and the new data is not printed out
- Nuxt - Universal(SSR / SSG)/ Single Page App(渲染模式)
- Principle of synchronized implementation
- Nuxt - Universal (SSR / SSG) / single page app (rendering mode)
- [fpga+pwm] design and implementation of phase shift trigger circuit for three-phase PWM rectifier based on FPGA
- @“齐鲁多娇”幸运用户,山东5A景区喊你免费游园啦!
猜你喜欢

"Half of Zhejiang's Venture Capital Circle" must be state-owned assets

Typescript (7) generic

如何做好研发效能度量及指标选取

国产手机干翻苹果?原来是靠百元机和猛降价实现的

巴比特 | 元宇宙每日必读:传腾讯成立XR部门,元宇宙板块再次上涨,多家券商发报告关注虚拟人的投资机会...

Unity中通过射线躲避障碍物寻路的一些初步探索

Ad20/altium Designer - oil for manhole cover
![azkaban启动报错 2022/06/20 21:39:27.726 +0800 ERROR [StdOutErrRedirect] [Azkaban] Exception in thread “m](/img/02/2e402f05022b36dc48ff47232e8535.png)
azkaban启动报错 2022/06/20 21:39:27.726 +0800 ERROR [StdOutErrRedirect] [Azkaban] Exception in thread “m

【win11】注册表修改fix 右键没有新建

游戏NFT市场:OpenSea最易被切下的蛋糕
随机推荐
[applet project development -- Jingdong Mall] configuration tabbar & window style for uni app development
What is flush software? Is it safe to open a mobile account?
【FPGA+PWM】基于FPGA的三相PWM整流器移相触发电路的设计与实现
【人脸识别】基于GoogleNet深度学习网络的人脸识别matlab仿真
抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
利用Inkscape转换为dxf文件的正确方法 svg导出dxf文件
当线上线下的融合加速,当信息对接渠道的多样化,传统意义上的中心将没有必要
Using stream API instead of SQL
How does flynk MySQL CDC guarantee the server_ Is the ID globally unique?
Graduation season · undergraduate graduation thoughts -- the self-help road of mechanical er
Unity中通过射线躲避障碍物寻路的一些初步探索
东华大学|具有强化知识感知推理的可解释推荐微观行为研究
GPIO operation method of imx6ull
Live streaming: dongyuhui is so popular. Is there anyone watching liuzhenhong?
网页制作存在的一些难点
Nuxt - Universal (SSR / SSG) / single page app (rendering mode)
Grafana 9 is officially released, which is easier to use and more cool!
imx6ull的GPIO操作方法
2022年5月中国游戏厂商及应用出海 EMEA 地区收入30强
Filebeat collects log data and transfers it to redis. Different es indexes are created based on log fields through logstash