Shell Scripting Basics-writing runs

Source: Internet
Author: User
Write:
The shell script is actually a plain text file (ASCII), and we can edit the file to help us execute multiple instructions at a time, or use some operational and logical judgments to help us achieve certain functions. The same considerations need to be used in the writing of shell scripts:
1. Multiple spaces between instructions and parameters are ignored
2. Blank lines and tabs will also be ignored
3. If read to a Enter symbol (CR), try to start execution of the line command;
4. If there is too much content on one line, you can use \ to extend to the next line;
5. Any words appended to the # will all be treated as annotation text and ignored.
Perform:
As a result, the program that we write in the script is executed from left to right from top to bottom line. OK, so this program assumes that the filename is shell.sh and how to execute the script. Quite simply, there are several ways to do this:
1. The shell.sh plus the ability to read and execute (RX) can then be executed with the./shell.sh;
2. Direct execution can be done directly by SH shell.sh.
The point, anyway, is to get the instructions within that shell.sh to be executed. Then why do I need to use./shell.sh to give orders. Remember what we've been stressing in bash about whether instructions can be executed in relation to the environment variable of PATH, so you need to add./This directory to execute the files in the current directory. In addition, you can also put shell.sh in your home directory in the ~/bin directory, and then use Path= "$PATH": ~/bin settings, you can directly execute your script, execute the command: shell.sh
Why SH shell.sh can also be implemented. This is because/bin/sh is actually/bin/bash, using sh shell.sh to tell the system that I want to directly execute the function of bash to implement the instructions in shell.sh this file. And we can also use the parameters of sh, such as-N and-X to check and trace the syntax of shell.sh is correct.




Script instance:
[Root@linux ~]# mkdir scripts; CD scripts
[Root@linux scripts]# VI sh01.sh
#!/bin/bash
# program:
# This are used to show ' Hello world! ' in the screen.
# History:
# 2005/08/23 Vbird
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
ECHO-E "Hello World!" \a \ n "
Exit 0


It is better to manage all the written script into the ~/scripts directory of your home directory. In the above writing, I mainly divided the whole process into several paragraphs, roughly this:
1. The first line #!/bin/bash the shell name used by the script: because we are using bash, we have to declare the syntax of the file using bash with "#!/bin/bash". So when the program is executed, he can load bash's associated environment profile and execute bash to get our instructions under execution. It's very important. (In many situations, if this line is not set up, the program is likely to fail because the system may not be able to determine what shell the program needs to execute.) )
2. Announcement of the contents of the program: in the entire script, except for the first line of #! is used to declare the shell, the other # is "annotation" purpose. So in the above program, the second line below is used to illustrate the state of the entire program. In general, it is recommended that you develop a description of the script: 1. Content and function; 2. Version information; 3. Author and contact method; 4. Date of establishment of the file; 5. Historical records and so on. This will help rewrite and debug future programs.
3. Announcement of major environmental variables: it is recommended that you set up some important environment variables so that when we do this program, you can give instructions directly without having to write an absolute path. It's better, and the environment variable only takes effect on the current shell, not the system shell.
4. The main procedure section will be the main procedure written well. In this case, the Echo line.
5. Implementation results tell if you remember we were in bash to discuss the success or failure of an instruction, you can use $? This variable to observe ~ Then we can also use the exit this instruction to interrupt the program, and return a value to the system. In our example, I use exit 0, which means leaving the script and returning a 0 to the system, so after I finish this script, what about the echo $? You can get a value of 0. More intelligent readers should also know, hehe. With this exit N function, we can also customize the error message to make the program more smart.


The next step is to see what happens.
[Root@linux scripts]# SH sh01.sh
Hello World!


You will see that the screen is like this, and you should also hear "boom" and why. Remember the printf bar mentioned in the previous chapter. The same thing can happen with Echo and then those special buttons. However, echo must add the-e parameter to the line. In addition, you can also use: "Chmod a+x sh01.sh./sh01.sh" to execute this script.

Above excerpt from the bird Brother Classic, with a little modification

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.