Linux Learning Diary-shell script Basics

Source: Internet
Author: User
Tags stdin

In the daily maintenance of Linux, Shell script is one of the essential daily management, its role is to solve the automatic maintenance of the server, reduce the workload of the Administrator, if you want to improve the speed of writing shell scripts, you must be proficient in the VI Editor of the various shortcut keys.

"What is a shell"?
Shell Script,shellScriptwith theWindows/Dossimilar to the batch processing, that is, with a variety of commands pre-placed into a file, convenient one-time execution of aProgram Files, mainly for the convenience of the administrator to set up or manage. But it's more powerful than batch processing under Windows, and it's better than using other programmingProgram editingprogram is more efficient, after all, it uses the commands under Linux/unix.

"My first Script"

First create a VIM script file, this example creates a test1 test file that ends with. sh

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/61/wKiom1gReSyThFYqAAAhot4yXYc614.png "title=" 1.png " alt= "Wkiom1gresythfyqaaahot4yxyc614.png"/>

Access the following content within the file

where echo outputs the comment content to the screen, making it easier for the user to read

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/61/wKiom1gRelqDcEVQAAAzTARqIBU103.png "title=" 2.png " alt= "Wkiom1grelqdcevqaaaztarqibu103.png"/> Add permissions and execute:

Common ways to execute: #sh script path and name

#source Script Path and name

#直接输入脚本路径或名称 (need to add execute permission to script)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/89/61/wKiom1gRep6SNRERAABIDNkCFxc722.png "title=" 3.png " alt= "Wkiom1grep6snreraabidnkcfxc722.png"/> "redirect Operation"

User through the operating system out of the process of information, including the following types of interactive device files

    • Standard input (STDIN): The default device is the keyboard, the file number is 0, and the command reads the input data required during execution from the standard input file.

    • Standard output (STDOUT): The default device is the monitor, the file number is 1, and the command sends the executed output to the standard output file.

    • Standard error (STDERR): The default device is the monitor, the file number is 2, the command sends various error messages during execution to the standard error file

(1) REDIRECT output: Saves the command's normal output to the specified file instead of displaying it on the monitor

">" indicates redirected output (overwritten if the file exists)

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/5F/wKioL1gRf1XzeqgkAAAvT42oNd0572.png "title=" 4.png " alt= "Wkiol1grf1xzeqgkaaavt42ond0572.png"/>>> indicates redirection append

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/89/62/wKiom1gRf7GCLcMMAAA3I0w2OGo430.png "title=" 5.png " alt= "Wkiom1grf7gclcmmaaa3i0w2ogo430.png"/> (2) redirect input: The way to receive input from the command has the default keyboard changed to the specified file instead of waiting for input from the keyboard, redirecting the input using the "<" operator.

Use Vim to add 123456 to the Pass.txt file

Then execute passwd--stdin Jerry < pass.txt

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/5F/wKioL1gRgmXCqYCQAAA3IjqqtKo609.png "title=" 6.png " alt= "Wkiol1grgmxcqycqaaa3ijqqtko609.png"/>

(3) Error redirection: Saves the error message that occurs during the execution of the command to the specified file instead of directly on the screen; error redirection uses the "2>" operator, where 2 refers to the number of the error file

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/89/5F/wKioL1gRgz6zAEWVAABCCvRDG9E139.png "title=" 7.png " alt= "Wkiol1grgz6zaewvaabccvrdg9e139.png"/>

(4) Use the "&>" operator to save two types of output information to the Unified directory. For example, in the automation script that compiles the source package, to ignore the operation procedure information such as Make,make install, you can direct it to an empty file/dev/null

Create a script file: Vim httpd_install.sh

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/89/62/wKiom1gRhYvQaYpCAAA9RXaOeio197.png "title=" 8.png " alt= "Wkiom1grhyvqaypcaaa9rxaoeio197.png"/>

Add permissions and execute: chmod +x httpd_install.sh

"Pipeline Operations"

Pipeline operations provide a mechanism for collaborative work between different commands, located in the pipe symbol "|" The result of the command output on the left is the input (processing object) of the right command, and multiple pipelines can be used in the same line of commands.

(1) Results prior to extraction

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/62/wKiom1gRhxzAUTQqAAA5dfi6rXs968.png "title=" 9.png " alt= "Wkiom1grhxzautqqaaa5dfi6rxs968.png"/>

(2) Effect after extraction, only show user name and Shell column

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/89/62/wKiom1gRh6azU0kJAAAyOsHQNwc881.png "title=" 10.png "alt=" Wkiom1grh6azu0kjaaayoshqnwc881.png "/>

(3) In the example display disk used percentage

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/89/5F/wKioL1gRiKWhubrPAABfGgxNbt8327.png "title=" 11.png "alt=" Wkiol1grikwhubrpaabfggxnbt8327.png "/>

This article is from the "Dark Cloud Technology" blog, please be sure to keep this source http://wuyunkeji.blog.51cto.com/12028005/1866261

Linux Learning Diary-shell script Basics

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.