Vim editor and Shell command scripts

Source: Internet
Author: User

Vim editor and Shell command script vim text editor

The Vim text editor features three modes: Command mode, last line mode, and edit mode.
Command mode: Control cursor movement, can delete, copy, paste and find text.
Input mode: Normal text entry.
Last-line mode: Save, exit, and set up the editing environment.

Some shortcut keys in command mode and last-line mode:

Commands in the last-line mode:

Shell command Script

Shell Terminal interpreter similar to human and computer hardware translation officer, as the user and the Linux system internal communication media. Shell script commands work in two ways, first in front of the interaction, that is, when the user executes one command at a time, and the bulk processing is the user in advance to compile a complete shell script, the shell will execute the script many commands. You can see through the shell variable that the system is currently using the Bash interpreter as the command-line terminal by default.

Write a simple script

Using the Vim editor to write a Linux command in one go is to write the simplest script, and the lower body is to write a view of the current working path and list all the file and attribute information in the directory.


The name of the shell script can be arbitrary, and the suffix of the industry specification script file is generally. SH, in the script content:#! indicates what Shell interpreter is used to execute the script,# represents the comment information, followed by the command line. There are two ways to execute a script: the first is to run with the interpreter shell script so that the system ignores the #! in the script content Content, the second is the way to enter the full path to execute, but the system will default because of insufficient permissions to prompt the error message, you need to add permissions to the script file.

Receive User's parameters

Many variable functions are defined in the script, for example "$" corresponds to the name of the current shell script, "$#" corresponds to a total of several parameters, "$*" corresponds to the parameter values for all positions, and "$" "$" and so on for the nth position parameters.

Determine the user's parameters

To determine the parameters entered by the user, the conditional test syntax is used, and if the condition is true, 0 is returned, otherwise the other random values are returned. Test statement format: [conditional expression] There is a space on both sides , the conditional judgment statement according to the test object can be divided into file test, logical test, Integer value comparison and string comparison.

File test

The file test is based on the specified criteria to determine whether the file exists or whether the permissions are satisfied, the specific parameters are:


Through $? Get the return value of the previous command

Logic test

The logic test is to determine whether the condition given by the user is true or false, so that the conditional test statement and the logical statement can be used together to achieve a more advanced method of use, for example, in the shell terminal, the logical "and" symbol is && This means that the following command will not be executed until the previous command has been successfully executed.

The "or" symbol is "| |", which represents the previous command execution failed before executing the subsequent command

Integer value Comparison

The integer comparison operator is a numeric only test operation, cannot manipulate numbers with strings, files, and so on, and can not be taken for granted by the daily life of the equal sign, greater than, less than the number, etc., because the equal sign and is the assignment of the command character conflict, The greater than and less than numbers are conflicting with the output redirection command and the input redirection command. Although sometimes it happens to be successful, there is a general error in the script that follows, so be sure to use the canonical integer comparison operator:

string comparison

String comparisons are actions that determine whether a test string is null, or whether two strings are the same, and are often used to determine if a variable is undefined (that is, the content is null), common parameters:

Flow control Statement If condition Test statement single branch structure (IF-THEN-FI)


Shell script file: Create a hahaha file if the hahaha file does not exist in the Linux directory
Test results:

Dual branch structure (IF-THEN-ELSE-IF) multi-branch (IF-THEN-ELIF-THEN-ELSE-FI)


If the score is greater than 85 less than 100, then output excellent and so on ...
Test results:

For Condition Loop statement

Format:
For variable name in value list
Do
Command sequence
Done

Here is an example of creating a user in bulk:

The read command on line 1th reads the value of the client input as the variable passwd, and the-p parameter is for the OUTPUT hint statement.
The ID of line 6th $UNAME command to see if the user is present,&> indicates that the output information is redirected to/dev/null,/dev/regardless of the correct or incorrect result of the preceding command output Null is a file called a Linux black hole, which redirects the output information to this file as if it were deleted (a garbage bin without recycling), keeping the User's screen window concise.
The 7th line indicates that the user exists if the ID $UNAME executed successfully.
11th, Line 12 is the command to add the user's command and set the password.
Note : the addition of variables to the Linux system by default is the addition of strings, for example:

To implement the value addition of two variables, you need to use the expr command to convert the string to a number

While condition loop

Format:
While condition test action
Do
Command sequence
Done

Use while to write a guessing price script

Test results

Case Condition Test

Format:
**case Variable value in
Mode 1)
Command sequence 1
;;
Mode 2)
Command Sequence 2
;;
*)
Default command sequence
esac**
Test:

Scheduling a task service to schedule a task once (using the AT command line)

Example:

Use ATRM + numbering to delete scheduled tasks with corresponding numbers

Recurring Scheduled Tasks

You can create tasks by using the Crond service that is enabled by default on your Linux system. The command to create and edit a scheduled task is crontab-e, the command to view the currently scheduled task is crontab-l, the command to delete a scheduled task is crontab-r, and if you use an administrator login system, you can use crontab-u To edit other people's tasks. formula: Minute, time, day, month, week .

It should be noted that, in addition to using commas to represent multiple time periods, such as "8,9,12" for August, September, and December, you can also use a minus sign to indicate a continuous period of time (for example, the field "Day" is the value of "12-15", that is, 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).
In addition, in the Crond service scheduling parameters, the command must be used absolute path to write, if you do not know the absolute path, you can use the whereis command to query, the absolute path is the bold portion of the output information.

Vim editor and Shell command scripts

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.