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

  1. According to line number
  2. According to the pattern --》 Regular expressions = character + Special symbols
  3. 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

原网站

版权声明
本文为[FanMY_ seventy-one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221651238757.html