Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux shell summing up the course

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

An array in 1 shell can be an associative array, but the index value is not an array 0 1 2

Decalre-A f_value # declares that the array can be omitted

Define value = ([apple] = '100doll' [orange] = '150 doll') define the array

Echo ${f _ value [apple]} # to the array value

For example:

Declare-A f_fruit # must declare that unlike those with an index of 012, there is no need to declare # declare-an A, which defines a normal array.

Fallow fruit = ([apple] = '100RMB' [orange] =' 50RMB')

Echo ${f _ fruit [apple]}

Echo ${f _ fruit [orange]}

Echo ${! f_fruit [*]}

3 sh script debugging

Debug all sh-x

Partial debugging only debugs the class capacity in the code

Set-x

Code

Set + x

Example:

#! / bin/bash

#

For i in {1..6}

Do

Set-x

Echo $I

Set + x

Done

Echo "script executed"

Note: set-x displays parameters and commands during execution

Set + x forbids debugging

4. Read read. Enter without enter indicates that you are sure to end with a specific character.

Read-d ":"

For example

Read-p "Please enter you name:"-d ":" NAME

For example

Read-p "Input you Name:" Name

Read-d ":"-p "Input you User:" User

Echo

Read-t 5-s-p "Input you Password:" Password

Add:

-s no echo mode such as password input

-t timeout setting

5 run until the command succeeds

Repeat ()

{while true:; do $@ & & return; sleep 30 done}

Execute the function repeat wget-c http://download

6 find based on depth depth

Find-maxdepth 2 level 2

-minimum depth of mindepth

Find / data/zhiliang.cui/scripts/-mindepth 10-name "* .sh"-print

7 one-way hash algorithm md5sum

Generate checksum

[root@cui1 data] # md5sum / data/zhiliang.cui/scripts/sh/1.sh / data/zhiliang.cui/scripts/sh/for.sh

24edee6130b1e1c6e57708592f6d6fa9 / data/zhiliang.cui/scripts/sh/1.sh

81eef2d13e3dfeabfe53e6a3ddf43156 / data/zhiliang.cui/scripts/sh/for.sh

Directing to the file file_sum.md5

-C check checksum and file checksum comparison

The result of the modified file

-- status does not output anything and is generally used to perform the function of return codes

Note:

Other encryption algorithms:

Base64

[root@cui1 data] # cat file_sum.md5

24edee6130b1e1c6e57708592f6d6fa9 / data/zhiliang.cui/scripts/sh/1.sh

81eef2d13e3dfeabfe53e6a3ddf43156 / data/zhiliang.cui/scripts/sh/for.sh

[root@cui1 data] # base64 file_sum.md5

MjRlZGVlNjEzMGIxZTFjNmU1NzcwODU5MmY2ZDZmYTkgIC9kYXRhL3poaWxpYW5nLmN1aS9zY3Jp

CHRzL3NoLzEuc2gKODFlZWYyZDEzZTNkZmVhYmZlNTNlNmEzZGRmNDMxNTYgIC9kYXRhL3poaWxp

YW5nLmN1aS9zY3JpcHRzL3NoL2Zvci5zaAo=

[root@cui1 data] # base64 file_sum.md5 > base64

[root@cui1 data] # base64-d base64 # decryption

24edee6130b1e1c6e57708592f6d6fa9 / data/zhiliang.cui/scripts/sh/1.sh

81eef2d13e3dfeabfe53e6a3ddf43156 / data/zhiliang.cui/scripts/sh/for.sh

Gpg encryption

Gpg-c file encrypts interactively

Decryption

Gpg-d decrypts it interactively

Encryption method with salt:

Openssl-1-salt SALT_STRING PASSWORD

8 delimited files

Separate split by size or number of rows

-b is delimited by size-b 10K is delimited by a file size of 10K

-l cut in a row

Split-b 100K messages-20150212

-a specifies that the [suffix] length defaults to 2.

-d is delimited with a number

The default is the letter suffix.

The prefix can be specified: split [OPTION] [INPUT [PREFIX]]

For example:

[root@cui1 test] # split-b 100K-d-a 4 messages-20150212 message

