Script production in linux

Source: Internet
Author: User
Script production in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. In windows, we can often save some common commands in various text editing tools and execute a series of automatic scripts to help us execute batch processing commands. in linux, we can also use some tools to do this efficiently and easily. This article uses the built-in graphic text editor gedit under gnome to illustrate a simple example.

OK. Let's start:

1. open gedit, find "edit"> "Preferences" in the menu, and select "external tools" in the "plug-in" option. (This plug-in is not loaded by gedit by default, we need to manually load it) so that we can use various external commands and shell scripts in gedit.



(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // aolinux.simnovel.com/uploaded_images/file-772612.png'); ">

2. in the "Tools" menu, we can see "external tools" and open it. In this dialog box, we can set various scripts and tools, of course, gedit has provided us with several small scripts such as "build" and "delete trailing spaces". We can also use the "new" button to customize the script. Now we create a new one: click "new". The Tool Name and description are called "formatting text". type the "shortcut key" and press the "ALT + 2" key to select the current document, select "output" to replace the current document, and select "applicability" to all documents, because we need to replace the copied text with the format, for example, there are two full-width spaces in front of each natural Section and an empty row in a paragraph.



(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // aolinux.simnovel.com/uploaded_images/file-772690.png'); ">

3. Now let's write the processing command, and in the "command", type:

#! /Bin/sh
Sed's // g; s/^ $/g; s/^/G ';

Let's take a look at the meanings of the two lines of code and what they can do.

The first line :#! /Bin/sh: This is a script to indicate rows./bin/sh declares to the system that this script is a shell script. Of course, there are many types of scripts in linux, perl, c, php and python, but now we only need shell scripts.

Row 2: sed's // g; s ///g; s/^ $/g; s/^ // G ';, this looks terrible. Don't be afraid. After my explanation, you will clearly understand this. First, "sed" is a shell command, such as ls, mkdir, and copy, "sed" is used to process various texts, split, splice, search, replace, and wait. For details, google sed. Now we only use sed to complete a simple task: "replace ", sed is replaced by the following command format:

Sed's/content before replacement/content after replacement/g (g indicates full-text search, without g indicates the current row search )'

";" Semicolon represents splitting two command segments, that is, "Replacement of the first class; replacement of the Second Class;...". Are you clear enough? Okay, let's look back at our code. It's very simple. We will replace the full text of the current document, and replace "halfwidth space and fullwidth space" with "" to clear these two items, remove all spaces at the beginning and end of the article ("^" indicates the beginning and "$" indicates the end), and add two spaces at the beginning of each section.

4. We have completed writing our own "formatted text" script. Let's try it now. Copy an article to gedit and press ALT + 2! The article was re-formatted in our established format and looks neat!



(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // aolinux.simnovel.com/uploaded_images/file-700468.png'); ">

Although this tool is still rough and cannot process many advanced replacement commands, it is just the beginning. You can define a more powerful processing script by yourself.

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.