Chapter Description :
This section will teach you how to use the Vim editor to write documents , configure host names , network card parameters , and the Yum repository , and be familiar with the various modes and command shortcuts.
We can finally write a simple and practical shell script by placing the Linux command into the appropriatelogical teststatement (if, for, while, case) through the Vim editor.
You can also automate your daily routine by using the AT command or by configuring the Crontab scheduled task service to automate your work more efficiently, once and for all!
Chapter directory Structure
4.1 Understanding the Vim text editor
- 4.1 Understanding the Vim text editor
- 4.1.1 Writing a simple document
- 4.1.2 Configuring host Names
- 4.1.3 Configuring NIC Information
- 4.1.4 Configuring the Yum Repository
- 4.2 Understanding shell Scripts
- 4.2.1 Compiling a simple script
- 4.2.2 parameters of the receiving user
- 4.2.3 Determining the user's parameters
- 4.3-piece test statement
- If condition statement
- For Loop statement
- While conditional statement
- Case Condition Statement
- 4.4 Scheduled Tasks Service
Configuring the Application service in a Linux system is actually modifying its configuration file (there may be multiple configuration files that contain different parameters), and it is inevitable that you will have to write documents in your daily work, which is done through a text editor.
In the popular Linux operating system will be installed by default a super-useful text editor-called "vim",vim is the VI Editor of the upgrade version .
Vim can be recognized by so many vendors and users, the reason is that in the Vim editor there are three modes- command mode , the last line mode and the editing mode , respectively, there are a number of different command shortcut key combinations, greatly improve the work efficiency, Use the habit will feel very handy. In order to be more efficient in text operations, we must first understand how the three modes of the Vim editor operate differently and switch methods.
Command mode: Control the movement of the cursor, you can delete, copy, paste and other work.
Input mode: Normal text entry.
Last-line mode: Save, exit, and set up the editing environment.
Remember that each time you run the VIM editor, the default is " Command mode ", you need to go to " input mode " and then do the work of the document, and each time the editing is completed you need to return to " command mode " before entering the " last line mode the Save or exit operation for the text.
Here for everyone summed up the most commonly used shortcut key commands, the reader as far as possible to remember, forget to check also can, as for the " input mode " There is no special skills.
Shortcut keys commonly used in the Vim Editor's command mode
|| Delete (cut) the entire line where the cursor is located.
|| Delete (cut) 5 lines starting at the cursor.
|| Copy the entire line where the cursor is located.
|| Copies the 5 lines starting at the cursor.
|| Pastes the previously deleted (DD) or copied (yy) data after the cursor.
|| Searches for the string from top to bottom in the text.
| ? string
|| Searches the text for the string from bottom to top.
|| Displays the next string that the search command navigates to.
|| Displays the previous string to which the search command is anchored.
|| Undo action from the previous step
Common commands in the last-line mode of the VIM editor
| : W
| : Q
| : q!
|| Force quit (Discard modifications to the text)
| : wq!
|| Force Save exit
| : Set Nu
|| Show line Numbers
| : Set Nonu
|| Do not display line numbers
| : command
|| Execute the command
| : integer
|| Jump to this line
Two points to note for readers:
4.1.1 Writing a simple document
1. In command mode and last-line mode, all shortcut key parameters are case-sensitive.
2. There is a colon ":" Before all the shortcut keys in the last-line mode.
Now write a document, I will each step and button as far as possible to mark out, if the command forgot to go back to the bar to see it ~ Super simple!
Please follow the steps below, you can click on either side of the picture arrow or the small dot "0" switch step.
- 1th Step: Create the document.
- 2nd step: Tap the letter "a" to enter the input mode.
- 3rd Step: Enter a line of characters.
- 4th step: Tap [ESC] to return to command mode.
- 5th step: Tap ": Wq", Save and exit.
- 6th step: View the contents of the document.
- 7th step: Edit the file.
- 8th step: Tap the letter "O" and enter into the input mode.
- 9th step: Enter a line of characters.
- 10th step: After returning to command mode, try ": Q" to exit without saving.
- 11th Step: Prompt content has been modified, cannot exit.
- 12th step: Tapping ": q!" Post-force exit is not saved.
- 13th Step: Review the document and find that the second sentence did not save.
4.1.2 Configuring host names
The host name of the Red Hat RHEL7 system is saved in the /etc/hostname file, we want to change it to "linuxprobe.com", the idea is roughly as follows:
1th Step: Use the VIM command to modify the "/etc/hostname" host name file.
2nd Step: Remove the original host name and append "linuxprobe.com".
3rd Step: Save exit and check if the modification was successful with the hostname command.
Use the VIM command to edit the host name file after the last line mode execution: wq! can be saved after the exit:
[Email protected] ~]# vim/etc/hostnamelinuxprobe.com
Use the hostname command to view the current host name:
4.1.3 Configuring NIC Information
[Email protected] ~]# hostnamelinuxprobe.com
Now that you have used the Vim editor, try to configure your Linux system network card, do not configure the network card first, you can not communicate with other machines.
In the Red Hat RHEL6 system, the NIC configuration file is prefixed with "eth", the 1th block is "eth0", the 2nd block is "eth1" and so on ...
In the Red Hat RHEL7 system, the NIC configuration file is prefixed with "Ifcfg-eno", such as "ifcfg-eno16777736".
The configuration file for the NIC is stored in the "/etc/sysconfig/network-scripts" directory.
Before you modify the configuration file, learn some keyword terms:
Network card type: TYPE = Ethernet
Address allocation mode: BOOTPROTO = static
NIC name: NAME = eno16777736
Whether to start: ONBOOT = yes
IP address: IPADDR = 192.168.10.10
Subnet mask: NETMASK = 255.255.255.0
Gateway address: GATEWAY = 192.168.10.1
DNS address: DNS1 = 192.168.10.1
The network card configuration file above represents " this is an Ethernet card device, the name is" eno16777736 "and the boot automatically start, IP address and other information needs to be specified manually ".
Before configuring the network card information, first to clarify the idea:
1th Step: First we want to switch to the "/etc/sysconfig/network-scripts" directory (this directory holds the network card configuration file).
2nd step: Use the VIM command to modify the file "ifcfg-eno16777736".
3rd Step: Write the configuration parameters, and save the exit.
4th Step: Restart the NIC command: "systemctl Restart Network".
5th step: The ping command to test the network card information is effective.
Switch to the directory where the NIC configuration file is located:
[Email protected] ~]# cd/etc/sysconfig/network-scripts/
Edit the NIC configuration file and fill in the following information:
[[email protected] network-scripts]# vim ifcfg-eno16777736
Restart the network card, the normal situation will not prompt information:
[Email protected] network-scripts]# systemctl Restart Network
Well, the successful ping pass proves that the NIC is properly configured and effective:
4.1.4 Configuring the Yum repository
[[email protected] network-scripts]# ping 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.081 ms
64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.083 ms
64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from 192.168.10.10: icmp_seq=4 ttl=64 time=0.097 ms
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.059/0.080/0.097/0.013 ms
Since the theory of the Vim editor has been solid, now it's time to configure the Yum repository- first to clarify the idea:
1th Step: First we want to switch to the "/etc/yum.repos.d/" directory (because the directory holds the configuration file for the Yum repository)
2nd step: Create and open a new file named Rhel7.repo using the Vim editor, the name can be customized, but the suffix must be repo.
3rd Step: Write the configuration parameters, and save the exit.
4th Step: Mount the disc by the path of the configuration parameter.
5th step: Write the CD mount information to the /etc/fstab file.
6th step: Use the "yum install httpd-y" command to check if the configuration is correct.
Switch to the/ETC/YUM.REPOS.D directory:
[Email protected] ~]# cd/etc/yum.repos.d/
After opening the Vim interface, tap "a" to enter into insert mode:
When the edit is complete, tap [ESC] and in the last line mode: wq! Save and exit.
[[email protected] yum.repos.d]# vim rhel7.repo
To create a directory to mount a CD:
[Email protected] yum.repos.d]# mkdir-p/media/cdrom
The warehouse is provided locally, so the disc needs to be mounted to the/media/cdrom:
[Email protected] yum.repos.d]# Mount/dev/cdrom/media/cdrommount:/dev/sr0 is write-protected, mounting read-only
Set to auto mount on boot:
[[email protected] yum.repos.d]# vim/etc/fstab/dev/cdrom/media/cdrom iso9660 defaults 0 0
To test the installation of the "httpd" service, "complete" appears to indicate that the Yum warehouse is configured correctly:
[[email protected] yum.repos.d]# yum install httpd
Loaded plugins: langpacks, product-id, subscription-manager
rhel7 | 4.1 kB 00:00
(1/2): rhel7/group_gz | 134 kB 00:00
(2/2): rhel7/primary_db | 3.4 MB 00:00
The 4th Chapter VIM editor and Shell command script