[root@cui1 test] # ll

Total dosage 760

-rw-r--r-- 1 root root 102400 March 3 00:21 message 0000 {message as the specified prefix 0000 means to use an array as the suffix}

-rw-r--r-- 1 root root 102400 March 3 00:21 message0001

-rw-r--r-- 1 root root 102400 March 3 00:21 message0002

-rw-r--r-- 1 root root 78495 March 3 00:21 message0003

-rwxr-xr-x 1 root root 385695 March 3 00:21 messages-20150212

Note: extended csplit is a split variant tool that supports specified file characters as cutters.

9 accelerate the execution of scripts using parallel processes

^ C [root@cui1 test] # vim sleep

#! / bin/sh

#

Pidarray= ()

For ((item1witi file.txt

Lynx is a full-featured WWW browser with a character interface that supports many operating systems.

Lynx-source source files of web pages under lynx-source http://pim.qq.com dump

12 sftp simulation ftp format into an interactive command

The server does not need to open the ftp service, just

Sftp-oPort=22 root@192.168.7.235

13 lastb failed to log in to get

14 watch dynamic observation of changes

Wathc COMMAND

Default 2 seconds

-n SECOND specified number of seconds

-d the part of the color that indicates the change

15 fsck

Fsck automatically detects the file system type and calls fsck.type, but does a final simulation before testing

Fsck-N simulates to see that the results are actually executed. For example, fsck may mark some sectors as bad sectors, but we want to recover data from them.

A

-A Walk through the / etc/fstab file

Fsck testing must not be in the mount state

16 shell combined with mysql operation

Mysql-u $USER-p$PASS / dev/null

Create table students (

Id int

Name varchar (100)

Mark int

);

EOF

Number of 17 ping-c

Note: as long as one of the values can ping, its ech $? The return value is 0. If all ping fails, the return value is 1 (unsuccessful).

18 print out the file starting from the second line in the file

Tail-n + 2

-n,-- lines=K

Output the last K lines, instead of the last 10; or use-n + K to output lines starting with the Kth

Sed-n'2

Synonym:

Head-n-Kth

19 ls command

Ls-S can be sorted by size, from large to small

Commonly used ls-lhS

Ls-lhSr

Sort when ls-t has been modified

Commonly used ls-lt (from the latest file to the oldest)

Ls-ltr

Ls-n displays uid and gid instead of username and user group

20 gzip

Usage

Gzip file

The default source file is deleted to generate file.gz

How to save the source file and output the compressed file? you can use the-c option original

-c Write output on standard output; keep original files unchanged.

Gzip-c file > file.gz

Lenovo:

Cat file1 file2 file3 | gzip > file.gz

Gzip-l file.gz checks the compression ratio of a file

Gunzip = gzip-d

Gunzip-c file.gz can keep the original file

Mount a disk for repair

Guestmount-a vm112x113.img-m / dev/sda2 / mnt

21 find out which shell you are currently in

Echo "$0" or ps-p $$or ps $$

How to find out on which layer the child's shell can exit ctrl + D?

Find out the level of the child shell or the nesting level of the temporary shell. After each bash instance is started, the value of the variable $SHLVL is incremented.

Such as

Echo "$SHLVL"

The result is 1.

Enter screen

Echo "$SHLVL"

The result is 2.

Enter bash

Echo "$SHLVL"

The result is 3.

22 lsof + D / usr/local/ is the same as above, but will search the directory under the directory for a long time

23 find / data/-name "* sum*"-type d

Find / data-name "keyword"-type f looks for files with the keyword keyword under the path

23 eval

An eval command will scan the command line for all replacements before executing the command. This command applies to variables that cannot function with a single scan. This command scans the variable twice. These variables that require two scans are sometimes called complex variables. But these variables themselves are not complex.

Echo "Last argument os" $(eval echo\ $$#) ""

Eval command: combine the parameters into a new command and give it to Shell for execution

Eval can read a series of parameters and execute commands according to the contents of the parameters themselves. It is often used to deal with variables in Shell scripts, with an unlimited number of parameters, separated by semicolons, and returns a value of 0 if the command is followed by no arguments or if the arguments are empty.

Usage: eval [arg...]

Parameters that arg gives to eval for combination

Example:

1. Edit a shell, first define a variable and assign the ls command, and then pass the variable to eval

#! / bin/bash

# define variables

Command= "ls-l / home"

The eval $command # access variable is preceded by a $symbol.

2. Pass ls-- color-l\ as a parameter to eval

Linux@office:~$ eval'ls-- color-l\'

Total 72

-rwxrwxrwx 1 linux linux 106 2011-03-04 09:58 cmd

Drwxr-xr-x 2 linux linux 4096 2011-03-04 09:15 Desktop

Drwxr-xr-x 14 linux linux 4096 2011-03-04 08:00 Documents

Drwxr-xr-x 4 linux linux 4096 2011-03-04 09:15 Downloads

-rw-r--r-- 1 linux linux 619 2011-03-04 09:42 echo

24 delete all except a file under this path

Rm-rf `ls | grep-v "tmux" `

Important options in 25 cp (scp)

-p preserver retains the original permissions and timestamp of the file

The recursive copy directory of rhand Mutual R

-an equals-p-r-d

Note: the result of scp-r D1 D2 is d2/d1

The result of scp-r d1amp * D2 is D2max *

26 kill loses a user and forces the user to exit

1 w check which users log in

2 ps-ef | grep "pts/4"

3 kill just drop the sshd process of this user

Kill-9 17306

27 passwd-S check the latest password change time. If the password has not been changed again, you can determine the creation time of this account.

[root@cui1 zhiliang] # passwd-S zhiliang

Zhiliang PS 2015-08-02 0 99999 7-1 (password is set and encrypted using SHA512.)

28 multiple lines change into one line, and you can implement multiple lines into one line if necessary

If you need to implement two parameters into a line, you can try xargs-N2

-n Use at most max-args arguments per command line

Note: of course, it can be implemented with awk's multiline processing technology (to be studied)

29:awk-F "regular expression"

Df-h | grep "/ boot" | awk-F "[\ t%] +" {print $5}'

thirty

Advanced uses of head and tail:

Head

-n,-- lines= [-] K

Print the first K lines instead of the first 10; with the leading'-', print all but the last K

Lines of each file

For example, how to use it: head-nMel 2 (if k is preceded by a-sign, it means to print everything except the last 2 lines)

Tail

-n,-- lines=K output the last K lines, instead of the last 10; or use-n + K to output lines starting with the Kth

For example, how to use it: tail-Numeric 2 outputs all the lines starting from the second line.

The output starts at the second line, that is, filtered to the first line.

31 how to view the full access modify (file content changed) change time of a file

Ls-l filename can only see the modify time of the day of the month.

View full time method 1 ls-1-full-time filename

Stat filename

[root@cui1 zhiliang.cui] # ls-l-full-time trigge83_list

-rw-r--r-- 1 root root 232089 2015-08-09 06 purl 21 trigge83_list 40.388338770-0400

[root@cui1 zhiliang.cui] # stat trigge83_list

File: "trigge83_list"

Size: 232089 Blocks: 456IO Block: 4096 ordinary files

Device: fd00h/64768d Inode: 279504 Links: 1

Access: (0644) Uid: (0 / root) Gid: (0 / root)

Access: 2015-08-09 06 20 purl 26.695384447-0400

Modify: 2015-08-09 06 Fringe 21 purl 40.388338770-0400

Change: 2015-08-09 06 26 Vera 10.308193915-0400

Problems with 32 ftp client

There are original ftp yum install ftp and window, etc., and lftp yum install-y lftp

Different commands may support different sex.

For example, when uploading ftp, put localfile remotefile (where remotefile cannot be omitted, that is, only renamed files)

It is generally recommended to use lftp

User name: password @ remotehost

Lftp ftp: NGjZPR86KinJO69Vf@192.168.216.171

The role of 33 sh-c

-c string, shell to execute a series of commands in the following string

Sh-c'cd / data/zhiliang.cui; touch testfiel'

34 sudo allows you to switch identities to perform a task, not just sudo defaults to root users

Sudo-u username execute command

Vs.

Su-username execute command

The difference between the two

The former is executed as a user, while su-switches to this user to execute the command, logs in first, and exits after the execution of the command is completed!

Therefore, if you want to execute a command in a certain capacity, sudo-u username is recommended, because this way of su-can not always achieve the goal.

For example, sshd, if the user does not have login permission to nologin, it cannot be completed with su -.

Such as:

Note: by default, root can switch to any user without a password.

Look at this:

[root@mail ~] # cat / etc/sudoers | grep root

# # the root user, without needing the root password.

# # Allow root to run any commands anywhere

Root ALL= (ALL) ALL

# # cdrom as root

Classic usage:

Sudo-u zhiliang sh-c'cd / home/zhiliang; touch 1'

35 differences between source filename.sh and. / filename.sh and sh filename.sh / root/filename.sh

. / filename.sh is similar to / root/filename.sh must have execution permission and first line

# / bin/bash definition interpreter

The reason why you can't filename.sh is that the shell terminal regards it as a command, and / root/filename.sh starts with "/", so shell knows it's a file, not a command.

Sh filename.sh executes the sh script file as a parameter to sh

The essence of the difference between the previous execution of source filename.sh is that there is no starting child shell, which is executed in the environment of this shell!

[root@master ~] # cat test.sh

#! / bin/bash

Axi1

Source test.sh

And sh test.sh

Difference

There is echo $a when it is executed by source, and there is no a variable when executed by sh.

Another unusual thing is that exec can study it.

Exec 3 / dev/shm/abc.txt

Cat / dev/shm/abc.txt

Function: need to quickly extract files that can be written to this directory and run in memory all the time

Note: 1 restart loss 2 default is half the size of memory 3 file system is tmpfs 4 distinguish swap this virtual memory

39 screen

Screen-S name creates a window virtual terminal like this

Screen-ls

Screen-r name/id recovery window

Ctrl + a + d exits without closing the window

Ctrl + a + k kills the current session window

Ctrl + a + z suspends this session back to the state of SHLVL=1 (fg 1 resumes)

Screen-d pid/name sets this window offline

Echo $SHLVL can check whether it is in a window or in a normal shell terminal

Screen-x multiple people can watch the dynamic window in Attached at the same time (suitable for remote multiple people to watch the screen

Curtain)

Note: three states of conversation in screen

1 Attached is running, if you want screen-x name/pid, you can see it.

2 Detached offline screen-r name/pid can be restored to that window

3 Dead is killed by kill-9 pid, if you need to remove screen-wipe pid/name

40 uname-I = arch to get hardware platform

41 shell custom function library and incoming function library

Shell functions call each other

# Source function library.

. . /

Generally write their own function recommendations and function libraries in the same level of directory! Easy to move

42: test speed ssh throughput

Yum install pv

Yes | pv | ssh 192.168.7.235 "cat > / dev/null"

forty-three

Client connection data

Mysql-h292.168.7.235-uroot-p

Basic database syntax:

Grant all on *. * to 'cui'@'192.168.7.235' identified by' 12qwaszx'

Select user,password,host from mysql.user

44 ssh uses the option of automation

Ssh $ip-o UserKnownHostsFile=/dev/null-o ConnectTimeout=3-o BatchMode=yes-o StrictHostKeyChecking=no

45 shell command combination

To execute a set of commands in the current shell

() operator:

(command; command )

The general use is

Ls b.txt | (echo "one" & & echo "two")'

Or

Ls b.txt | | (echo "one"; echo "two")

46 mkpasswd create a random password (length and complexity can be specified)

Often used with echo "password" | passwd-stdin root

Mkpasswd-l 8-s 2

-l password length

The number of-s special characters

For example, result: s | 7dF5_V

Echo "s | 7dF5_V" | passwd-stdin root

47 iptables and tcpdump often don't remember orders

Tcpdump-nn port 53

Iptables-An INPUT-s 10.1.110.30-p udp-dport 53

Iptables-D INPUT number

-vv

-e can display mac address

-nn non-inverse solution

Src keyword source ip address

Dst destination address

Dst port 22 destination port

Srt port 22 Source Port

Or or

And and

Tcpdump-vv dst port 22

Tcpdump 'port 80 and (host 192.168.1.10 or host 192.168.1.11)'

48 how to view an application in a blocking state such as DNS blocking

Netstat-tulnp | grep 53

View

Check the size of the first number, if there is a jam, it is not 0

Meaning:

Recv-Q: how many bytes have not been passed into the socket

Send-Q: how many bytes are not recognized by the remote receiver

49 [- s file] true if the file exists and has content (content in the file)

#! / bin/bash

If [!-s "b"]

Then

Echo "empty file or file does not exist"

Else

Echo "content exists in file"

Fi

~

Principle of 50 linux file deletion

Multi-separator for 51 awk

Echo "- 1 print 2:" | awk-F "[-:] +" {print $2}'

52 three swords customers pick up the trip

Ifconfig eth0 | grep "inet addr" | awk-F "[:\ t] +" {print $4}'

Ifconfig eth0 | sed-n '2p' | awk-F "[:\ t] +" {print $4}'

Ifconfig eth0 | awk-F "[:\ t] +"'NR = = 2 {print $4}'

Note: NR: line number currently processed

Regular BRE (Ten) and ERE in 52 linux

1)。 Any character, but does not contain (\ n)

2) * match the previous 0 to infinite number of repeats

3) [0-9] match 0-9

