Application of shell script

shell

script

diff

Usage
diff [options] files |directorys

Output information:

[num1,num2][a|c|d][num3,num4]
num1,num2 ##The line in the first file
a ##Add
c ##Change
d ##Delete
< ##The line in the first file content

                ##第二文件中的内容

num3,num4 ##Line in the second file

Common parameters:

-b ##Ignore spaces
-B ##Ignore blank lines
-i ##Ignore case
-c ##Display all contents of the file and mark the difference
-r ##Comparison directory-
u ##Merge output
Insert picture description here
Insert picture description here

2.path

patch original file patch file
-b ### backup original file
Insert picture description here

diff -u westos westos1> westos.path ##Compare the difference between westos and westos1 to generate a patch file westos.path
patch westos westos.path ##打 Patch
diff westos westos1 ##At this time, the file westos is no different from the file westos1

Insert picture description here
Note: The generated backup file will be saved as a file with a suffix of **.org**

3.cut

cut
-d: ##Specify: For the delimiter
-f ##Specify the displayed line
5 The fifth line
3, 5, 3 and 5 lines
3-5 3 to 5 lines
5-5th line after
-5 to 5th line
-c # #Specify the interception character (number usage is the same as -f)
Insert picture description here

Insert picture description here

4.sort

-n ##Pure numeric sort

-r ##回述
-u ##Remove duplicates
-o ##Output to the specified file
-t ##Specify the separator
-k ##Specify the sorted column
Insert picture description here

Insert picture description here

5.uniq
-d ##Display duplicate rows
-c ##Merge duplicates and count the number
-u ##Display unique rows
Insert picture description here

5.&& ||

&& ## means an action that meets the conditions
|| ## an action that does not meet the conditions
Experimental demonstration:
Insert picture description here

text

text=[] ##[] is equivalent to the text command
"text $a = b "= [" b "= ["b=["a"="$b"]

Digital comparison of text

=
!=
-eq ## equal to-
ne ##not equal to-
le ##less than or equal to-lt
##less than-
ge ##greater than or equal to-gt
##greater than

Conditional relationship of text
-a ## and
-o ## or

text Judgment to empty
-n ##nozero Judgment content is not empty
-z ##zero Judgment content is empty

Insert picture description here

Insert picture description here

Execute the following script to determine the user type
user_check.sh user The
user type is
super user
system user
common user

Text's judgment on files

-et ##file node-
nt ##Is file 1 newer than file 2
-ot ##Is file 1 older than file 2
-d ##directory-
S ##Socket-
L ##soft connection-
e ## existence-
f ## ordinary file-
b ##block device-
c ##character device

Insert picture description here

3.awk

awk -F split vice BEGIN {}{}END{} FILENAME

NR ##行数
NF ##列数
FILENAME ##file name itself
westos ##westos variable value
"westos" ## westos string

/bash$/ ##Condition
Condition 1|Condition2/ ##Condition 1 or Condition 2
/Condition1/||/Condition2/ ###Condition 1 or Condition 2
/Condition1/&&/Condition2/ ##Condition 1 and condition 2

$0 #all columns
$1 #first column
$2
$3

awk -F:'BEGIN{print “userlist:”}{print $1}END{print “end”}' passwd
userlist:
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
end
##begins with userlist: end For end
Insert picture description here

-F awk: '/ the bash $ / {Print Katex the parse error: the Expected' the EOF ', GOT'} 'AT position 2:}. 1' shown in the passwd ... BA / && / ^ the root / Print $ {}. 1 'the passwd
display The first column ending in bash and beginning with root

awk -F:'/bash$/||/^root/{print $1}' passwd
displays the first column ending with bash or starting with root

awk -F:'/bash$/||/^root/{print $1""$2}' passwd
displays the first and second columns ending in bash or beginning with root

awk -F:'{print NR }'passwd
display the number of lines

awk -F:'{print NF }'passwd
shows the number of columns
Insert picture description here

awk -F:'{print FILENAME }'passwd
displays the name of the file itself

awk -F:'$7~//bin/bash/{print $1}' passwd
displays the seventh column and the first line ending with /bin/bash

awk -F:'$7!~//bin/bash/{print $1}' passwd
shows that the seventh column is not the first line ending with /bin/bash

awk -F:'/root/ {print $0}' passwd
displays all lines containing root

awk -F : ‘BEGIN{n=0}/bash ∣ s h |sh sh/&&$6!~/^\home/{N++}END{print N}’ /etc/passwd
Insert picture description here

ifconfig ens3 | awk ‘/inet>/{print $2}’

Guess you like

Origin blog.csdn.net/Antonhu/article/details/114477765