You can use anyProgramTo compile the shell program. Because the shell program is interpreted and executed, it does not need to be compiled into the target program. According to the shell programming Convention, for example, the first line of the program is generally "#! /Bin/bash ", where # indicates that this row is a comment and an exclamation point! Tell the shell to run the command after the exclamation mark and use the rest of the document as the input, that is, run/bin/bash and let/bin/bash execute the shell program content.
There are three methods for executing Shell programs.
1. sh shell program file name
The command format for this method is:
BASH Shell program file name
This is actually to call a new bash command to explain the program, and pass the shell program file name as a parameter to it. The newly started shell reads the specified file and runs the commands listed in the executable file. When all the commands are executed, the execution ends. The advantage of this method is that the shell debugging function can be used.
2. Sh
Format:
Bash <shell program name
In this way, the shell command interpreter uses the specified program file to redirect input.
3. Run the CHMOD command to make the shell program executable.
Whether a file can run depends on whether the content of the file is executable and the file has the execution right. For Shell programs, when a file is generated using the editor, the system grants 644 (RW-r --) Permission. Therefore, when you need to run this file, you only need to directly type the file name.
Among the three methods for running Shell programs, it is best to choose the following method: when a shell program is just created and its correctness is not yet grasped, the first method should be used for debugging. When a shell program has been debugged, use the third method to fix it. Later, you only need to enter the corresponding file name and can be called by another program.
4. 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.
-During U replacement, unconfigured variables are considered as errors.
-V displays the 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. Unset variable exit select item-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 displayed 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. -V selects the item so that the shell will display every command line it reads during the execution of the program, the-x option enables shell to display every command it executes in the process of executing the program with a + + command name at the beginning of the line. And display each variable and the value of the variable. Therefore, the main difference between them is that-V is not available before the command line is executed, and the original contents of the command line are displayed, if-V exists, the content of the replaced command line is displayed.
In addition to shell-V and-x options, you can also take some auxiliary debugging measures within the shell program. For example, you can use the echo command to display 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.