4)\ escape characters to restore the nature of special characters

5). * any character, matching and repeating any number of times

6) the beginning of the ^ match line

7) $end

8)\ {n egrep m\} matches the previous n to m times without escaping

\ {n,\}

\ {n\}

Egrep-- color=auto "r {1jue 5}" / etc/passwd

Grep-E-- color=auto "r {1jue 5}" / etc/passwd

Equal to

Grep-- color=auto "r\ {1jue 5\}" / etc/passwd

[[: alnum:]] means [0-9A-Za-z]

[[: space:]]

ERE (egrep)

| |

() Group

+

?

Egrep-- color=auto "^ (root | sshd):" / etc/passwd

A regular expression may be followed by one of several repetition operators:

? The preceding item is optional and matched at most once.

* The preceding item will be matched zero or more times.

+ The preceding item will be matched one or more times.

{n} The preceding item is matched exactly n times.

{n,} The preceding item is matched n or more times.

{, m} The preceding item is matched at most m times.

{n,m} The preceding item is matched at least n times, but not more than m times.

Filter REGULAR in man grep to find

53 canonical replacement combined with sed

Sed's # regular # cannot be replaced with null regular expression # g' action

Combine the parenthesis grouping function of se

Ifconfig eth0 | sed-n's # ^. * inet addr:\ (. *\) Bcast.*$#\ 1roomgp'

