Linux bash program debugging class

Source: Internet
Author: User

Linux bash programs are very important for programmers, especially for debugging Linux bash programs when using shell programs. There are many technical points that need attention during debugging. Next we will learn in detail.

Linux bash program debugging

Errors are inevitable during programming. Sometimes, debugging takes more time than programming, as does shell.

Shell program debugging mainly uses bash commands to explain Program Selection items. Bash is called in the form:

Bash-select the shell program file name

Several common options are:

-E: Exit immediately if a command fails.

-N: reads commands but does not execute them.

-U: During replacement, unconfigured variables are considered as errors.

-V: displays shell input rows.

-X: displays commands and their parameters when executing commands.

All the options above can also be referenced in the shell program in the form of "set-select item", and "set + select item" will disable this option. If you only want to use some selection items for a part of the program, you can enclose this part with the preceding two statements.

1. unset variable exit and exit now

The unset variable exit feature allows you to check all variables. If an unassigned variable is referenced, shell program execution is terminated. Shell usually allows the use of unspecified variables. In this case, the variable value is null. If you have set an unset variable to exit the selection item, once you have used the unset variable, the error message is displayed and the program is terminated. The unset variable exit option is "-u ".

When a shell is running, if a command does not exist or cannot be executed, the redirection fails, or the command ends abnormally, the error message is printed on the terminal screen without redirection, the shell program continues to run. To force the shell program to end immediately when an error occurs, you can use the "-e" option to terminate the execution of the shell program immediately.

2. shell program tracking

The main method to debug a shell program is to use shell commands to explain the program's "-v" or "-x" options to track program execution. The "-v" option enables shell to display every command line it reads during program execution, the "-x" option enables shell to display every command it executes with a "+" and a command name at the beginning of the line during the execution of the program. Each variable and the value obtained from the variable are also displayed. Therefore, the main difference between them is: if "-v" is not specified before the command line is executed, the original content of the command line is printed. If "-v" is provided, the content of the replaced command line is printed.

In addition to the "-v" and "-x" Options of shell, you can also take some auxiliary debugging measures within the shell program. For example, you can use the echo command to display the necessary information in some key aspects of the shell program. Its function is equivalent to the printf statement in the C language, in this way, you can know where the program runs and the current state of the program.

Linux bash program Internal commands

The Linux bash program interpreter contains some internal commands. Internal commands are invisible in the directory list. They are provided by the shell itself. Common internal Commands include echo, eval, exec, export, readonly, read, shift, wait, and point (.). The following describes the Command Format and functions.

1. echo

Command Format: echo arg

Function: print the string specified by arg on the screen.

2. eval

Command Format: eval args

Function: When the shell program runs the eval statement, the shell reads The args parameter and combines them into a new command and then runs it.

3. exec

Command Format: exec command Command Parameters

Function: When the shell executes the exec statement, it does not create a new sub-process, but instead executes the specified command. When the specified command is executed, the process, that is, the original shell is terminated, so the statements after exec in the shell program will not be executed.

4. export

Command Format: export variable name or: export variable name = variable value

Function: shell can use export to bring its variables down to the sub-shell so that the sub-process inherits the environment variables in the parent process. However, the sub-shell cannot use export to bring its variables up to the parent shell.

Note: The export statement without any variable name will display all the current export variables.

5. readonly

Command Format: readonly variable name

Function: identifies a user-defined shell variable as unchangeable. The readonly command without any parameters will display all read-only shell variables.

6. read

Command Format:

Read variable table

Function: Read a row from a standard input device, break it into several words, and assign values to variables defined in the shell program.

7. shift statement

Function: shift statements rename all location parameter variables as follows: Yes, become ...... Every shift statement used in the program makes all the location parameters move one position to the left one by one, and the location parameter "$ #" minus one until it is reduced to 0.

8. wait

Function: it refers to the completion of all sub-processes that the shell waits for to start in the background. The return value of Wait is always true.

9. exit

Function: exit the shell program. You can specify a number as the return status after exit.

10. "." (point)

Command Format:. Shell program file name

Function: enables shell to read the specified shell program file and execute all statements in the file in sequence.

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.