Shell script)

Source: Internet
Author: User
What is shell scripts?

What is shell script (ProgramScripts? Literally, we divide it into two parts. In the "shell" section, we have mentioned in bash that it is a tool interface that allows us to communicate with the system under a text interface. So what is "script? Literally, script is the meaning of "script, script. In the whole sentence, shell script is a "script!" written for shell !』

What? In fact,Shell script is a program written using the shell function. This program uses a pure text file to write shell syntax and commands (including external commands, it works with functions such as regular notation, pipeline commands, and data stream redirection to achieve our desired processing purpose.

Shell script provides array, loop, conditions, logical judgment, and other important functions, allowing users to directly write programs using shell, instead of using the syntax written by traditional programs like C programming languages!

 

Write the first script

In the martial arts world, whether it is the Martial School, it is necessary to learn martial arts from the ground up, so what about programs? Haha, it must have been "show Hello world !』 This word begins! OK! So laruence writes a script to show you:

[Root @ WWW ~] #Mkdir  Scripts; CD scripts [root @ WWW Scripts] # vi sh01.  Sh  # ! /Bin/ Bash # program: # This program shows  "  Hello world!  "   In  Your screen. # History :#  2005 / 08 / 23  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport path  Echo -E "  Hello world! \ A \ n  "  Exit  0 

In this chapter, place all scripts in your home directory ~ /Scripts directory, better management in the future! In the above writing, laruence mainly divides the entire program into several sections, roughly like this:

  1. First line #! /Bin/Bash:
    Because Bash is used 『#! /Bin/bashTo declare the syntax in this file using the bash syntax! When this program is run, it can load the bash environment configuration file (generally non-Login Shell ~ /. Bashrc) and run Bash to run the following commands! This is important! (In many cases, if this line is not configured, the program may not run, because the system may not be able to determine what shell the program needs to run !)

  2. Description of program content:
    In the entire script, except for the first line 『#! "Is used to declare the shell, and other # is used for" annotation! Therefore, the second line is used to describe the basic data of the entire program. In general, we recommend that you develop the Script: 1. content and functions; 2. version Information; 3. author and contact information; 4. file creation date; 5. history and so on. This will help rewrite and debug future programs!
  3. Announcement of major environment variables:
    We recommend that you configure some important environment variables. laruence believes that path and Lang are the most important! In this way, we can directly issue some external commands when this program is running, without having to write an absolute path! Better!
  4. Main program section
    Write the main program! In this example, it is the line of Echo!
  5. Running result notification (define the return value)
    Do you want to know whether a command runs successfully or not? Can you use $? Observe this variable ~We can also use the exit command to interrupt the program and return a value to the system.. In our example, laruence uses exit 0, which indicatesLeave the script and return a 0 value to the system.So after I run this script, if ECHO $? You can get a value of 0! More intelligent readers should also know, haha! With this exit n (n is a number) function, we can also customize the error information to make this program more smart!

Next, let's see the result through the running method introduced in the previous step!

[Root @ WWW Scripts] #ShSh01.ShHello World!

You will see that the screen is like this, and you should hear a bang. Why? Do you still remember the printf mentioned in the previous chapter? The same thing can happen with the special Buttons Using echo ~ However, the-e option must be added to echo! Haha! After you finish writing this script, you can say, "I will write a program too 』! Haha! It's very simple and interesting ~ Pai_^

In addition, you can also use "chmod A + x sh01.sh;./sh01.sh" to run this script!

 

Differences in script running modes (source, SH script,./script) Run the script directly.

When you use the direct commands mentioned in the previous section (either in the absolute path/relative path or in $ PATH) or bash (or sh) to issue scripts, this script uses a new bash environment to run commands in the script! That is to say, when the user runs the script in the bash of the subprogram! Parent programs/subprograms :『After the subroutine is completed, the variables or actions in the subroutine will end and will not be passed back to the parent program.』! What does this mean?

We have mentioned the sh02.sh script. This script allows the user to configure two variables, firstname and lastname. If you run this command directly, will the firstname configured by this command take effect? Let's take a look at the running results below:

 
[Root @ WWW Scripts] #Echo$ Firstname $ lastname<=OK, these two variables do not exist! [Root @ WWW Scripts] #ShSh02.ShPlease input your first name: vbird<=This name is the please input your input by laruence.LastName: Tsai your full name is: vbird Tsai<=Look! In script running, these two variables take effect [root @ WWW Scripts] #Echo$ Firstname $ lastname<= In fact, these two variables do not exist in the bash of the parent program!

The above results may seem strange to you. How can I use the variable configured in sh02.sh to invalidate it in the bash environment! What's going on? If we plot the program relevance, we will explain it. When you use a direct running method for processing, the system will give a new bash for us to run the commands in sh02.sh, so your firstname, lastname and other variables are actually running in the bash subroutine. After sh02.sh is run, all data in the bash subprogram is removed. Therefore, when ECHO $ firstname under the parent program is in the exercise above, nothing is visible! Can this be understood?


Use source to run the Script: run in the parent program

If you use source to run the command, it will be different! Run the same script:

[Root @ WWW Scripts] # source sh02.ShPlease input your first name: vbirdplease input yourLastName: tsaiyour full name is: vbird Tsai [root @ WWW Scripts] #Echo$ Firstname $ lastnamevbird Tsai<==! Data is generated!

It takes effect! That's right! The following figure shows how source runs the script! Sh02.sh runs in the parent program, so all actions will take effect in the original Bash! This is why you need to write some data without logging out of the system ~ /. When bashrc configuration is effective, you need to use "Source ~ /. Bashrc "instead of bash ~ /. Bashrc "is the same!

 

Create a good habit of writing shell scripts

It is very important to develop a good habit ~ When you first write a program, it is easy to ignore this part and think it is good to write the program. Others are not important. In fact, if the program description can be clearer, it will be of great help to you.

For example, for your own needs, I have written many scripts to help me detect Host IP addresses, analyze and manage logon files, and automatically upload and download important configuration files. However, in the early days, it was too lazy to manage too many hosts, and the same program was often changed on different hosts. At the end of the day, it was hard to remember which one was the latest, what's more, the point is, where did I change it? Why do we make such changes? I forgot everything ~ Really terrible ~

Therefore, when writing a program, laruence usually records the program design process carefully and records some historical records ~ At least it is easy to know what data I have modified, the concept of program modification, and the logic concept. It is much easier to maintain it!

In addition, in some environment configurations, after all, everyone's environment is different. In order to get a better running environment, I will first define some environment variables that will be used, for example, path! This is better ~ Therefore, we recommend that you develop a good script writing habit and record it at the beginning of each script:

    • Script functions;
    • Script version information;
    • The author and contact information of the script;
    • Copyright announcement method of script;
    • Script history (history );
    • Special commands in the script are issued using the "absolute path" method;
    • Environment variables required for script running are pre-declared and configured.

In addition to recording this information, in the special code section, you are advised to add annotations to help you a lot!

 

From http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_1.php

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.