[root@master ~] # ifconfig eth0 | sed-n's # ^. * inet addr:\ (. *\) Bcast:\ (. *\) M.birthday #\ 1\ 2roomgp'

192.168.7.235 192.168.7.255

Access to digital permissions

Stat file1 | sed-n's # ^. * s: (\ ([0-9] *). * $#\ 1roomgp'

Equal to

Stat-c a file1

Options for 54 tree

Tree-dL 2

-d print only the directory

The level of L printing-L 1 prints only one level

-I do not indent

-f print full path

Tree-idL 1 | egrep-v "^ $| direct |\. | xargs du-sh

55 principle of minimization of operation and maintenance

1 system minimization

2 Service startup minimization

3 minimize rm-f file.txt (without rm-rf file.txt)

4 login minimization without root

5 permission minimization

6 minimization of configuration parameters

56 back up files before modification

Cp file file.ori

Revised comparison

Vimdiff file file.ori

fifty-seven

Change sshd_config

How to switch to root (with root permission):

Note: commands that cannot be used by sudo

1 built-in commands such as type

Commands that cannot be found in 2 path

58 enlarge the file descriptor

[root@shop-web01 ~] # ulimit-n

65536

The default is 1024

/ etc/security/limits.conf

Can be executed directly

Echo "*-nofile 65535" > > / etc/security/limits.conf

