Vim editor and Shell command script

Source: Internet
Author: User

1.Vim Text Editor

A text editor installed by default on all current Linux systems, with three modes:

1. Command mode: Control cursor movement, can copy, paste, delete, find and so on text.
2. Input mode: normal text input.
3. Last-line mode: Save or exit the document, and set the editing environment.

The default is the command mode, you need to switch to the input mode and then document writing, you need to return to the command mode, then enter the last line mode, the document is saved or exited. Cannot switch directly from input mode to last row mode.

Common commands in Vim:

    • DD: Delete (cut) the cursor in the row
    • 5DD: Delete (cut) 5 lines starting at the cursor
    • YY: The copy cursor is in the row
    • 5yy: Copy 5 lines starting at the cursor
    • N: Displays the next character to which the search command is anchored
    • N: Displays the previous character to which the search command is anchored
    • U: Undo Previous Action
    • P: Paste before deleting (dd) or copying (yy) data to the cursor

Commands available in the last-line mode

    • : w: Save
    • : Q: Exit
    • : q! : Force quit (discard modifications to the document)
    • : wq! : Force Save to exit
    • : Set Nu: Display line number
    • : Set Nonu: line number not displayed
    • : Command: Execute the command
    • : integer: Jumps to the row
    • : S/one/two: Replaces the first one in the row where the current cursor is located
    • : s/one/two/g: Replaces all one of the lines in which the current cursor is located
    • :%s/one/two/g: Replace all of the one in the full text with
    • ? String: Search for the string from bottom to top in text
    • /string: Search for the string from top to bottom in the text
1.1 Writing a simple document

First open the document Practice.txt, and if the document does not exist, create a temporary input file.

Here the default is the command mode, you can not freely enter text, you need to press A, I or O key to switch to the input mode, where a and I are the cursor after a bit and the current position of the cursor switch to input mode, O is under the cursor and then create a blank line, at this time hit a key into the input mode.

You can then enter text.

If you want to save, you must first press the ESC key to exit input mode, return to command mode, and then enter: wq! Switch to the last line mode to complete the save exit operation.

Then you can view the contents of the document.

If you want to append content after the original document, it is more convenient to use the O key when switching from command mode to input mode.

1.2 Configuring host Names

In order to find a particular host in the local area network, or to distinguish the host, in addition to the IP address, but also to configure a host name for the host, between the host can be accessed through this similar to the name of the domain names to each other. In Linux
System, the host name is mostly saved in the/etc/hostname file, the next step is to modify the contents of the/etc/hostname file to "linuxprobe.com", the following steps.

1th Step: Use the VIM editor to modify the "/etc/hostname" host name file.

2nd Step: Delete the original host name and append "Linuxprobe.com". Note that after modifying the host name file with the Vim editor, execute in the last line mode: The wq! command to save and exit the document.

3rd Step: Save and exit the document, and then use the hostname command to check if the modification was successful.

The hostname command is used to view the current host name, but sometimes changes to the host name are not immediately synchronized to the system, so if you find that the original host name is displayed after the modification is complete, you can restart the virtual machine and then view it again.

1.3 Configuring NIC Information

The correct configuration of the IP address of the network card is the precondition of whether the two servers can communicate with each other. In a Linux system, everything is a file, so the job of configuring network services is to edit the NIC configuration file

In Rhel 7, the NIC configuration file prefix starts with IFCFG, and the NIC name together makes up the name of the NIC configuration file, such as Ifcfg-eno16777736,ifconfig-ens32

Configure the network card device to boot, and the IP address, subnet, gateway and other information is manually specified, the steps are as follows:

    1. First switch to the/etc/sysconfig/network-scripts directory (the configuration file that holds the NIC)

    2. Use the Vim editor to modify the NIC file Ifcfg-ens32, write the following configuration parameters and save the exit. Because the hardware and architecture of each device is different, you can use the Ifconfig command to confirm the default name of the NIC

Type=ethernet
Bootproto=static
Name=ens32
Onboot=yes
ipaddr=192.168.10.10
netmask=255.255.255.0
gateway=192.168.10.1
dns1=192.168.10.1

    1. Restart Network Service and test network connectivity

Perform a command to restart the NIC device (under normal circumstances, without prompting), and then ping to test the network connectivity. Because the ping command does not automatically terminate in a Linux system, it is necessary to manually press the CTRL-C key to force the end of the process.

1.4 Configuring the Yum Software repository

