The 4th Chapter VIM editor and Shell command script

Source: Internet
Author: User



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.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
4.1 Understanding the Vim text editor


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


Edit
Command Role
Dd Delete (cut) the entire line where the cursor is located.
5dd Delete (cut) 5 lines starting at the cursor.
Yy Copy the entire line where the cursor is located.
5yy Copies the 5 lines starting at the cursor.
P Pastes the previously deleted (DD) or copied (yy) data after the cursor.
/String Searches for the string from top to bottom in the text.
? string Searches the text for the string from bottom to top.
N Displays the next string that the search command navigates to.
N Displays the previous string to which the search command is anchored.
U Undo action from the previous step


Common commands in the last-line mode of the VIM editor


Edit
Command Role
: W Save
: Q Exit
: 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:


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.

4.1.1 Writing a simple document


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:


[Email protected] ~]# hostnamelinuxprobe.com
4.1.3 Configuring NIC Information


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
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777736
ONBOOT=yes
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS1=192.168.10.1


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:


[[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
^C
--- 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
4.1.4 Configuring the Yum repository


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
[rhel7]
name=rhel7
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0


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
Resolving Dependencies
……………………………………………………
Complete!


The 4th Chapter VIM editor and Shell command script


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.