59 simply encrypt shell files

Method 1: gzexe

(encrypt colleague compression)

The original unencrypted files will be backed up as file.sh~, and the file.sh will be turned into encrypted files.

Restore:

Gzexe-d file.sh

60 guard against

Hide version information

> / etc/issue

61 locking important files in the system

Lock

Chattr + I / etc/passwd / etc/group / etc/shadow / etc/gshadow

Unlock

Chattr-I

A file with the 'i'attribute cannot be modified

62 the default foreign rpm source is changed to domestic source

Http://mirrors.163.com/.help/centos.html

Types of files in 63 windows are distinguished by extension (.txt)

File.txt

File is the file name. Txt is the extension.

The extension of linux has nothing to do with the file type, but it is easy for users to identify by following the habit of windows

The files in 64 linux are ordinary files, which can be roughly divided into three types according to their contents.

1 ASCII plain text file

2 two-tier files, usually executable commands cat ls cd

3 data file data format file, [root@www py] # file / var/log/wtmp

/ var/log/wtmp: dat

Use the file command to determine the type of ordinary file

65 filter the directories under this path

Ls-l | grep "^ d"

Or tree-diL1 | grep "[Amurz]"

66 inode stored information, except for file names

67 ssh tool

Transfer tool between windows client and linux server

1 rz sz

