Beginner's "Linux Programming" learning-shell Script

Source: Internet
Author: User
Tags echo command

1. Understanding Shell Scriptshell Script is a "program" that uses the shell's function abbreviation, which uses plain text files to write some shell syntax and commands (including external commands) in it, with regular expressions, pipeline commands and data flow redirection, To achieve the purpose we want to deal with.
Shell script has a wide range of applications: (1) important basis for automation management (2) Tracking and management system important work (3) Simple intrusion detection function (4) Continuous command single (5) Simple data processing (6) support cross-platform
So, shell script used in the system management is a good tool, but it also has some shortcomings, in the processing of a large number of numerical calculations, the speed is slow, occupy more CPU resources, will cause the host resource allocation is bad.
2. The shell script program is written for the shell script program, using the Editing tool vim, which is a very useful text editing command. First, use the Bash command to create a script program
[[email protected] shellscript]$ vim helloworld.sh//in the Open Text, edit the program, helloworld.sh#!/bin/bash#program:# this is       my First shell script program. It'll Show "Hello world!" on# the screen       . Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathecho-e "Hello World! \a \ n "Exit 0
Program Content parsing: (1) The first line #!/bin/bash is the shell name used to declare the script, and each shell script content must be declared on the first line. That is, we use the Bash command, we must declare this file to use bash syntax, (2) except for the first line, the statement beginning with # is the function of annotations, which is similar to the ordinary C program comments, (3) The Declaration of the main environment variables, bird brother suggested that Path and Lang are the most important, if used, it is recommended to set some important environment variables, so that the program can be executed, the direct execution of external commands, do not have to write absolute path, (4) The main program content, in this document only echo this line, (5) Inform the execution results, using the Exit command, Let the program interrupt, and return a value to the system, the value of 0 for success, non-0 value is the error; 3. The program's compilation runs in two ways for Shell script: (1) Execute with the SH command,
[Email protected] shellscript]$ sh helloworld.sh Hello world!  

(2) using chmod to change file permissions, directly with./helloworld.sh execution
[Email protected] shellscript]$ chmod +x helloworld.sh [[email protected] shellscript]$./helloworld.sh Hello world!  

We see that both of these methods can successfully execute scripts and helloworld.sh. 4. Two ways to compile (1) Execute the script in a direct execution: run in a child process (2) Use Source to execute the script: Execute in the parent process below with a practical example detailed: We create a new script test.sh
[[email protected] shellscript]$ vim test.sh//Below is the program content #!/bin/bash#program# User inputs his first name and last       name. Program shows he full name #History: # 2015/05/14 Shine_yr First releasepath=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/ Bin:usr/local/sbin:~/binexport pathread-p "Please input your first name:" FirstName #提示用户输入read-P "please input your Las T name: "LastName #提示用户输入echo-E" \nyour full name is: $firstname $lastname "#结果在屏幕输出exit 0
First, execute the script in a way that executes directly:
As can be seen from the above, the program executes smoothly, then I use the echo command to output the contents of FirstName and LastName, but the output is empty. What is the reason for this? This means that when the child process is complete, the variables or actions in the child process will end without being passed back to the parent process, as shown in:
Then, execute the script using source:
As we can see, there are definite values in the variables FirstName and LastName. That is, test.sh is executed in the parent process at this point, so the actions will take effect in the original bash.

5. Writing the shell The good habits of script are recorded in the header file: (1) script function (2) script version information (3) script author and contact (4) script's copyright notice (5) Script history (historical record) (6) More specific commands in script , using the "absolute path" method to perform (7) Pre-declaration and setting of environment variables required for script execution
In addition, we should be accustomed to using the [Tab] key to control the format, in addition, using Vim instead of VI as the authoring tool, because Vim is more powerful, not only color hints, but also the syntax detection function.

Beginner's "Linux Programming" learning-shell 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.