Linux common command parsing and Bash Shell use sample script demonstration

Hello, everyone. Meet again. I'm Quan Jun

abstract

Linux command is a program based on text format input and output. According to the Unix philosophy, the program function is simple, the input is loose, the output is rigorous, and various program combinations can have more powerful functions. The main reason for this flexibility is that Linux stipulates that the input and output of programs must adhere to the file stream format. That is, text format, which is one of the core of Linux system.

For bash, a kind of Shell. The default command-line interpreter for today's mainstream Linux distribution version number is a powerful tool. It can realize the combination of program commands supported by Linux. So as to realize powerful functions. Similar to the bat file of Window system, bash has more powerful functions. Bash can realize its own active program design, function operation and even system startup, which are due to the design concept of Unix.

This blog post is based on the author's experience and is only for learning reference. If there are omissions, please leave a message to correct them. Thank you very much~

——————————-- specific explanation of Linux commands -- -- -- -- --

Folder related commands

Displays the file details in the current folder. ls only displays the file name. Ll is another name of the 'ls -alF' command. You can view it through the 'alias| grep'll' command:

king@king-desktop:~/test$ ls
t1  test1
king@king-desktop:~/test$ ll
total 12
drwxr-xr-x  3 king king 4096 2014-08-23 18:26 ./
drwxr-xr-x 39 king king 4096 2014-08-23 18:26 ../
-rw-r--r--  1 king king    0 2014-08-23 18:26 t1
drwxr-xr-x  2 king king 4096 2014-08-23 18:26 test1/
king@king-desktop:~/test$ alias | grep  'll'
alias ll='ls -alF'

In addition, suppose you want to display it through the classic tree folder. Additional tree commands (not internal commands) can be used. For example, the following detailed operations, - L2 refers to taking the current folder as the root folder and displaying the folder structure to the second layer:

king@king-desktop:~$ tree
The program 'tree' is currently not installed.  You can install it by typing:
sudo apt-get install tree
king@king-desktop:~$ sudo apt-get install

king@king-desktop:~/test$ tree -L 2
.
|-- t1
`-- test1
    `-- t2
1 directory, 2 files

Create delete folder operation. Use the mkdir and rmdir commands. For deletion, assuming that the folder is not empty, rm – rf DirName can be used:

