Linux should learn this way--VIM editor

4.1 Vim text editor

Command mode: Control cursor movement, copy, paste, delete and search text.

Input mode: normal text input.

Last line mode: save or exit the document, and set the editing environment.

Chapter 4 Vim Editor and Shell Command Script.  Chapter 4 Vim Editor and Shell Command Script.

[root@myserver ~]# vim new.txt
[root@myserver ~]# cat new.txt 
This is a test 

Vim has three visual modes.

When using Vim to edit text, the visual mode is very useful for identifying the text block to be manipulated. Vim’s visual mode has three modes: character, line and block. The keys to enter each mode are:

Character mode: v (lower case)

 Line mode: V (uppercase)

 Block mode: Ctrl+v

命令	用途
Esc	退回到普通模式
v	激活面向字符的可视模式
V	激活面向行的可视模式
Ctrl + v	激活面向列块的可视模式
gv	重选上次的高亮选区
o	切换到高亮选区的活动端

(1) Press the v key to enter the visual character mode. The word VISUAL will appear at the bottom of the screen. Use the arrows to highlight the desired text. You can use other navigation commands, such as w to highlight to the beginning of the next word, and $ to include the rest of the line. After the text is highlighted, press d to delete the text. If you delete too much or not enough, press u to undo and start again. Move the cursor to the new location and press p to paste the text. You can also highlight a piece of text to be replaced.

w select some words, then d, then u cancel the effect as shown in the figure: 

(2) Place the cursor anywhere on the first or last line of the text to be operated. Press V to enter line mode. The word VISUAL LINE will appear at the bottom of the screen. Use navigation commands (up and down arrow keys) to highlight multiple lines of text. After highlighting the desired text, use commands to manipulate it. Press d to delete, then move the cursor to a new position, press p to paste the text, the result is as shown below

 

(3) Press Ctrl+v to enter the visualization block mode. The word VISUAL BLOCK will appear at the bottom of the screen. Hit the keyboard up and down keys to highlight the line to be annotated first, then click the big I, then annotate and then press the esc key, the effect is as shown in the figure below:

 

 

 4.1.2 Configure the host name

Step 1 : Use the Vim editor to modify the "/etc/hostname" hostname file.

Step 2 : Delete the original host name and append "linuxprobe.com". Note that after using the Vim editor to modify the host name file, you must execute the :wq! command in the last line mode to save and exit the document.

Step 3 : Save and exit the document, and then use the hostname command to check whether the modification is successful

或者hostnamectl set-hostname  linuxprobe

[root@myserver c]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.88 myserver
[root@myserver c]# 

4.1.3 Configure network card information

Step 1 : First switch to the /etc/sysconfig/network-scripts directory (store the configuration file of the network card).

Step 2 : Use the Vim editor to modify the network card file ifcfg-eno160, write the following configuration parameters one by one and save and exit. Since the hardware and architecture of each device are different, please use the ifconfig command to confirm the default name of the respective network card

设备类型:TYPE=Ethernet

地址分配模式:BOOTPROTO=static

网卡名称:NAME=eno160

是否启动:ONBOOT=yes

IP地址:IPADDR=192.168.1.88

子网掩码:NETMASK=255.255.255.0

网关地址:GATEWAY=192.168.1.2

DNS地址:DNS1=114.114.114.114

Step 3 : Restart the network service and test whether the network is connected

[root@myserver c]# nmcli connection  reload
[root@myserver c]# ping www.baidu.com
PING www.wshifen.com (103.235.46.39) 56(84) bytes of data.
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=5 ttl=128 time=372 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=6 ttl=128 time=371 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=7 ttl=128 time=371 ms
^C
--- www.wshifen.com ping statistics ---
7 packets transmitted, 3 received, 57.1429% packet loss, time 128ms
rtt min/avg/max/mdev = 371.288/371.707/372.365/0.685 ms
[root@myserver c]# 

4.1.4 Configure Yum Warehouse

Step 1 : Enter the /etc/yum.repos.d/ directory (because this directory stores the configuration files of the Yum software warehouse).

Step 2 : Use the Vim editor to create a new configuration file named rhel7.repo (the file name can be arbitrary, but the suffix must be .repo), write the following bold configuration parameters one by one and save and exit (do not write the following Chinese annotation)

Step 3 : Mount the CD according to the path of the configuration parameters, and write the CD mounting information into the /etc/fstab file.

Step 4 : Use the "yum install httpd -y" command to check whether the Yum software repository is available.

Configure local yum source

[root@myserver c]# cat /etc/yum.repos.d/redhat.repo 
#
# Certificate-Based Repositories
# Managed by (rhsm) subscription-manager
#
# *** This file is auto-generated.  Changes made here will be over-written. ***
# *** Use "subscription-manager repo-override --help" if you wish to make changes. ***
#
# If this file is empty and this system is subscribed consider
# a "yum repolist" to refresh available repos
#
[localREPO]
name=localhost8
baseurl=file:///media/BaseOS
enable=1
gpgcheck=0