2 winscp based on ssh sftp

3 SFX (xshell)

4 sftp based on SSH encrypted transport

5 samba http ftp nfs

Note: sftp connection method:

Sftp-o Port=22 root@192.168.100.13

sixty-eight

. / etc/init.d/functions

Professional script

#! / bin/bash

. / etc/init.d/functions

Action "scp ile to remotedir is ok" / bin/true

69:

25 Apr 15:30:17 ntpdate [11520]: no server suitable for synchronization found

In fact, this is not a mistake. Instead, it takes about 3-5 minutes for the client to establish a normal communication connection with server after each restart of the NTP server. This information will be reported when the client is connected to the server at this time. It usually takes a few minutes to wait.

70 ssh execute sudo command

Ssh executes sudo commands

[root@master] # tail-1 / etc/sudoers

Oldbody ALL= (ALL) NOPASSWD: / bin/cp

[oldboy@master ~] $scp hosts oldboy@192.168.100.13:~

Hosts 100% 184 0.2KB/s 00:00

[oldboy@master ~] $ssh oldboy@192.168.100.13 sudo cp ~ / hosts / etc/

Sudo: sorry, you must have a tty to run sudo

Solution: 2 kinds

1-t option

[oldboy@master] $ssh-t oldboy@192.168.100.13 sudo cp ~ / hosts / etc/

Connection to 192.168.100.13 closed.

Principle source: view the sudoers file:

# Disable "ssh hostname sudo", because it will show the password in clear.

# You have to run "ssh-t hostname sudo".

#

Defaults requiretty

71 sort depth

-r

-n

-k

-t

Sort the arp file by the size of the ip address (the third column ranks the fourth column)

Cat.arp

192.168.0.1 00:0c:29:65:14:0f

192.168.1.10 00:0c:29:65:14:0f

192.168.20.9 00:0c:29:65:14:0f

192.168.0.10 00:0c:29:65:14:0f

192.168.1.11 00:0c:29:65:14:0f

192.168.10.10 00:0c:29:65:14:0f

192.168.200.8 00:0c:29:65:14:0f

192.168.19.2 00:50:56:f9:96:ad

Results:

[oldboy@slave ~] $cat arp.txt | sort-n-t "."-K3Magol 3-K4.1 Person4.3

192.168.0.1 00:0c:29:65:14:0f

192.168.0.10 00:0c:29:65:14:0f

192.168.1.10 00:0c:29:65:14:0f

192.168.1.11 00:0c:29:65:14:0f

192.168.10.10 00:0c:29:65:14:0f

192.168.19.2 00:50:56:f9:96:ad

192.168.20.9 00:0c:29:65:14:0f

192.168.200.8 00:0c:29:65:14:0f

The command explains:

Sort-n-t "."-K3Magol 3-k4.1pr 4.3

Sort by-n number, by-t "." Separate, ending with-k third column (starting with the first character) to third column (last character)

The three characters from the first character of the fourth column to the fourth column of-k are sorted to the end of k.