king@king-desktop:~/test$ ls
t1  test1
king@king-desktop:~/test$ mkdir test2
king@king-desktop:~/test$ rmdir test1
rmdir: failed to remove `test1': Directory not empty
king@king-desktop:~/test$ rm -rf test1
king@king-desktop:~/test$ ls
t1  test2
king@king-desktop:~/test$ rmdir test2/
king@king-desktop:~/test$ ls
t1

tar command use

It is often used for packaging, compression and decompression. The usage is related to parameters. C refers to packaging, x refers to extraction, and z refers to gzip compression. j refers to bzip compression. v shows the decompression process. C is the specified folder. This refers to the folder to unzip. Pack the folder test1, pay attention to the sequence of parameters followed, and display the name of the package, followed by the folder name

king@king-desktop:~/test$ tar -cvf test.tar test1/
test1/
test1/t2
king@king-desktop:~/test$ ls
t1  test1  test.tar

Extract the packaged files to the specified folder.-C realization
king@king-desktop:~/test$ tar -xvf test.tar -C ./test1/
test1/
test1/t2
king@king-desktop:~/test$ ls ./test1/
t2  test1

gzip Compression and decompression
king@king-desktop:~/test$ tar -czvf test.tar.gz test1/
king@king-desktop:~/test$ tar -xzvf test.tar.gz

bzip2 Compression and decompression
king@king-desktop:~/test$ tar -cjvf test.tar.bz test1/
tar -xjvf test.tar.bz test1/

Document related

Show file contents

Internal commands such as cat, more, less, head, tail and NL can be implemented, but they are slightly different

cat concatenates files to stdout, which is usually output at the terminal. Frequently used parameters include – n, and the function is similar to nl, that is, the line number is output at the same time.

more can be used to browse the file content that exceeds one page or the length displayed on the terminal, turn the page through the space bar, press Enter to read, press Q to exit, and the text is displayed on the terminal

less can be used to browse the contents of files that exceed one page or the length of the terminal display. Turn the page through the space bar, and press Enter to read. The up and down arrows can move forward or backward, and the Q key exits. The text is displayed in independent on mode

head followed by parameter – n 10. Of course, 10 can be changed, which refers to the first 10 lines of displayed text

tail followed by parameter – n 10, the same as above, refers to the last 10 lines of displayed text

nl is similar to cat -n

king@king-desktop:~/test$ cat t1
hello
world
!!!

END
gujinjin
king@king-desktop:~/test$ head -n 1 t1
hello
king@king-desktop:~/test$ tail -n 2 t1
END
gujinjin
king@king-desktop:~/test$ nl t1
     1	hello
     2	world
     3	!!!
       
     4	END
     5	gujinjin
king@king-desktop:~/test$ cat -n t1
     1	hello
     2	world
     3	!!!
     4	
     5	END
     6	gujinjin
king@king-desktop:~/test$ more t1
hello
world
!!!

END
gujinjin

Text stream processing and usage

Text stream mode is one of the core ideas of Linux. Thus, commands can be combined to form more powerful functions. There are many commands for processing text. Here I mainly introduce what I think at this moment. Please forgive me for my shortcomings!

Here I want to mention the awk command, the text processor, which is more powerful and wonderful, by A of Bell Labs. W. It's easy to get started. Here's an article by teacher Chen Hao. Published on CoolShell.cn. The web address is as follows. Those interested can see:

http://coolshell.cn/articles/9070.html

Introduction to special symbols
"|" Pipeline character, connecting the output of a program and the input path of a program
">"">>" Redirect and output to the specified file. The difference is that the former outputs and covers the original contents of the file, and the latter output is added to the end of the file

grep		(global search regular expression(RE) and print out the line), Parameters –o This means that only matching objects are output, not complete lines
 Demo example: output IP address
king@king-desktop:~/test$ ifconfig | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
192.168.229.200
192.168.229.255
255.255.255.0
127.0.0.1
255.0.0.0

cut  As the name suggests, the input text is removed and output
 To demonstrate a sample, you will IP Address segmentation, storing the above output in ip.log file
 Parameter interpretation: -s Do not output rows that do not include the specified delimiter. -d Specify separator, -f Output the specified segment, which can be multiple, such as –f1,3 Indicates output 1,3 paragraph
king@king-desktop:~/test$ cat ip.log | cut -s -d. -f1
192
192
255
127
255

sort That is, sorting. Two parameters are often used here, i.e –n Sort based on numerical value, generally in ascending order; -r Reverse, i.e reverse
 plus n It's a little different from not adding it. Here to show the difference, right ip.log Make a slight change and pay attention to the difference. In fact, it is regarded as the difference between numerical processing and string processing:
king@king-desktop:~/test$ cat ip.log | cut -s -d. -f1 | sort
127
192
192
20
255
255
king@king-desktop:~/test$ cat ip.log | cut -s -d. -f1 | sort -n
20
127
192
192
255
255
king@king-desktop:~/test$ cat ip.log | cut -s -d. -f1 | sort -nr
255
255
192
192
127
20

uniq  Unique, a parameter is often used here –c, Used for counting, which refers to the same up and down lines
king@king-desktop:~/test$ cat ip.log | cut -s -d. -f1 | sort | uniq -c
      1 127
      2 192
      1 20
      2 255

awk  Preliminary exploration of application. -F Followed by a separator, $1 Represents the first segment. See the connection described above for detailed usage.
king@king-desktop:~/test$ awk  -F. '$1>127 && $1<255 {print $0}' ip.log 
192.168.229.200
192.168.229.255

Text processing demo sample

Count the top 10 most frequently used historical commands

king@king-desktop:~/test$ history | awk '{print $2}' | sort | uniq -c | sort -nr | head -n 10
    255 ls
    168 clear
     79 cd
     64 history
     60 sh
     48 sudo
     46 cat
     22 vim
     20 clea
     19 tree

Hahaha, what did you find? Clean has 20 times. It can be seen that the clea r command is often typed incorrectly~

Permission related

The privilege management system is very intact in Linux, which is one of the reasons why Linux is rarely attacked by hackers. The three commands that are commonly used are Chmod, chown, and chgrp for changing the edge file permissions

chmod Changing file permissions is generally divided into readable. It can be written and run in 3 ways, i.e r - 4, w - 2,x - 1, - - 0,Combine all documents at the same time u – User, g – Group, o – Other, a – All For user groups, the following three commands are equivalent
king@king-desktop:~/test/test1$ chmod ugo=rwx t2
king@king-desktop:~/test/test1$ chmod a=rwx t2
king@king-desktop:~/test/test1$ chmod 777 t2
king@king-desktop:~/test/test1$ ll t2 
-rwxrwxrwx 1 king king 10240 2014-08-23 19:27 t2*

chown The user group can also be changed if all files are changed
king@king-desktop:~/test/test1$ sudo chown root t2
king@king-desktop:~/test/test1$ ll t2 
-rwxrwxrwx 1 root king 10240 2014-08-23 19:27 t2*

king@king-desktop:~/test/test1$ sudo chown root:root t2
king@king-desktop:~/test/test1$ ll t2 
-rwxrwxrwx 1 root root 10240 2014-08-23 19:27 t2*

chgrp Change the user group where the file is located
king@king-desktop:~/test/test1$ sudo chgrp root t2
king@king-desktop:~/test/test1$ ll t2 
-rwxrwxrwx 1 king root 10240 2014-08-23 19:27 t2*

Network related

ifconfig  View network configuration information

netstat  provide TCP connect, TCP,UDP Listening, process memory management, routing table information, etc. you can also view port information at the same time. Frequently used parameters –r Output routing table information, -i network interface interface Information,-a Show all socketking@king-desktop:~/test/test1$ netstat -a | grep tcp | head -n 1tcp        0      0 *:ssh                   *:*                     LISTENlsof  list open files,List the tools for opening files in the current system, usually in root Use under permission. To view the process information related to port 80: root@king-desktop:~# lsof -i:80COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAMEapache2  987     root    3u  IPv4   4423      0t0  TCP *:www (LISTEN)apache2 3899 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)apache2 3900 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)apache2 3901 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)apache2 3902 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)apache2 3903 www-data    3u  IPv4   4423      0t0  TCP *:www (LISTEN)nc 	 NetCat, a network tool, is powerful. Description of parameters available for port monitoring: - 4 IPv4 only- 6 IPv6 only- V output operation process- W agreed delay time (s)- Z scan only listening daemon does not send messages- UDP protocol default TCPking@king-desktop:~$ nc -v -w 2 -z 192.168.229.200 76-80nc: connect to 192.168.229.200 port 76 (tcp) failed: Connection refusednc: connect to 192.168.229.200 port 77 (tcp) failed: Connection refusednc: connect to 192.168.229.200 port 78 (tcp) failed: Connection refusednc: connect to 192.168.229.200 port 79 (tcp) failed: Connection refusedConnection to 192.168.229.200 80 port [tcp/www] succeeded!

————————Bash Shell detailed implementation demonstration example————————

Here are a few frequently used small examples. It's a basic memory of Shell syntax

View the specified string of a file changed by an author within a specified time

#!/bin/bash
res=`ls -l | awk '$3=="king" && $6=="2014-08-23" && $7<"22:10" && NR!=1 {print $8}'`
#echo $res
for var in $res;do
        r=`cat ./$var | grep -n -w 'test' >> mod.log`
        #echo $var
        if [ $? -ne 0 ];then
                echo "Execute CMD error!!!"
                exit 1
        fi
done
exit 0


king@king-desktop:~/Shell$ sudo sh p1.sh 
king@king-desktop:~/Shell$ cat mod.log 
1:this is a test this is a test
6:this is a test this is a test

Execute in the background to start a process, and then close the process after the operation

#!/bin/bash

DIR=/home/king/CPPFile/Socket
echo $DIR

if [ -e $DIR/server ];then
        $DIR/server & > info.log 2>&1
        if [ $? -ne 0 ];then
                echo "Execute Wrong!"
                exit 1
        fi
        sleep 2
        pid=$!
        echo $pid
        kill -9 $pid
        if [ $? -ne 0 ];then                echo "fail to kill pid"                exit 2        fifiexit 0king@king-desktop:~/CPPFile/Socket$ sudo sh test.sh /home/king/CPPFile/Socket16028

Copyright notice: This article is the original article of the blogger. Blog, can not be reproduced without consent.

Publisher: full stack programmer, stack length, please indicate the source for Reprint: https://javaforall.cn/116880.html Original link: https://javaforall.cn

Added by larsojl on Fri, 14 Jan 2022 13:19:54 +0200