[localREPO_APP]
name=localhost8_app
baseurl=file:///media/AppStream
enable=1
gpgcheck=0
[root@myserver c]# 

Test the validity of the yum source

[root@myserver c]# yum install -y httpd
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Extra Packages for Enterprise Linux Modular 8 - x86_64                                                                      1.4 kB/s | 9.1 kB     00:06    
Extra Packages for Enterprise Linux 8 - x86_64                                                                              4.3 kB/s | 7.6 kB     00:01    
localhost8                                                                                                                  2.7 MB/s | 2.7 kB     00:00    
localhost8_app                                                                                                              3.1 MB/s | 3.2 kB     00:00    
Dependencies resolved.
============================================================================================================================================================
 Package                              Arch                     Version                                                Repository                       Size
============================================================================================================================================================
Installing:
 httpd                                x86_64                   2.4.37-10.module+el8+2764+7127e69e                     localREPO_APP                   1.4 M
Installing dependencies:
 redhat-logos-httpd                   noarch                   80.7-1.el8                                             localREPO                        25 k
 apr                                  x86_64                   1.6.3-9.el8                                            localREPO_APP                   125 k
 apr-util                             x86_64                   1.6.1-6.el8                                            localREPO_APP                   105 k
 httpd-filesystem                     noarch                   2.4.37-10.module+el8+2764+7127e69e                     localREPO_APP                    34 k
 httpd-tools                          x86_64                   2.4.37-10.module+el8+2764+7127e69e                     localREPO_APP                   101 k
 mod_http2                            x86_64                   1.11.3-1.module+el8+2443+605475b7                      localREPO_APP                   156 k
Installing weak dependencies:
 apr-util-bdb                         x86_64                   1.6.1-6.el8                                            localREPO_APP                    25 k
 apr-util-openssl                     x86_64                   1.6.1-6.el8                                            localREPO_APP                    27 k
Enabling module streams:
 httpd                                                         2.4                                                                                         

Transaction Summary
============================================================================================================================================================
Install  9 Packages

Total size: 2.0 M
Installed size: 5.4 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                    1/1 
  Installing       : apr-1.6.3-9.el8.x86_64                                                                                                             1/9 
  Running scriptlet: apr-1.6.3-9.el8.x86_64                                                                                                             1/9 
  Installing       : apr-util-bdb-1.6.1-6.el8.x86_64                                                                                                    2/9 
  Installing       : apr-util-openssl-1.6.1-6.el8.x86_64                                                                                                3/9 
  Installing       : apr-util-1.6.1-6.el8.x86_64                                                                                                        4/9 
  Running scriptlet: apr-util-1.6.1-6.el8.x86_64                                                                                                        4/9 
  Installing       : httpd-tools-2.4.37-10.module+el8+2764+7127e69e.x86_64                                                                              5/9 
  Running scriptlet: httpd-filesystem-2.4.37-10.module+el8+2764+7127e69e.noarch                                                                         6/9 
  Installing       : httpd-filesystem-2.4.37-10.module+el8+2764+7127e69e.noarch                                                                         6/9 
  Installing       : redhat-logos-httpd-80.7-1.el8.noarch                                                                                               7/9 
  Installing       : mod_http2-1.11.3-1.module+el8+2443+605475b7.x86_64                                                                                 8/9 
  Installing       : httpd-2.4.37-10.module+el8+2764+7127e69e.x86_64                                                                                    9/9 
  Running scriptlet: httpd-2.4.37-10.module+el8+2764+7127e69e.x86_64                                                                                    9/9 
  Verifying        : redhat-logos-httpd-80.7-1.el8.noarch                                                                                               1/9 
  Verifying        : apr-1.6.3-9.el8.x86_64                                                                                                             2/9 
  Verifying        : apr-util-1.6.1-6.el8.x86_64                                                                                                        3/9 
  Verifying        : apr-util-bdb-1.6.1-6.el8.x86_64                                                                                                    4/9 
  Verifying        : apr-util-openssl-1.6.1-6.el8.x86_64                                                                                                5/9 
  Verifying        : httpd-2.4.37-10.module+el8+2764+7127e69e.x86_64                                                                                    6/9 
  Verifying        : httpd-filesystem-2.4.37-10.module+el8+2764+7127e69e.noarch                                                                         7/9 
  Verifying        : httpd-tools-2.4.37-10.module+el8+2764+7127e69e.x86_64                                                                              8/9 
  Verifying        : mod_http2-1.11.3-1.module+el8+2443+605475b7.x86_64                                                                                 9/9 
Installed products updated.