= = sort-n-t "."-k3pj3-k4 although the result is the same, but the post-sorting time is longer (because-k4 is a default first character to the end of the fourth column, which will include the mac address to participate in the sorting)

72 awk Bao Ding Jie Niu

[oldboy@slave ~] $cat access.log

Http://www.baidu.com/index.html

Http://www.12306.cn/jsp.php

Http://www.sina.com.cn/index.html

Http://www.12306.cn/jsp.php

Http://www.sina.com.cn/index.html

Http://www.12306.cn/jsp.php

Http://www.taobao.com/

Http://www.baidu.com/index.html

[oldboy@slave ~] $cat access.log | awk-F'/'{print $3}'| sort-rn | uniq-c

1 www.taobao.com

2 www.sina.com.cn

2 www.baidu.com

3 www.12306.cn

It is equivalent to sorting statistics by using the array method of awk.

[oldboy@slave ~] $cat access.log | awk-F'/'{print $3}'| awk'{+ + S [$NF]} END {for (k in S) print S [k], k}'| sort-n

1 www.taobao.com

2 www.baidu.com

2 www.sina.com.cn

3 www.12306.cn

Statistical group of awk

Awk'{+ + S [$NF]} END {for (k in S) print S [k], k}'

Bao Ding Jie Niu:

S [] defines a number group called S, which in awk usually starts at 1 instead of 0

NF records the number of fields before, separated by spaces by default. In the records shown above, the number of NF fields is equal to 1.

$NF represents the value of the last field of a row, and $NF equals $1 in the above

S [$NF] represents the value of the number group element, such as the value of the network station as key

+ + S [$NF] means to add a number to one, for example, the record of a complaint is to add one to the name of the station.

The END awk line has finished processing the sentences of the execution.

For (key in S) traversal

Print S [k], k print value

Statistical group of awk

Awk'{+ + S [$NF]} END {for (k in S) print S [k], k}'

Bao Ding Jie Niu:

S [] defines a number group called S, which in awk usually starts at 1 instead of 0

NF records the number of fields before, separated by spaces by default. In the records shown above, the number of NF fields is equal to 1.

$NF represents the value of the last field of a row, and $NF equals $1 in the above

S [$NF] represents the value of the number group element, such as the value of the network station as key

+ + S [$NF] means to add a number to one, for example, the record of a complaint is to add one to the name of the station.

The END awk line has finished processing the sentences of the execution.

For (key in S) traversal

Print S [k], k print value

Note: awk can directly deal with text, can separate awk text without the need for cat text first, improve performance.

2 Statistical value of tcp connection status of netstat

[oldboy@slave ~] $netstat-an | awk'/ ^ tcp/ {+ + S [$6]} END {for (k in S) print S [k] "\ t" k}'

2 ESTABLISHED

10 LISTEN

3 count the IP address of the public network, no matter what status it is

[oldboy@slave ~] $netstat-an | awk-F "[:] +" / ^ tcp/ {+ + S [$6]} END {for (k in S) print KLI S [k]}'| grep "^ [0-9]" | sort-n

0.0.0.0 5

192.168.100.10 1

192.168.100.1 1

73 files can be tab incomplete if they have execution permissions (directories can be incomplete because they have execution permissions)

For example, / etc/init.d/rsyslog can be completed.

And / var/log/mes can't be incomplete.

74 bash pipeline for execution

Cat a.log | bash

75 filter the next few fields in the file line

[root@log_server zhiliang.cui] # cat column.log

1 ls / etc/init.d/

2 cd / root/

33 bash x.sh 1 2

45 echo "abc"

46 echo $?

[root@log_server zhiliang.cui] # grep-o "[Amurz]. *" column.log

Ls / etc/init.d/

Cd / root/

Bash x.sh 1 2

Echo "abc"

Echo $?

Note: if there is a letter grep in the first column, it will not come out.

Remove all columns in the file except the first column

[root@log_server zhiliang.cui] # cat column2.log

1a ls / etc/init.d/

2b cd / root/

33 bash x.sh 1 2

45 echo "abc"

46 echo $?

[root@log_server zhiliang.cui] # awk'{for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report