The Yum Software Warehouse is designed to further simplify the difficulty of RPM management software and to automatically analyze the technologies required for packages and their dependencies. You can think of Yum as a huge repository of software that stores almost all of the common
Tool, and just say the required package name, the system will automatically take care of everything for you.

The approximate steps to build and configure the Yum repositories are as follows.

1th Step: Enter into the/etc/yum.repos.d/directory (because the directory holds the configuration file for the Yum repository).

2nd step: Use the VIM editor to create a new profile named Rhel7.repo (the file name is optional, but the suffix must be. Repo), write down the following configuration parameters and save the exit (do not write the subsequent Chinese comments).

[Rhel-media]: Yum repository Unique identifier to avoid conflicts with other warehouses.

Name=linuxprobe:yum Software warehouse Name description, easy to identify the use of the warehouse.

Baseurl=file:///media/cdrom: The available methods include FTP (ftp://. ), HTTP (/. ), local (file:///. )。

Enabled=1: Sets whether this source is available, 1 is available, and 0 is disabled.

Gpgcheck=1: Sets whether this source verifies the file, 1 for the checksum, and 0 for the non-checksum.

Gpgkey=file:///media/cdrom/rpm-gpg-key-redhat-release: If the above parameter turns on the checksum, then specify the public key file address.

3rd Step: Mount the disc according to the path of the configuration parameter and write the CD mount information to the/etc/fstab file.

4th Step: Use the "Yum install httpd-y" command to check if the Yum repositories are available.

2. Writing shell scripts

Shell script commands work in two ways: interactive and batch.

Interactive (Interactive): The user executes each command as soon as it is entered.

Batch: A complete shell script is compiled by the user in advance, and the shell executes many commands in the script at once.

Viewing the shell variable Discovery system already uses bash as the terminal interpreter for the command line by default.

2.1 Writing a simple script

Create the example.sh and write the following:

The shell script file can be arbitrarily named, but in order to avoid being mistaken for ordinary files, it is usually named after the. sh suffix.

There are three different elements in the above file:

The first line of the script declaration (#! Tells the system which shell interpreter to use to execute the script;

The second line of comment information (#) is a description of the script function and some commands;

Line third to fourth is an executable statement, the Linux command.

The results of the implementation are as follows:

In addition to running shell scripts directly through the Bash command, you can also execute them by entering the full path. This way by default due to insufficient permissions to error, at this time only to increase the execution permissions for the script file.

2.2 Parameters of the receiving user

The script commands above are too rigid to be able to receive parameters from the user, using variables that are built into Linux to accept parameters.

$: The name of the current shell script program
$#: A total of several parameters
$*: Parameter values for all locations
$?: Displays the execution return value of the last command
$, $, $ ... the parameter values for the 1th, 2nd, and 3rd positions are respectively, and so on.

Examle.sh:

Perform

2.3 Determining the user's parameters

The conditional test syntax in the shell script can determine whether the expression is true or not, and returns the other random number if it is set to return the number 0. Conditional test Statement format: [conditional expression], there should be a space on both sides of the conditional expression.

According to the test object, conditional test statement can be divided into four kinds: file test statement, logical test statement, integer value comparison statement, string comparison statement.

A file test is an operator that uses a specified condition to determine whether a file exists or if the permission satisfies the condition. The operator of the file test and its effect:

-D: Test whether the file is a directory type
-E: Test whether the file exists
-F: Determine if it is a generic file
-R: Tests whether the current user has permission to read
-W: Tests whether the current user has permission to write
-X: Tests whether the current user has permission to execute

Determine if/etc/fstab is a directory-type file, the return value is not 0, so it is not a directory-type file.

The return value is 0, so/etc/fstab is a generic file.

&& is the logical "and" operator in the shell, indicating that the preceding command executes successfully before it executes the command following it.

|| Represents the logical OR, the preceding command execution fails to execute later.

There is also a logical statement is "non", the symbol is an exclamation point (!) ), which indicates the inverse value of the judgment in the condition test.

The integer comparison operation is only for numbers, cannot manipulate numbers with strings, files, and so on, and does not take for granted the greater than and less than and equal signs in daily life. Because the equals sign conflicts with an assignment command, greater than less than sign conflicts with the output and input redirection symbols. The canonical integer comparison operator is as follows:

-eq: Is it equal to
-ne: is not equal to
-GT: Is it greater than
-LT: Is it less than
-le: is less than or equal to
-ge: is greater than or equal to

A string comparison statement is used to determine whether the test string is empty, or whether two strings are the same. The operators are as follows:

=: compare strings are the same
! =: Compare Strings if different
-Z: Determines whether the string contents are empty

The string variable is empty, that is, the variable is not defined.

3. Process Control Statement 3.1 if condition test statement

The single branch structure of the ①IF conditional statement consists of the IF, then, and fi keywords, and the syntax format:

If condition test action
then command sequence
Fi

This example is the creation of a file that is completed by a single-spoke if statement.

②if's dual-branch structure consists of the IF, then, else, fi keywords, and the syntax format:

If condition test action
then command sequence 1
else Command sequence 2
Fi

The ping command tests the network connectivity with the other host,-c specifies the number of attempts,-I defines the send interval for each packet, and-W defines the wait time-out.

③IF Multi-branch structure consists of the IF, then, else, elif, fi keywords, syntax format:

If condition test action
then command sequence 1
elif Condition Test Action 2
then command sequence 2
else Command Sequence 3
Fi

The read command is used to read the user input information and can assign the received user input information to the following specified variable, and-p indicates that a certain hint is displayed to the user.

3.2 for Condition Loop

The For loop allows the script to read multiple messages at once, and then manipulate the information one at a time, in a syntax format:

For variable name in value list
DO command sequence
Done

3.3 While conditional loop statement

Syntax format:

While condition test action
DO command sequence
Done

Here is a script that guesses the size of a value through a while loop guess.sh

The $RANDOM variable represents a random number (range 0~32767), which takes it to 1000 for the remainder operation. This script allows the user to interact with the program, resulting in the correct random number, with the following results:

3.4 Case Condition Test statement

Syntax structure:

Case Variable value in
Mode 1)
Command sequence 1
;;
Mode 1)
Command Sequence 2
;;
......
*)
Default command sequence
Esac