Installed:
  httpd-2.4.37-10.module+el8+2764+7127e69e.x86_64                                 apr-util-bdb-1.6.1-6.el8.x86_64                                           
  apr-util-openssl-1.6.1-6.el8.x86_64                                             redhat-logos-httpd-80.7-1.el8.noarch                                      
  apr-1.6.3-9.el8.x86_64                                                          apr-util-1.6.1-6.el8.x86_64                                               
  httpd-filesystem-2.4.37-10.module+el8+2764+7127e69e.noarch                      httpd-tools-2.4.37-10.module+el8+2764+7127e69e.x86_64                     
  mod_http2-1.11.3-1.module+el8+2443+605475b7.x86_64                             

Complete!
[root@myserver c]# 

4.2 Shell script

交互式(Interactive):用户每输入一条命令就立即执行。

批处理(Batch):由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中诸多的命令

 If you want to view the current htop information, use the command to execute as follows (htop is installed using epel-release source)

Receive user parameters

[root@myserver c]# cat example.sh 
#!/bin/bash
#output message
echo "当前脚本名称为$0"
echo "总共有$#个参数,分别是$*。"
echo "第1个参数为$1,第5个为$5。"
[root@myserver c]# sh example.sh 1 2 3 4 5 
当前脚本名称为example.sh
总共有5个参数,分别是1 2 3 4 5。
第1个参数为1,第5个为5。
[root@myserver c]# 

Judge the user's parameters

[root@myserver c]# [ -d /etc/fstab ]
[root@myserver c]# echo $?
1
[root@myserver c]# [ -f /etc/fstab ]
[root@myserver c]# echo $?
0
[root@myserver c]# [ -e /dev/cdrom ] && echo "Exist"
Exist
[root@myserver c]# [ $USER = docker ] || echo "user"
user
[root@myserver c]# 

Integer comparison operators are only operations on numbers. You can't operate numbers with strings, files, etc., and you can't use equal signs, greater than signs, and less than signs in daily life for judgment. Because the equal sign conflicts with the assignment command character, the greater than sign and the less than sign conflict with the output redirection command character and the input redirection command character respectively. So be sure to use the standard integer comparison operators to operate

[root@myserver c]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1966        1095         223           8         646         683
Swap:          2095         261        1834
[root@myserver c]# free -m | grep Mem:
Mem:           1966        1095         223           8         646         683
[root@myserver c]#  free -m | grep Mem: | awk '{print $4}'
223
[root@myserver c]# FreeMem=`free -m | grep Mem: | awk '{print $4}'`
[root@myserver c]#  echo $FreeMem 
223
[root@myserver c]#  [ $FreeMem -lt 1024 ] && echo "Insufficient Memory"
Insufficient Memory
[root@myserver c]# 

 

4.3 Flow Control Statement

if conditional test statement

The if conditional test statement allows the script to automatically execute the corresponding command according to the actual situation. From a technical point of view, if statements are divided into single-branch structure, double-branch structure, and multi-branch structure; its complexity increases with flexibility.

The single-branch structure of an if conditional statement is composed of if, then, and fi keywords, and the preset command is executed only after the condition is satisfied, which is equivalent to the colloquial "if... then...". The single-branch if statement belongs to the simplest kind of conditional judgment structure. The syntax format is shown in Figure 4-17.
Chapter 4 Vim Editor and Shell Command Script.  Chapter 4 Vim Editor and Shell Command Script.

The script mainly uses the ping command to test the network connectivity with the opposite host. The -c parameter is required to specify the number of attempts, the -i parameter is used to define the sending interval of each data packet, and the -W parameter is used to define the waiting timeout time

 In the Linux system, read is a command used to read user input information, which can assign the received user input information to the specified variable later, and the -p parameter is used to display certain prompt information to the user. In the following script example, only when the score entered by the user is greater than or equal to 85 points and less than or equal to 100 points, the word "Excellent" will be output; if the score does not meet the condition (that is, the match is unsuccessful), continue to determine whether the score is greater than or equal to 70 points And less than or equal to 84 points, if yes, output the word Pass; if it fails both times (that is, the two matching operations have failed), output the word Fail

[root@myserver ~]# cat chkscore.sh 
#!/bin/bash
read -p "Enter your score(0-100):" GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then
	echo "$GRADE is Excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
	echo "$GRADE is Pass"
else
	echo "$GRADE is Fail" 
fi
[root@myserver ~]# sh chkscore.sh
Enter your score(0-100):80
80 is Pass
[root@myserver ~]# sh chkscore.sh
Enter your score(0-100):60
60 is Fail
[root@myserver ~]# sh chkscore.sh
Enter your score(0-100):70
70 is Pass
[root@myserver ~]# 

 

Guess you like

Origin blog.csdn.net/yanghuadong_1992/article/details/112443896