Shell Programming Study Notes (1) Author: Badcoffee
Email: blog.oliver@gmail.com
November 2004
Original article: http://blog.9cbs.net/yayong
Copyright: Please be sure to indicate the original source of the article in the form of hyperlink when reprinting, the author information and this statement doing nearly 5 years of software engineers on UNIX / Liunx platform.
But it is still very embarrassed, I am not familiar with shell, just a rough understanding of it.
I used to encounter problems in my work. I'm going to endure, or I walked around, using C language to solve the problem.
But now, I have to learn the shell programming.
Experience tells us that the best learning method is to learn while drying.
So, I start writing the shell program without using the shell grammar.
Learning a language to solve practical problems, it is quite easy.
In the process of writing the shell, some of the knowledge points touched, and then look back and read, and often time.
debugging
The skill of debugging is very important. Before you start writing a language program, you should be a debugging skill:
Several commonly used debug parameters in the shell are:
-e: Exit immediately if a command fails
-n: Read the command but do not perform them
-u: When the displacement does not set the variables as an error
-v: Display them when reading the Shell input row
-x: Show commands and their parameters when executing commands
All parameters above can be used in three ways, and will be taken as an example of KSH:
Command line method
Use the command line to call the shell's method to modify the step of the shell source:
# KSH -XV
# KSH -XV
2. SET statement
You can open the relevant parameters with the "SET - Parameter" statement inside the shell program, and the "set parameter" will prohibit this parameter. If you just want to debug a part of the program, you can use the part to surround the above two statements.
SET -XV ....... I want to debug the statement ........ set XV
3. Interpreter parameters
The associated debug parameter can be set at the interpreter declaration of the head of the source file:
#! / bin / ksh-xv
Debug parameter usage:
1. No variable exits and exit immediately
The uneven variable exit feature allows the user to check all variables, and if the unfielded variable is referenced, the execution of the shell program is terminated. Shell usually allows for use in an unsettled variable, in which case the value of the variable is empty. If an uneven variable exits the selection is set, the error message is displayed once used an uneven variable, and the operation of the program is terminated. The no variable exit selection is "-u".
When the shell is running, if you encounter an absence or informable command, redirect failure or command is abnormal end, etc., if it is not reordered, the error message will be printed on the terminal screen, and the shell program will continue. carried out. To force the shell program to end immediately when the error occurs, you can use the "-e" option to terminate the execution of the shell program.
2. Track of the SHELL program
The main way to debug the shell program is to use the shell command to explain the "-v" or "-x" option of the program to track the execution of the program. The "-v" selection makes the shell in the executable process, and the "-x" selection makes the shell every command to execute in the execution process. In the row, use a " " plus the command name to display it. And the value taken with each variable and the variable is also shown, therefore, their main difference is that the original content of the command line is printed until "-v" before executing the command line, and "-v" Print the contents of the replaced command line. In addition to using the "-v" and "-x" selection options, some auxiliary debugging can be taken within the shell program. For example, you can display the necessary information with some of the hellograpons in some critical places, which is equivalent to the PrintF statement in the C language, which can know where the program runs to where the program is currently available.
After learning the debugging skill, you can debug your own source of yourself; of course, learn to debug, more importantly, you can use it to learn about the source code written by others.
If you don't understand the code written by others, open the debug function is perhaps the best choice.