In the example of the while loop, there is a flaw, that is, the input can only be a number, if the input non-digital script will crash. At this point, you can solve this problem by combining a case with a wildcard character.

4. Scheduled Task Service Program

Scheduled tasks are divided into one-time scheduled tasks and long-term scheduled tasks, which can be understood as follows:

One-time scheduled task: 11:30 P.M. Open Web services.

Long-term Scheduled tasks: 3:25 A.M. every Monday to pack the/home/wwwroot directory back to Backup.tar.gz

As the name implies, one-time scheduled tasks are performed only once and are generally used to meet temporary work requirements. We can use the AT command to achieve this function, just write the "at Time" form. If you want to see a one-time scheduled task that has been set up but not yet executed, you can use the "at-l" command, or "ATRM task number" If you want to remove it. When you use the AT command to set up a one-time scheduled task, the interactive method is used by default. For example, use the following command to set the system to automatically restart the site service at 23:30.

If you want the Linux system to perform certain tasks periodically and regularly, the Crond service that is enabled by default on Linux systems is simply perfect. The command to create and edit a scheduled task is "Crontab-e", and the command to view the currently scheduled task is "Crontab-l", and the command to delete a scheduled task is "Crontab-r". In addition, if you are a system that is logged on as an administrator, you can also add the-u parameter to the crontab command to edit other people's scheduled tasks.

Use the Crond service to set the parameter format for the task: minute, time, day, month, and week commands.

Suppose that at 3:25 A.M. per week, three or five, you need to use the TAR command to package a Web site's data directory as a backup file. We can use the CRONTAB-E command to create a scheduled task. To create a scheduled task for yourself without using the-u parameter, the specific implementation effect parameters are as shown in the crontab-l command result:

It should be noted that, in addition to using commas (,) to represent multiple time periods, for example, "8,9,12" represents August, September, and December. You can also use a minus sign (-) to indicate a continuous period of time (for example, the field "Day" has a value of "12-15", which means 12-15 days per month). and using Division sign (/) to indicate when a task is performed (for example, "*/2" means to perform a task every 2 minutes).

If you need a command statement that contains multiple scheduled tasks in the Crond service, write only one per line. For example, we add another scheduled task that automatically empties all files in the/tmp directory every Monday to Friday 1 o'clock in the morning. In particular, it is important to note that in the Crond service's scheduled task parameters, all commands must be written in an absolute path, and if you do not know the absolute path, use the Whereis command to query the RM command path to the bold portion of the output information below.

The "Minutes" field in a scheduled task must have a numeric value, absolutely not be empty or an * number, and the day and week fields cannot be used at the same time, or there will be a conflict.

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.